Advertisement
FireBot

woocommerce-custom-variation-fields-functions-php

Nov 19th, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.11 KB | None | 0 0
  1. /**
  2.  * Create new fields for variations
  3.  *
  4. */
  5. function variable_fields( $loop, $variation_data ) {
  6. ?>
  7.     <tr>
  8.         <td>
  9.             <?php
  10.             // Text Field
  11.             woocommerce_wp_text_input(
  12.                 array(
  13.                     'id'          => 'color_hex['.$loop.']',
  14.                     'label'       => __( 'Swatch Color', 'woocommerce' ),
  15.                     'placeholder' => '#ffffff',
  16.                     'desc_tip'    => 'true',
  17.                     'description' => __( 'Enter hex value of color to display when option is selected.', 'woocommerce' ),
  18.                     'value'       => $variation_data['color_hex'][0]
  19.                 )
  20.             );
  21.             ?>
  22.         </td>
  23.     </tr>
  24. <?php
  25. }
  26.  
  27. /**
  28.  * Create new fields for new variations
  29.  *
  30. */
  31. function variable_fields_js() {
  32. ?>
  33.     <tr>
  34.         <td>
  35.             <?php
  36.             // Text Field
  37.             woocommerce_wp_text_input(
  38.                 array(
  39.                     'id'          => 'color_hex[ + loop + ]',
  40.                     'label'       => __( 'Swatch Color', 'woocommerce' ),
  41.                     'placeholder' => '#ffffff',
  42.                     'desc_tip'    => 'true',
  43.                     'description' => __( 'Enter hex value of color to display when option is selected.', 'woocommerce' ),
  44.                     'value'       => $variation_data['color_hex'][0]
  45.                 )
  46.             );
  47.             ?>
  48.         </td>
  49.     </tr>
  50. <?php
  51. }
  52.  
  53. /**
  54.  * Save new fields for variations
  55.  *
  56. */
  57. function save_variable_fields( $post_id ) {
  58.     if (isset( $_POST['variable_sku'] ) ) :
  59.  
  60.         $variable_sku          = $_POST['variable_sku'];
  61.         $variable_post_id      = $_POST['variable_post_id'];
  62.        
  63.         // Text Field
  64.         $color_hex = $_POST['color_hex'];
  65.         for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
  66.             $variation_id = (int) $variable_post_id[$i];
  67.             if ( isset( $color_hex[$i] ) ) {
  68.                 update_post_meta( $variation_id, 'color_hex', stripslashes( $color_hex[$i] ) );
  69.             }
  70.         endfor;
  71.        
  72.     endif;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement