Guest User

Untitled

a guest
Nov 23rd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. <?php
  2. // Display Fields
  3. add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');
  4.  
  5. // Save Fields
  6. add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
  7.  
  8.  
  9. function woocommerce_product_custom_fields(){
  10. global $woocommerce, $post;
  11. echo '<div class="product_custom_field">';
  12. // Custom Product Checkbox Field
  13. woocommerce_wp_checkbox(
  14. array(
  15. 'id' => '_custom_product_checkbox_field',
  16. 'placeholder' => 'Custom Product Checkbox Field',
  17. 'label' => __('Custom Product Checkbox Field', 'woocommerce'),
  18. 'desc_tip' => 'true'
  19. )
  20. );
  21. echo '</div>';
  22.  
  23. }
  24.  
  25. // Saving Values
  26. function woocommerce_product_custom_fields_save($post_id){
  27. // Custom Product Text Field
  28. $woocommerce_custom_product_checkbox_field = $_POST['_custom_product_checkbox_field'];
  29. if (!empty($woocommerce_custom_product_checkbox_field ))
  30. update_post_meta($post_id, '_custom_product_checkbox_field', esc_attr($woocommerce_custom_product_checkbox_field ));
  31. }
  32. ?>
  33.  
  34. function add_engrave_text_field() {
  35. if (is_single('product-url-a')) {
  36. echo 'Enter your chosen letters: <span id="character_count"></span>
  37. <div><table class="variations" cellspacing="0">
  38. <tbody>
  39. <tr>
  40. <td class="value"><label class="product-custom-text-label" for="custom_text">Custom Text</label></td>
  41. <td class="value">
  42. <label><input type="text" class="product-counter" name="engrave_text" placeholder="Enter Your Custom Letters ..." maxlength="3" /></label>
  43. </td>
  44. </tr>
  45. </tbody>
  46. </table></div>';
  47. }
  48. }
  49. add_action( 'woocommerce_before_add_to_cart_button', 'add_engrave_text_field', 0 );
  50.  
  51. // Display Fields
  52. add_action('woocommerce_product_options_general_product_data', 'product_custom_fields_add');
  53. function product_custom_fields_add(){
  54.  
  55. echo '<div class="product_custom_field">';
  56.  
  57. // Custom Product Checkbox Field
  58. woocommerce_wp_checkbox(
  59. array(
  60. 'id' => '_engrave_text_option',
  61. 'desc' => __('set custom Engrave text field', 'woocommerce'),
  62. 'label' => __('Display custom Engrave text field', 'woocommerce'),
  63. 'desc_tip' => 'true'
  64. )
  65. );
  66.  
  67. echo '</div>';
  68. }
  69.  
  70. // Save Fields
  71. add_action('woocommerce_process_product_meta', 'product_custom_fields_save');
  72. function product_custom_fields_save($post_id){
  73. // Custom Product Text Field
  74. if ( ! empty( $_POST['_engrave_text_option'] ) )
  75. update_post_meta($post_id, '_engrave_text_option', esc_attr( $_POST['_engrave_text_option'] ));
  76. }
  77.  
  78. add_action( 'woocommerce_before_add_to_cart_button', 'add_engrave_text_field', 0 );
  79. function add_engrave_text_field() {
  80. global $post;
  81.  
  82. // Get the checkbox value
  83. $engrave_option = get_post_meta( $post->ID, '_engrave_text_option', true );
  84.  
  85. // If is single product page and have the "engrave text option" enabled we display the field
  86. if ( is_product() && ! empty($engrave_option) ) {
  87.  
  88. ?>
  89. <div>
  90. <label class="product-custom-text-label" for="engrave_text"><?php _e( 'Engraving option:', 'woocommerce'); ?><br>
  91. <input style="min-width:220px" type="text" class="product-counter" name="engrave_text" placeholder="<?php _e( 'Enter Your Custom Letters ...', 'woocommerce'); ?>" maxlength="3" />
  92. </label>
  93. </div><br>
  94. <?php
  95. }
  96. }
  97.  
  98. add_action( 'woocommerce_before_add_to_cart_button', 'add_engrave_text_field', 0 );
  99. function add_engrave_text_field() {
  100. global $post;
  101.  
  102. // Get the checkbox value
  103. $engrave_option = get_post_meta( $post->ID, '_engrave_text_option', true );
  104.  
  105. // If is single product page and have the "engrave text option" enabled we display the field
  106. if ( is_product() && ! empty($engrave_option) ) {
  107.  
  108. ?>
  109. <div>
  110. <label class="product-custom-text-label" for="engrave_text"><?php _e( 'Engraving option:', 'woocommerce'); ?><br>
  111. <input style="min-width:220px" type="text" class="product-counter" name="engrave_text" placeholder="<?php _e( 'Enter Your Custom Letters ...', 'woocommerce'); ?>" maxlength="3" />
  112. </label>
  113. </div><br>
  114. <?php
  115. }
  116. }
Add Comment
Please, Sign In to add comment