在WooCommerce中以编程方式添加产品标签或术语,但请保留现有的标签
我一直在阅读我想在WooCommerce上实现的此功能。这个想法是,一旦产品售罄,新的类别“售罄”将自动分配给它。
幸运的是,我遇到此答案代码。我添加了此代码:
function action_woocommerce_no_stock( $wc_get_product ) {
$term_ids = array(769);
$tag_ids = array(770);
// Set category ids
$wc_get_product->set_category_ids( $term_ids );
// Product set tag ids
$wc_get_product->set_tag_ids( $tag_ids );
// Save
$wc_get_product->save();
}
add_action( 'woocommerce_no_stock', 'action_woocommerce_no_stock', 10, 1 );
在我的functions.php中,它可以工作!问题在于它覆盖所有以前的类别。
如果有人可以指导我如何获取以前的“ product_cat”阵列,并将它们添加为“售罄”类别(769),我将非常感谢。
I've been reading about this functionality that I'd like to implement on my WooCommerce. The idea is that once a product is sold out, a new category "Sold Out" is automatically assigned to it.
Luckily, I've come across this answer code. I added this piece of code:
function action_woocommerce_no_stock( $wc_get_product ) {
$term_ids = array(769);
$tag_ids = array(770);
// Set category ids
$wc_get_product->set_category_ids( $term_ids );
// Product set tag ids
$wc_get_product->set_tag_ids( $tag_ids );
// Save
$wc_get_product->save();
}
add_action( 'woocommerce_no_stock', 'action_woocommerce_no_stock', 10, 1 );
To my functions.php and it works! the problem is that it overwrites all previous categories.
If anyone can guide me how to obtain the previous "product_cat" array and add them with the "Sold Out" category (769) I'd greatly appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以首先使用
get_category_ids()
或get_tag_ids()
,然后 array_merge(),因此您获得:
注意::在您的情况下
You can first use
get_category_ids()
ORget_tag_ids()
and thenarray_merge()
So you get:
Note: in your case
$wc_get_product
is equal to$product