在不丢失现有属性的情况下添加 WooCommerce 产品属性
Add WooCommerce product attribute without losing existings
我已经用两个属性(pa_size、pa_color)以编程方式向我的 WooCommerce 添加了一些产品。它们都用于变化。现在我想制作一个 php 文件,它将在每个产品中插入一个属性 (pa_brand)。这将仅用于可见性而不用于变化。
我尝试了一些代码,例如:
$term_taxonomy_ids = wp_set_object_terms( $productID, $productBrand, 'pa_brand', true );
$thedata = Array('pa_brand'=>Array(
'name'=>'pa_brand',
'value'=>$productBrand,
'is_visible' => '1',
'is_taxonomy' => '1'
));
update_post_meta( $productID,'_product_attributes',$thedata);
但我的问题是,通过这种方式,添加了品牌属性,但我已经拥有的属性却丢失了。
结果是我得到的所有产品都只有一个属性。
有没有办法只添加一个属性,而不会丢失之前的任何内容(属性 -
update_post_meta()
将在调用时始终更改值 - 您需要先获取现有的元数据并将其也存储在数组中:
$term_taxonomy_ids = wp_set_object_terms( $productID, $productBrand, 'pa_brand', true );
$existingData['pa_size'] = get_post_meta($productID, 'pa_size', true);
$existingData['pa_color'] = get_post_meta($productID, 'pa_color', true);
$thedata = Array(
'pa_brand'=>Array(
'name'=>'pa_brand',
'value'=>$productBrand,
'is_visible' => '1',
'is_taxonomy' => '1'
),
'pa_size' => Array(
'name'=>'pa_size',
'value'=>$existingData['pa_size'],
'is_visible' => '1',
'is_taxonomy' => '1'
),
'pa_color' => Array(
'name'=>'pa_color',
'value'=>$existingData['pa_color'],
'is_visible' => '1',
'is_taxonomy' => '1'
)
);
update_post_meta( $productID,'_product_attributes',$thedata);
最后,我在最后一行添加了这个:
wp_set_object_terms( $productID, $productBrand, 'pa_brand' , true);
现在好像没什么问题了。我希望它也能帮助你。
我已经用两个属性(pa_size、pa_color)以编程方式向我的 WooCommerce 添加了一些产品。它们都用于变化。现在我想制作一个 php 文件,它将在每个产品中插入一个属性 (pa_brand)。这将仅用于可见性而不用于变化。
我尝试了一些代码,例如:
$term_taxonomy_ids = wp_set_object_terms( $productID, $productBrand, 'pa_brand', true );
$thedata = Array('pa_brand'=>Array(
'name'=>'pa_brand',
'value'=>$productBrand,
'is_visible' => '1',
'is_taxonomy' => '1'
));
update_post_meta( $productID,'_product_attributes',$thedata);
但我的问题是,通过这种方式,添加了品牌属性,但我已经拥有的属性却丢失了。
结果是我得到的所有产品都只有一个属性。
有没有办法只添加一个属性,而不会丢失之前的任何内容(属性 -
update_post_meta()
将在调用时始终更改值 - 您需要先获取现有的元数据并将其也存储在数组中:
$term_taxonomy_ids = wp_set_object_terms( $productID, $productBrand, 'pa_brand', true );
$existingData['pa_size'] = get_post_meta($productID, 'pa_size', true);
$existingData['pa_color'] = get_post_meta($productID, 'pa_color', true);
$thedata = Array(
'pa_brand'=>Array(
'name'=>'pa_brand',
'value'=>$productBrand,
'is_visible' => '1',
'is_taxonomy' => '1'
),
'pa_size' => Array(
'name'=>'pa_size',
'value'=>$existingData['pa_size'],
'is_visible' => '1',
'is_taxonomy' => '1'
),
'pa_color' => Array(
'name'=>'pa_color',
'value'=>$existingData['pa_color'],
'is_visible' => '1',
'is_taxonomy' => '1'
)
);
update_post_meta( $productID,'_product_attributes',$thedata);
最后,我在最后一行添加了这个:
wp_set_object_terms( $productID, $productBrand, 'pa_brand' , true);
现在好像没什么问题了。我希望它也能帮助你。