2017-11-21 9 views
1

商品に属性を追加する際に苦労しています。WooCommerce:商品の既存商品に商品属性を追加する

私は、製品に追加したいキーワードの配列を持っている:

$clean_keywords = array('cake','cup cakes'); 
$term_taxonomy_ids = wp_set_object_terms(get_the_ID(), $clean_keywords, 'pa_keywords', true); 
$thedata = Array('pa_keywords' => Array(
    'name' => 'pa_keywords', 
    'value' => '', 
    'is_visible' => '0', 
    'is_taxonomy' => '1' 
)); 

update_post_meta(get_the_ID(),'_product_attributes',$thedata); 

これは正常に動作しますが、それはすべて私の他の属性は、製品に添付削除されます。

解決策は、現在の属性を取得し、$thedata変数とマージすることですが、これを行う方法がわかりません。

アイデア?あなたは、既存の製品が最初の属性と、それを保存する前に、アレイに新しい製品の属性を挿入取得する必要があります

おかげ

答えて

1

$product_id = get_the_ID(); 
$taxonomy = 'pa_keywords'; 
$clean_keywords = array('cake','cup cakes'); 
$term_taxonomy_ids = wp_set_object_terms($product_id, $clean_keywords, $taxonomy, true); 

// Get existing attributes 
$product_attributes = get_post_meta($product_id, '_product_attributes', true); 

// get the count of existing attributes to set the "position" in the array 
$count = count($product_attributes); 

// Insert new attribute in existing array of attributes (if there is any) 
$product_attributes[$taxonomy] = array(
    'name' => $taxonomy, 
    'value' => '', 
    'position' => $count, // added 
    'is_visible' => '0', 
    'is_variation' => '0', // added (set the right value) 
    'is_taxonomy' => '1' 
); 

// Save the data 
update_post_meta($product_id, '_product_attributes', $product_attributes); 

これは、既存のデータを削除せずに動作するようになりました。また、私はだからあなたのコードがあるべき...配列に

を2つの欠落している引数を追加しています。

+0

これは完璧に働いた、ありがとう。 – Amz4u2nv

+0

ちょうどそれを行う方法を考え出した...もう一度ありがとう – Amz4u2nv

関連する問題