2016-09-23 4 views
3

最初の製品バリエーションフィールドを保存すると保存されますが、2番目、3番目、4番目を保存しようとすると保存されません。複数の製品バリエーションフィールドを保存できません

私はSKUを製品バリエーションごとに記入していますので、動作するはずです。しかし、それは私がここで紛失しているものですか?これは最新のWordPressと最新のWooCommerceバージョンです。私は保存するためのアクション&機能を変更した

//Display Fields 
add_action('woocommerce_product_after_variable_attributes', 'variable_fields', 10, 3); 

//Save variation fields 
add_action('woocommerce_save_product_variation', 'save_variable_fields', 10, 1); 


/** 
* Create new fields for variations 
* 
*/ 
function variable_fields($loop, $variation_data, $variation) { 
?> 
<tr> 
    <td> 
     <?php 
     // Textarea 
     woocommerce_wp_textarea_input(
      array(
       'id'   => '_textarea['.$loop.']', 
       'label'  => __('Contains', 'woocommerce'), 
       'placeholder' => '', 
       'description' => __('<br />Contains', 'woocommerce'), 
       'value'  => get_post_meta($variation->ID, '_textarea', true), 
      ) 
     ); 
     ?> 
    </td> 
</tr> 

<tr> 
    <td> 
     <?php 
     // Textarea 
     woocommerce_wp_textarea_input(
      array(
       'id'   => '_textarea_2['.$loop.']', 
       'label'  => __('Observation message', 'woocommerce'), 
       'placeholder' => '', 
       'description' => __('<br />Observation message', 'woocommerce'), 
       'value'  => get_post_meta($variation->ID, '_textarea_2', true), 
      ) 
     ); 
     ?> 
    </td> 
</tr> 

<?php 
} 


/** 
* Save new fields for variations 
* 
*/ 
function save_variable_fields($post_id) { 
    if (isset($_POST['variable_sku'])) : 

     $variable_sku   = $_POST['variable_sku']; 
     $variable_post_id  = $_POST['variable_post_id']; 

     // Textarea 
     $_textarea = $_POST['_textarea']; 
     for ($i = 0; $i < sizeof($variable_sku); $i++) : 
      $variation_id = (int) $variable_post_id[$i]; 
      if (isset($_textarea[$i])) { 
       update_post_meta($variation_id, '_textarea', stripslashes($_textarea[$i])); 
      } 
     endfor; 


     // Textarea 
     $_textarea_2 = $_POST['_textarea_2']; 
     for ($i = 0; $i < sizeof($variable_sku); $i++) : 
      $variation_id = (int) $variable_post_id[$i]; 
      if (isset($_textarea_2[$i])) { 
       update_post_meta($variation_id, '_textarea_2', stripslashes($_textarea_2[$i])); 
      } 
     endfor; 

    endif; 
} 

答えて

1

:あなたの答えのための

//Save variation fields 
add_action('woocommerce_save_product_variation', 'save_variable_fields', 10, 2); 
/** 
* Save new fields for variations 
* 
*/ 
function save_variable_fields($variation_id, $i) { 
    if (empty($variation_id)) return; 
    if (isset($_POST['_textarea'][$i])) { 
     update_post_meta($variation_id, '_textarea', stripslashes($_POST['_textarea'][$i])); 
    } 
    if (isset($_POST['_textarea_2'][$i])) { 
     update_post_meta($variation_id, '_textarea_2', stripslashes($_POST['_textarea_2'][$i])); 
    } 
} 
+0

おかげで、残念ながら私はまだそれを動作させることはできません。 –

+0

上記のHTMLコードでこのコードをテストしたところ、うまくいきました。あなたはこのコードを変更する前にこのコードを使用しましたか? –

+0

いいえ、私は正確にそのコードを使用しました。最初の変数は編集できますが、2番目以降の変数は編集できません。私の古いコードと同じ問題です。 –

関連する問題