保存时覆盖 Woocommerce 产品属性
当用户保存新产品或更新现有产品时,我试图更改属性。如果产品属于某个类别,则“权重”应为 N/A..
这是我的产品,因为它应该启动,这很好。现在我想向其添加一个权重字段(它已经作为属性存在)并将权重分配给 N/A。这是我想以编程方式完成的最终结果。
据我所知:
add_action( 'woocommerce_update_product', 'mp_sync_on_product_save', 10, 1 );
function mp_sync_on_product_save( $product_id ) {
$product = wc_get_product( $product_id );
if($product->category_ids[0] == 18) {
//We are jewellery
$post_id = $product->get_id();
$product_attr = get_post_meta($post_id, '_product_attributes',true);//get the whole product attributes first
$product_attr['pa_weight'] = array(
'name' => 'pa_weight',
'value' => 'N/A',
'position' => 1,
'is_visible' => 1,
'is_variation' => 1,
'is_taxonomy' => '1'
);
update_post_meta($post_id , '_product_attributes',$product_attr);
}
}
目前这里似乎什么也没有发生。我希望我已经很接近了,所以任何帮助解决这个问题的帮助都会很棒。 谢谢
I am trying to alter an attribute when the user saves a new product or updates an existing. If the product is a certain category the 'weight' should be N/A..
Here is my product as it should start which is fine. Now I want to add a weight field to it (Which already exists as an attribute) and assign the weight to N/A. Here's the end result I want to do programmatically.
Here is as far as I can get:
add_action( 'woocommerce_update_product', 'mp_sync_on_product_save', 10, 1 );
function mp_sync_on_product_save( $product_id ) {
$product = wc_get_product( $product_id );
if($product->category_ids[0] == 18) {
//We are jewellery
$post_id = $product->get_id();
$product_attr = get_post_meta($post_id, '_product_attributes',true);//get the whole product attributes first
$product_attr['pa_weight'] = array(
'name' => 'pa_weight',
'value' => 'N/A',
'position' => 1,
'is_visible' => 1,
'is_variation' => 1,
'is_taxonomy' => '1'
);
update_post_meta($post_id , '_product_attributes',$product_attr);
}
}
Currently nothing seems to happen here. I hope I'm close so any help to get this over the line would be awesome.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论