WC_Product_Data_Store_CPT::update_visibility()protectedWC 3.0.0

Update visibility terms based on props.

Method of the class: WC_Product_Data_Store_CPT{}

Hooks from the method

Return

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() code WC 9.4.2

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 ( 'outofstock' === $product->get_stock_status() ) {
			$terms[] = 'outofstock';
		}

		$rating = min( 5, NumberUtil::round( $product->get_average_rating(), 0 ) );

		if ( $rating > 0 ) {
			$terms[] = 'rated-' . $rating;
		}

		switch ( $product->get_catalog_visibility() ) {
			case 'hidden':
				$terms[] = 'exclude-from-search';
				$terms[] = 'exclude-from-catalog';
				break;
			case 'catalog':
				$terms[] = 'exclude-from-search';
				break;
			case '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() );
		}
	}
}