在 Woocommerce 3+ 上通过 PHP 更改产品可见性 [英] Change product visibility via PHP on Woocommerce 3+
问题描述
我的商店有一项功能可以让我自动生成产品报价,根据报价的属性、价格和运输规则,我可以即时创建新产品.
I have a feature on my store that allows me to auto-generate a product quote, after the quote's attributes, prices, and shipping rules, I create a new product on the fly.
我不希望这些产品显示在我的目录或搜索中,所以我一直在想办法将目录可见性"更新为隐藏.
I don't want these products to show on my catalogue or searches, so I've been trying to figure out how to update the "Catalog Visibility" to Hidden.
起初我在尝试:
update_post_meta( $prod_ID, '_visibility', 'hidden' );
添加了元数据,但是,查看它我发现 WooCommerce 改变了它指定产品是可见还是隐藏的方式:: 下面来自 WooCommerce
The meta is added, however, looking into it I found out that WooCommerce changed the way it specifies if a product is visible or hidden:: below is from WooCommerce
产品可见性是基于分类法而不是基于元数据3.0.0 引入了新的产品可见性分类法;目录、搜索、隐藏、精选、缺货都是术语.这些设置用于在前端查询期间升级和帮助过滤目录中的产品.
Product visibility is taxonomy based instead of meta based 3.0.0 introduces a new product visibility taxonomy; catalogue, search, hidden, featured, out of stock are terms. These are set to upgrade and help filter products in the catalogue during frontend queries.
我的问题是;如何通过 PHP 修改分类法.
My issue is; how can I modify a taxonomy via PHP.
推荐答案
应该是这样的:
$terms = array( 'exclude-from-search', 'exclude-from-catalog' ); // for hidden..
wp_set_post_terms( $prod_ID, $terms, 'product_visibility', false );
你可以在这里得到这个想法......woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php#L671
you can get the idea here... woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php#L671
这篇关于在 Woocommerce 3+ 上通过 PHP 更改产品可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!