Set Catalog visibility hidden woo-commerce

2019-06-05 20:36发布

How to set Catalog visibility hidden in woo-commerce WordPress programmatically?

Like its mentioned here :

https://docs.woothemes.com/document/catalog-visibility-options/

But i can't find any hook or hack, that how to do it in PHP.

标签: wordpress
2条回答
放荡不羁爱自由
2楼-- · 2019-06-05 21:14

I have tried doing this for some days, and there is nothing about it online so I read the woocommerce documentation and discovered that in woocommerce 3.x.x the visibility is a taxonomy called "product_visibility".

To achieve that you should set taxonomy terms, for example:

//Set product hidden: 
$terms = array( 'exclude-from-catalog', 'exclude-from-search' );
wp_set_object_terms( $post_id, $terms, 'product_visibility' );

//Set product visible in catalog:
$terms = 'exclude-from-search';
wp_set_object_terms( $post_id, $terms, 'product_visibility' );

//Set product visible in search:
$terms = 'exclude-from-catalog';
wp_set_object_terms( $post_id, $terms, 'product_visibility' );

All possible taxonomy terms:

"exclude-from-catalog"
"exclude-from-search"
"featured"
"outofstock"
查看更多
We Are One
3楼-- · 2019-06-05 21:21

The visibility is set in the custom field _visibility. You can change it with update_post_meta():

update_post_meta( $product_id, '_visibility', '_visibility_hidden' );

Possible values:

  • visible (Catalog & Search)
  • catalog (Catalog only)
  • search (Search only)
  • hidden (nowhere)
查看更多
登录 后发表回答