WC_Product_Data_Store_CPT::update_visibility
Update visibility terms based on props.
Method of the class: WC_Product_Data_Store_CPT{}
Hooks from the method
Returns
null. Nothing (null).
Usage
// protected - for code of main (parent) or child class $result = $this->update_visibility( $product, $force );
- $product(WC_Product) (required) (passed by reference — &)
- Product object.
- $force(true|false)
- Force update. Used during create.
Default: false
Changelog
| Since 3.0.0 | Introduced. |
WC_Product_Data_Store_CPT::update_visibility() WC Product Data Store CPT::update visibility code WC 10.3.6
protected function update_visibility( &$product, $force = false ) {
$changes = $product->get_changes();
if ( $force || array_intersect( array( 'featured', 'stock_status', 'average_rating', 'catalog_visibility' ), array_keys( $changes ) ) ) {
$terms = array();
if ( $product->get_featured() ) {
$terms[] = 'featured';
}
if ( ProductStockStatus::OUT_OF_STOCK === $product->get_stock_status() ) {
$terms[] = ProductStockStatus::OUT_OF_STOCK;
}
$rating = min( 5, NumberUtil::round( $product->get_average_rating(), 0 ) );
if ( $rating > 0 ) {
$terms[] = 'rated-' . $rating;
}
switch ( $product->get_catalog_visibility() ) {
case CatalogVisibility::HIDDEN:
$terms[] = 'exclude-from-search';
$terms[] = 'exclude-from-catalog';
break;
case CatalogVisibility::CATALOG:
$terms[] = 'exclude-from-search';
break;
case CatalogVisibility::SEARCH:
$terms[] = 'exclude-from-catalog';
break;
}
if ( ! is_wp_error( wp_set_post_terms( $product->get_id(), $terms, 'product_visibility', false ) ) ) {
do_action( 'woocommerce_product_set_visibility', $product->get_id(), $product->get_catalog_visibility() );
}
}
}