WC_Product_Data_Store_CPT::handle_updated_props()protectedWC 3.0.0

Handle updated meta props after updating meta data.

Метод класса: WC_Product_Data_Store_CPT{}

Возвращает

null. Ничего (null).

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->handle_updated_props( $product );
$product(WC_Product) (обязательный) (передается по ссылке — &)
Product Object.

Список изменений

С версии 3.0.0 Введена.

Код WC_Product_Data_Store_CPT::handle_updated_props() WC 8.9.3

protected function handle_updated_props( &$product ) {
	$price_is_synced = $product->is_type( array( 'variable', 'grouped' ) );

	if ( ! $price_is_synced ) {
		if ( in_array( 'regular_price', $this->updated_props, true ) || in_array( 'sale_price', $this->updated_props, true ) ) {
			if ( $product->get_sale_price( 'edit' ) >= $product->get_regular_price( 'edit' ) ) {
				update_post_meta( $product->get_id(), '_sale_price', '' );
				$product->set_sale_price( '' );
			}
		}

		/**
		 * It's tempting to add a check for `_price` in the updated props, so that when `wc_scheduled_sales` is called, we don't have to rely on `date_on_sale_from` being present in the list of updated props.
		 *
		 * However, it has a side effect of overriding the `_price` meta value with the `_sale_price` meta value when product is in sale, or with `_regular_price` meta value when product is not in sale. This is not desirable, because `_price` can also be set as a temporary active price for a product, and we don't want to override it.
		 *
		 * If we want to preserve previous sales schedules, a better way would be to store them in dedicated meta keys as logs.
		 */
		$product_price_props = array( 'date_on_sale_from', 'date_on_sale_to', 'regular_price', 'sale_price', 'product_type' );
		if ( count( array_intersect( $product_price_props, $this->updated_props ) ) > 0 ) {
			if ( $product->is_on_sale( 'edit' ) ) {
				update_post_meta( $product->get_id(), '_price', $product->get_sale_price( 'edit' ) );
				$product->set_price( $product->get_sale_price( 'edit' ) );
			} else {
				update_post_meta( $product->get_id(), '_price', $product->get_regular_price( 'edit' ) );
				$product->set_price( $product->get_regular_price( 'edit' ) );
			}
		}
	}

	if ( in_array( 'stock_quantity', $this->updated_props, true ) ) {
		if ( $product->is_type( 'variation' ) ) {
			do_action( 'woocommerce_variation_set_stock', $product );
		} else {
			do_action( 'woocommerce_product_set_stock', $product );
		}
	}

	if ( in_array( 'stock_status', $this->updated_props, true ) ) {
		if ( $product->is_type( 'variation' ) ) {
			do_action( 'woocommerce_variation_set_stock_status', $product->get_id(), $product->get_stock_status(), $product );
		} else {
			do_action( 'woocommerce_product_set_stock_status', $product->get_id(), $product->get_stock_status(), $product );
		}
	}

	if ( array_intersect( $this->updated_props, array( 'sku', 'regular_price', 'sale_price', 'date_on_sale_from', 'date_on_sale_to', 'total_sales', 'average_rating', 'stock_quantity', 'stock_status', 'manage_stock', 'downloadable', 'virtual', 'tax_status', 'tax_class' ) ) ) {
		$this->update_lookup_table( $product->get_id(), 'wc_product_meta_lookup' );
	}

	// Trigger action so 3rd parties can deal with updated props.
	do_action( 'woocommerce_product_object_updated_props', $product, $this->updated_props );

	// After handling, we can reset the props array.
	$this->updated_props = array();
}