Woocommerce以编程方式添加和启用属性



我们有一个Woocommerce网站,上面有超过1000种产品。

是否可以为每个产品设置一个默认的属性项。

属性名称为'pa_arm-length',术语为'1'。

例如,每个产品都需要一个启用术语为'1'的属性;

不需要单独进入每个产品,启用属性,然后分配一个术语,这是可能的吗?

每个产品的期望

我可以这样做但它只在属性已经赋值了一个词

时才有效
wp_set_object_terms( $object_id, '1', 'pa_arm-length' , true);

提前感谢。

我想这就是你要找的。这将为每个值为1的产品添加pa_arm-length。如果这对你有用,请告诉我。您也可以点击此链接了解更多详情

$target_products = array(
'post_type' => 'product',
'post_status' => 'publish'
);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); 
$term_taxonomy_ids = wp_set_object_terms( get_the_ID(), '1', 'pa_arm-length', true );
$thedata = Array('pa_arm-length'=>Array(
'name'=>'pa_arm-length',
'value'=>'1',
'is_visible' => '1',
'is_taxonomy' => '1'
));
update_post_meta( get_the_ID(),'_product_attributes',$thedata); 
endwhile;
}
wp_reset_query();

如果这对其他人有帮助,我有一个更新。

以下代码需要在产品循环中运行。

$thedata = Array(
'pa_arm-length' => Array(
'name'=>'pa_arm-length',
'value' => '1',
'is_visible' => '1',
'is_taxonomy' => '1'
), 
'pa_lens-height' => Array(
'name'=>'pa_lens-height',
'value' => '1',
'is_visible' => '1',
'is_taxonomy' => '1'
), 
'pa_bridge-size' => Array(
'name'=>'pa_bridge-size',
'value' => '1',
'is_visible' => '1',
'is_taxonomy' => '1'
), 
'pa_color' => Array(
'name' => 'pa_color',
'value' => 'green',
'is_visible' => '1',
'is_taxonomy' => '1',
'is_variation' => '1'
),
'pa_size' => Array(
'name' => 'pa_size',
'value' => '48',
'is_visible' => '1',
'is_taxonomy' => '1',
'is_variation' => '1'
)

);

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

对于它的工作,我需要在数组中包含现有的属性集(颜色和大小)。这样,当你运行update_post_meta()时,它就不会被覆盖。

最新更新