2017-12-05 6 views
1

WooCommerce製品にバリエーションを追加すると、ブランク属性で追加されます。つまり、自分の属性を追加しようとすると、既存の配列に追加されます。私が実行していますWooCommerce製品バリエーションを作成すると、空の属性値が追加されます

例コード:

["attributes"]=> 
array(2) { 
    [0]=> 
    string(0) "" 
    ["pa_varinfo"]=> 
    string(4) "5034" 
} 

私はWooCommerce adminに製品を表示したときので、私のすべてのバリエーションがありますが、属性:その後、私は、私は、次の取得var_dump($product_variation);とき

$product_variation = wc_get_product($variation_id); 
$product_variation->set_attributes(array('attribute_pa_varinfo' => 'blue')); 
$product_variation->save(); 

それらのすべての "任意のオプション"で立ち往生しています。

no option selected

Iその後、「更新」のwp-adminのからの製品バリエーションのすべてが正しい属性が選択された取得するときに奇妙なことがあります。

誰もこれまでに遭遇したことがありますか、私は何ができるか考えていますか?

別の例として、私は次のことを実行する場合:

$product_variation = wc_get_product($variation_id); 
$product_variation->set_attributes(array ('simon' => 'confused')); 
$product_variation->save(); 
var_dump($product_variation->get_attributes()); 

これが返されます。

最初の項目から来たん
array(2) { 
[0]=> string(0) "" 
["simon"]=> string(8) "confused" 
} 

を?私はそれをクリアするように見えることはできません。

+0

私は私の答えを更新しました[OK]を...あなたは、私がそのコードを試してみましたし、同期機能は、私が行方不明になった何だったことを期待@LoicTheAztec助けを – LoicTheAztec

答えて

1

をので、それは問題が主な親製品に対する属性の実際の設定であったことが判明しました。私は無名の配列を通りました。wc_attribute_taxonomy_name('varinfo') =>(2行目下)、これは正しくデータを保存し、私が持っていた空の配列を削除します。

 $product_attributes = array(
     wc_attribute_taxonomy_name('varinfo') => 
     array (
     'name'   => wc_attribute_taxonomy_name('varinfo'), // set attribute name 
     'value'  => '', // set attribute values 
     'position'  => 1, 
     'is_visible' => 1, 
     'is_variation' => 1, 
     'is_taxonomy' => 1 
    ) 
    ); 

    update_post_meta($post_id, '_product_attributes', $product_attributes); 
1

更新(あなたの更新やコメントに関連する)

(当社コメント)を再開するには:製品が存在する属性。私は "新しいを作成する必要があり

  1. :また、この属性のすべての用語が定義され、親(「属性」の設定]タブで)変数製品

に設定されている私はいくつかのテストをメイド持っています
104 mm110 mm130 mm140 mm(その用語のナメクジが... 104-mmのようなもの):4つの値(用語名)とVarinfo」属性(pa_varinfo)。

  • 私は1つの空の変化(この変化のために定義され何もない)で新しい変数の製品を作成し、選択フィールドを示し節約変更しています
    enter image description here
  • (あなたに似ている)、このコードを使用する場合:

    $parent_product = wc_get_product(738); // Get the variable product 
    $variation_ids = $parent_product->get_children(); // Get all children variations (Here only one) 
    
    // Iterating through each variation 
    foreach($variation_ids as $variation_id){ 
        $variation = wc_get_product($variation_id); 
        $variation->set_attributes(array('pa_varinfo' => '104-mm')); 
        $variation->save(); 
    } 
    

    それはちょうど私のために働いていると私は、この変動をバックエンドで選択した値を取得する: enter image description here

    私は、アレイ内の属性と長期SLUGため分類名を使用しています注 ...

    だから私はあなたが何か間違っをやっているを知りません...


    これは、存在しない、および/またはpの後期として登録されていない属性用語を設定すると発生します可変の製品ではありません。あなたはこれを試すことができます。これは、テストの結果、あなたはすでにWooCommerce ...で追加の属性を作成したと仮定すると

    に動作さ

    // Get an instance of the WC_Product_Variation object 
    $variation = wc_get_product($variation_id); 
    
    // Initialising variables 
    $taxonomy = 'pa_varinfo'; // The taxonomy 
    $term_name = 'Blue'; // The term "NAME" 
    
    // Check if the term exist and if not we create it. 
    if(! term_exists($term_name, $taxonomy)) 
        wp_insert_term($term_name, $taxonomy); 
    
    // Get an instance of the WP_Term object 
    $term = get_term_by('name', $term_name, $taxonomy); 
    
    // Get the post terms names from the parent variable product. 
    $post_term_names = wp_get_post_terms($variation->get_parent_id(), $taxonomy, array('fields' => 'names')); 
    
    // Check if the post term exist and if not we set it in the parent variable product. 
    if(! in_array($term_name, $post_term_names)) 
        wp_set_post_terms($variation->get_parent_id(), $term_name, $taxonomy, true); 
    
    // Now you can set the term for the attribute in your variation 
    $variation->set_attributes(array($taxonomy => $term->slug)); 
    $variation->save(); // Registering the data 
    
    // Get an instance of the parent WC_Product_Variable object 
    $parent_product = wc_get_product($variation->get_parent_id()); 
    
    // Sync the data of the variation in the parent variable product 
    $parent_product->sync($variation_id); 
    

    、あなたが得る:

    enter image description here

    +0

    感謝を確認することができます。悲しいかな、私はまだ同じ問題に悩まされています。そのバリエーションのどれにも属性が割り当てられていません。そして、私は以下を取得します:http://i.imgur.com/TnmTW0I.png –

    +0

    @SimonPollardあなたの質問は、それがバックエンドにあると言っていないので、明確で明示的ではありません...私の答えはフロントエンドに関連しています。今私は少し後で(バックエンドのために)私の答えを更新します... – LoicTheAztec

    +0

    お詫び - 私は、より明確にするために質問を書き直して、画像を追加しました - あなたの助けに感謝します –

    関連する問題