Advertisement
retoo

Woocommerce Produkt Variation - ein eigenes Feld einfügen

Mar 15th, 2021
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.14 KB | None | 0 0
  1. // Display Fields
  2. add_action( 'woocommerce_product_after_variable_attributes', 'variable_fields', 10, 2 );
  3. //JS to add fields for new variations
  4. add_action( 'woocommerce_product_after_variable_attributes_js', 'variable_fields_js' );
  5. //Save variation fields
  6. add_action( 'woocommerce_process_product_meta_variable', 'variable_fields_process', 10, 1 );
  7. function variable_fields( $loop, $variation_data ) { ?>
  8.     <tr>
  9.         <td>
  10.             <div>
  11.                     <label></label>
  12.                     <input type="text" size="5" name="my_custom_field[]" value=""/>
  13.             </div>
  14.         </td>
  15.     </tr>
  16. <tr>
  17.         <td>
  18.             <div>
  19.                     <label></label>
  20.             </div>
  21.         </td>
  22.     </tr>
  23. <?php }
  24. function variable_fields_process( $post_id ) {
  25.     if (isset( $_POST['variable_sku'] ) ) :
  26.         $variable_sku = $_POST['variable_sku'];
  27.         $variable_post_id = $_POST['variable_post_id'];
  28.         $variable_custom_field = $_POST['my_custom_field'];
  29.         for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
  30.             $variation_id = (int) $variable_post_id[$i];
  31.             if ( isset( $variable_custom_field[$i] ) ) {
  32.                 update_post_meta( $variation_id, '_my_custom_field', stripslashes( $variable_custom_field[$i] ) );
  33.             }
  34.         endfor;
  35.     endif;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement