Advertisement
olie480

WOOCOMMERCE: Woocommerce Custom Product Fields

Apr 1st, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.96 KB | None | 0 0
  1. <?php
  2. // http://www.remicorson.com/mastering-woocommerce-products-custom-fields/
  3.  
  4. /*
  5. The right hooks
  6.  
  7. The first step is to hook an action to woocommerce_product_options_general_product_data. The function linked to this hook will be responsible of the new fields display. A second hook will be taken into account to save fields values: woocommerce_process_product_meta. Basically these two actions will be done using that code:
  8. */
  9.  
  10. // Display Fields
  11. add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
  12.  
  13. // Save Fields
  14. add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
  15.  
  16. /*
  17. Adding New Fields
  18.  
  19. The snippet above link two custom functions to the right hooks. Now we need to create those functions. Let’s start bye the first one, woo_add_custom_general_fields(), that will create the fields. Here is how the function will look like
  20. */
  21. function woo_add_custom_general_fields() {
  22.  
  23. global $woocommerce, $post;
  24. echo '<div class="options_group">';
  25. // Custom fields will be created here...
  26. echo '</div>';
  27. }
  28.  
  29. /*
  30. To create our fields we will mainly use WooCommerce builtin function like (All these functions are located in WooCommerce/Admin/WritePanels/writepanels-init.php.):
  31.  
  32.     woocommerce_wp_text_input()
  33.     woocommerce_wp_textarea_input()
  34.     woocommerce_wp_select()
  35.     woocommerce_wp_checkbox()
  36.     woocommerce_wp_hidden_input()
  37.  
  38.  
  39. Text Field Type
  40.  
  41. To create a text field type, you will need to use that code:*/
  42.  
  43. // Text Field
  44. woocommerce_wp_text_input(
  45. array(
  46.     'id' => '_text_field',
  47.     'label' => __( 'My Text Field', 'woocommerce' ),
  48.     'placeholder' => 'http://',
  49.     'desc_tip' => 'true',
  50.     'description' => __( 'Enter the custom value here.', 'woocommerce' )
  51.     )
  52. );
  53.  
  54. /*
  55. Please note the use of desc_tip to display those little nice bubble to the right of the field instead of displaying the default field description. This attribute works for every field type.
  56. Number Field Type
  57.  
  58. To create a number field type, you will have to use that code:*/
  59.  
  60. // Number Field
  61. woocommerce_wp_text_input(
  62.     array(
  63.         'id' => '_number_field',
  64.         'label' => __( 'My Number Field', 'woocommerce' ),
  65.         'placeholder' => '',
  66.         'description' => __( 'Enter the custom value here.', 'woocommerce' ),
  67.         'type' => 'number',
  68.         'custom_attributes' => array(
  69.         'step' => 'any',
  70.         'min' => '0'
  71.         )
  72.     )
  73. );
  74.  
  75. /*
  76. The main difference here is the type attribute set to number. You can also define custom attributes like step, and min, or even max. In the code above, step is the default one (1), and min is set to zero. Basically that means that we expect a positive value here (at least greater than zero).
  77. Textarea Field Type
  78.  
  79. To create a textarea, here is the code to use:*/
  80.  
  81. // Textarea
  82. woocommerce_wp_textarea_input(
  83. array(
  84.     'id' => '_textarea',
  85.     'label' => __( 'My Textarea', 'woocommerce' ),
  86.     'placeholder' => '',
  87.     'description' => __( 'Enter the custom value here.', 'woocommerce' )
  88.     )
  89. );
  90.  
  91. /*
  92. Nothing really complex here…
  93.  
  94. Dropdown Select Field Type
  95.  
  96. To create a dropdown select, use the following code:*/
  97.  
  98. // Select
  99. woocommerce_wp_select(
  100.     array(
  101.         'id' => '_select',
  102.         'label' => __( 'My Select Field', 'woocommerce' ),
  103.         'options' => array(
  104.         'one' => __( 'Option 1', 'woocommerce' ),
  105.         'two' => __( 'Option 2', 'woocommerce' ),
  106.         'three' => __( 'Option 3', 'woocommerce' )
  107.         )
  108.     )
  109. );
  110.  
  111. /*
  112. The options attributes defines available options within an array.
  113. Checkbox Field Type
  114.  
  115. To create a checkbox, use this code below:*/
  116.  
  117. // Checkbox
  118. woocommerce_wp_checkbox(
  119.     array(
  120.         'id' => '_checkbox',
  121.         'wrapper_class' => 'show_if_simple',
  122.         'label' => __('My Checkbox Field', 'woocommerce' ),
  123.         'description' => __( 'Check me!', 'woocommerce' )
  124.     )
  125. );
  126.  
  127.  
  128. /*
  129. Hidden Field Type
  130.  
  131. You can also create hidden fields with the following code:*/
  132.  
  133. // Hidden field
  134. woocommerce_wp_hidden_input(
  135. array(
  136. 'id' => '_hidden_field',
  137. 'value' => 'hidden_value'
  138. )
  139. );
  140.  
  141. /*
  142. Products Select Field Type
  143.  
  144. There’s a really nice way to create a customized dropdown select for WooCommerce products with that code:*/
  145.  
  146. // Product Select
  147. ?>
  148. <p class="form-field product_field_type">
  149. <label for="product_field_type"><?php _e( 'Product Select', 'woocommerce' ); ?></label>
  150. <select id="product_field_type" name="product_field_type[]" class="ajax_chosen_select_products" multiple="multiple" data-placeholder="<?php _e( 'Search for a product&hellip;', 'woocommerce' ); ?>">
  151. <?php
  152. $product_field_type_ids = get_post_meta( $post->ID, '_product_field_type_ids', true );
  153. $product_ids = ! empty( $product_field_type_ids ) ? array_map( 'absint', $product_field_type_ids ) : null;
  154. if ( $product_ids ) {
  155. foreach ( $product_ids as $product_id ) {
  156.  
  157. $product = get_product( $product_id );
  158. $product_name = woocommerce_get_formatted_product_name( $product );
  159.  
  160. echo '<option value="' . esc_attr( $product_id ) . '" selected="selected">' . esc_html( $product_name ) . '</option>';
  161. }
  162. }
  163. ?>
  164. </select> <img class="help_tip" data-tip='<?php _e( 'Your description here', 'woocommerce' ) ?>' src="<?php echo $woocommerce->plugin_url(); ?>/assets/images/help.png" height="16" width="16" />
  165. </p>
  166. <?php
  167.  
  168. /*
  169. Custom Field Type
  170.  
  171. You can also create custom fields. In the example below, i created a “double field”. You can customize that code and use as many fields as you want that will be merged into one single array:*/
  172.  
  173. // Custom field Type
  174. ?>
  175. <p class="form-field custom_field_type">
  176. <label for="custom_field_type"><?php echo __( 'Custom Field Type', 'woocommerce' ); ?></label>
  177. <span class="wrap">
  178. <?php $custom_field_type = get_post_meta( $post->ID, '_custom_field_type', true ); ?>  
  179. <input placeholder="<?php _e( 'Field One', 'woocommerce' ); ?>" class="" type="number" name="_field_one" value="<?php echo $custom_field_type[0]; ?>" step="any" min="0" style="width: 80px;" />
  180. <input placeholder="<?php _e( 'Field Two', 'woocommerce' ); ?>" type="number" name="_field_two" value="<?php echo $custom_field_type[1]; ?>" step="any" min="0" style="width: 80px;" />
  181. </span>
  182. <span class="description"><?php _e( 'Place your own description here!', 'woocommerce' ); ?></span>
  183. </p>
  184. <?php
  185.  
  186. /*
  187. Custom fields can be pretty much everything, just make sure that you use the form-field class to make them pretty!
  188.  
  189. Saving Fields Values
  190.  
  191. Now that you created your WooCommerce product fields, you need to create a function to save their values once you edit the update or publish button. As we saw earlier, we will use a function called woo_add_custom_general_fields_save() hooked to woocommerce_process_product_meta. Basically the idea behind this function is pretty simple: we check if the field is empty and if not we create a post meta using update_post_meta(). Please note that we use esc_attr() and esc_html() to secure data just a bit. Here is the code to save each field type values:*/
  192.  
  193. function woo_add_custom_general_fields_save( $post_id ){
  194.    
  195.     // Text Field
  196.     $woocommerce_text_field = $_POST['_text_field'];
  197.     if( !empty( $woocommerce_text_field ) )
  198.         update_post_meta( $post_id, '_text_field', esc_attr( $woocommerce_text_field ) );
  199.        
  200.     // Number Field
  201.     $woocommerce_number_field = $_POST['_number_field'];
  202.     if( !empty( $woocommerce_number_field ) )
  203.         update_post_meta( $post_id, '_number_field', esc_attr( $woocommerce_number_field ) );
  204.        
  205.     // Textarea
  206.     $woocommerce_textarea = $_POST['_textarea'];
  207.     if( !empty( $woocommerce_textarea ) )
  208.         update_post_meta( $post_id, '_textarea', esc_html( $woocommerce_textarea ) );
  209.        
  210.     // Select
  211.     $woocommerce_select = $_POST['_select'];
  212.     if( !empty( $woocommerce_select ) )
  213.         update_post_meta( $post_id, '_select', esc_attr( $woocommerce_select ) );
  214.        
  215.     // Checkbox
  216.     $woocommerce_checkbox = isset( $_POST['_checkbox'] ) ? 'yes' : 'no';
  217.     update_post_meta( $post_id, '_checkbox', $woocommerce_checkbox );
  218.    
  219.     // Custom Field
  220.     $custom_field_type =  array( esc_attr( $_POST['_field_one'] ), esc_attr( $_POST['_field_two'] ) );
  221.     update_post_meta( $post_id, '_custom_field_type', $custom_field_type );
  222.    
  223.     // Hidden Field
  224.     $woocommerce_hidden_field = $_POST['_hidden_field'];
  225.     if( !empty( $woocommerce_hidden_field ) )
  226.         update_post_meta( $post_id, '_hidden_field', esc_attr( $woocommerce_hidden_field ) );
  227.        
  228.     // Product Field Type
  229.     $product_field_type =  $_POST['product_field_type'];
  230.     update_post_meta( $post_id, '_product_field_type_ids', $product_field_type );
  231.    
  232. }
  233.  
  234. /*
  235. Retrieve Fields Values
  236.  
  237. Now that we successfully created our fields and saved their values, i guess you’d like to display those values on the frontend. In this case the best method would be to work with WooCommerce custom templates. Basically a custom template allows you to override WooCommerce default files and use your own custom files instead. Here is a quick tutorial that will explain you how to create your custom templates: http://docs.woothemes.com/document/template-structure/
  238.  
  239. To get those values we just need to use the popular get_post_meta() function. That’s pretty much all you need.
  240.  
  241. Example:*/
  242.  
  243.  
  244. // Display Custom Field Value
  245. echo get_post_meta( $post->ID, 'my-field-slug', true );
  246.  
  247. // You can also use
  248. echo get_post_meta( get_the_ID(), 'my-field-slug', true );
  249.  
  250. /*
  251. Create Custom Tabs
  252.  
  253. Finally here is a quick snippet to create a custom product tab:
  254. */
  255.  
  256. add_action( 'woocommerce_product_write_panel_tabs', 'woo_add_custom_admin_product_tab' );
  257.  
  258. function woo_add_custom_admin_product_tab() {
  259.     ?>
  260.     <li class="custom_tab"><a href="#custom_tab_data"><?php _e('My Custom Tab', 'woocommerce'); ?></a></li>
  261. }
  262. <?php
  263.  
  264. /*
  265. I recommend you to style your tab using a bit of CSS (simply add a nice icon and you’re done!).
  266. One More Thing
  267.  
  268. One more thing: if you want to add your fields to any other tab than the general one you simply need to modify the hook name you linked your woo_add_custom_general_fields() function to. Fo example use that hook woocommerce_product_options_shipping to add your fields to the shipping tab. You can find all the available hooks in  woocommerce/admin/post-types/writepanels/writepanel-product_data.php.*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement