WC_Product_Variable_Data_Store_CPT::read_price_data()publicWC 3.0.0

Get an array of all sale and regular prices from all variations. This is used for example when displaying the price range at variable product level or seeing if the variable product is on sale.

Can be filtered by plugins which modify costs, but otherwise will include the raw meta costs unlike get_price() which runs costs through the woocommerce_get_price filter. This is to ensure modified prices are not cached, unless intended.

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

Возвращает

Массив. of prices

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

$WC_Product_Variable_Data_Store_CPT = new WC_Product_Variable_Data_Store_CPT();
$WC_Product_Variable_Data_Store_CPT->read_price_data( $product, $for_display );
$product(WC_Product) (обязательный) (передается по ссылке — &)
Product object.
$for_display(true|false)
If true, prices will be adapted for display based on the woocommerce_tax_display_shop setting (including or excluding taxes).
По умолчанию: false

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

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

Код WC_Product_Variable_Data_Store_CPT::read_price_data() WC 8.7.0

public function read_price_data( &$product, $for_display = false ) {

	/**
	 * Transient name for storing prices for this product (note: Max transient length is 45)
	 *
	 * @since 2.5.0 a single transient is used per product for all prices, rather than many transients per product.
	 */
	$transient_name    = 'wc_var_prices_' . $product->get_id();
	$transient_version = WC_Cache_Helper::get_transient_version( 'product' );
	$price_hash        = $this->get_price_hash( $product, $for_display );

	// Check if prices array is stale.
	if ( ! isset( $this->prices_array['version'] ) || $this->prices_array['version'] !== $transient_version ) {
		$this->prices_array = array(
			'version' => $transient_version,
		);
	}

	/**
	 * $this->prices_array is an array of values which may have been modified from what is stored in transients - this may not match $transient_cached_prices_array.
	 * If the value has already been generated, we don't need to grab the values again so just return them. They are already filtered.
	 */
	if ( empty( $this->prices_array[ $price_hash ] ) ) {
		$transient_cached_prices_array = array_filter( (array) json_decode( strval( get_transient( $transient_name ) ), true ) );

		// If the product version has changed since the transient was last saved, reset the transient cache.
		if ( ! isset( $transient_cached_prices_array['version'] ) || $transient_version !== $transient_cached_prices_array['version'] ) {
			$transient_cached_prices_array = array(
				'version' => $transient_version,
			);
		}

		// If the prices are not stored for this hash, generate them and add to the transient.
		if ( empty( $transient_cached_prices_array[ $price_hash ] ) ) {
			$prices_array = array(
				'price'         => array(),
				'regular_price' => array(),
				'sale_price'    => array(),
			);

			$variation_ids = $product->get_visible_children();

			if ( is_callable( '_prime_post_caches' ) ) {
				_prime_post_caches( $variation_ids );
			}

			foreach ( $variation_ids as $variation_id ) {
				$variation = wc_get_product( $variation_id );

				if ( $variation ) {
					$price         = apply_filters( 'woocommerce_variation_prices_price', $variation->get_price( 'edit' ), $variation, $product );
					$regular_price = apply_filters( 'woocommerce_variation_prices_regular_price', $variation->get_regular_price( 'edit' ), $variation, $product );
					$sale_price    = apply_filters( 'woocommerce_variation_prices_sale_price', $variation->get_sale_price( 'edit' ), $variation, $product );

					// Skip empty prices.
					if ( '' === $price ) {
						continue;
					}

					// If sale price does not equal price, the product is not yet on sale.
					if ( $sale_price === $regular_price || $sale_price !== $price ) {
						$sale_price = $regular_price;
					}

					// If we are getting prices for display, we need to account for taxes.
					if ( $for_display ) {
						if ( 'incl' === get_option( 'woocommerce_tax_display_shop' ) ) {
							$price         = '' === $price ? '' : wc_get_price_including_tax(
								$variation,
								array(
									'qty'   => 1,
									'price' => $price,
								)
							);
							$regular_price = '' === $regular_price ? '' : wc_get_price_including_tax(
								$variation,
								array(
									'qty'   => 1,
									'price' => $regular_price,
								)
							);
							$sale_price    = '' === $sale_price ? '' : wc_get_price_including_tax(
								$variation,
								array(
									'qty'   => 1,
									'price' => $sale_price,
								)
							);
						} else {
							$price         = '' === $price ? '' : wc_get_price_excluding_tax(
								$variation,
								array(
									'qty'   => 1,
									'price' => $price,
								)
							);
							$regular_price = '' === $regular_price ? '' : wc_get_price_excluding_tax(
								$variation,
								array(
									'qty'   => 1,
									'price' => $regular_price,
								)
							);
							$sale_price    = '' === $sale_price ? '' : wc_get_price_excluding_tax(
								$variation,
								array(
									'qty'   => 1,
									'price' => $sale_price,
								)
							);
						}
					}

					$prices_array['price'][ $variation_id ]         = wc_format_decimal( $price, wc_get_price_decimals() );
					$prices_array['regular_price'][ $variation_id ] = wc_format_decimal( $regular_price, wc_get_price_decimals() );
					$prices_array['sale_price'][ $variation_id ]    = wc_format_decimal( $sale_price, wc_get_price_decimals() );

					$prices_array = apply_filters( 'woocommerce_variation_prices_array', $prices_array, $variation, $for_display );
				}
			}

			// Add all pricing data to the transient array.
			foreach ( $prices_array as $key => $values ) {
				$transient_cached_prices_array[ $price_hash ][ $key ] = $values;
			}

			set_transient( $transient_name, wp_json_encode( $transient_cached_prices_array ), DAY_IN_SECONDS * 30 );
		}

		/**
		 * Give plugins one last chance to filter the variation prices array which has been generated and store locally to the class.
		 * This value may differ from the transient cache. It is filtered once before storing locally.
		 */
		$this->prices_array[ $price_hash ] = apply_filters( 'woocommerce_variation_prices', $transient_cached_prices_array[ $price_hash ], $product, $for_display );
	}
	return $this->prices_array[ $price_hash ];
}