WC_Product_Variable_Data_Store_CPT::get_price_hash
Create unique cache key based on the tax location (affects displayed/cached prices), product version and active price filters. DEVELOPERS should filter this hash if offering conditional pricing to keep it unique.
Метод класса: WC_Product_Variable_Data_Store_CPT{}
Хуки из метода
Возвращает
Строку.
Использование
// protected - в коде основоного (родительского) или дочернего класса $result = $this->get_price_hash( $product, $for_display );
- $product(WC_Product) (обязательный) (передается по ссылке — &)
- Product object.
- $for_display(true|false)
- If taxes should be calculated or not.
По умолчанию:false
Список изменений
| С версии 3.0.0 | Введена. |
Код WC_Product_Variable_Data_Store_CPT::get_price_hash() WC Product Variable Data Store CPT::get price hash WC 10.5.2
protected function get_price_hash( &$product, $for_display = false ) {
$price_hash = array( false );
if ( $for_display && wc_tax_enabled() ) {
$price_hash = array(
get_option( 'woocommerce_tax_display_shop', 'excl' ),
WC_Tax::get_rates(),
empty( WC()->customer ) ? false : WC()->customer->is_vat_exempt(),
);
}
$filter_names = array( 'woocommerce_variation_prices_price', 'woocommerce_variation_prices_regular_price', 'woocommerce_variation_prices_sale_price' );
/**
* Filters whether to use the legacy callback serialization algorithm.
*
* By default, WooCommerce will use the legacy algorithm to get the callback signatures
* for variation price hash calculation. This algorithm serializes the entire callback
* array as it comes from $wp_filter, which means that for callbacks that are class methods
* the entire object will be serialized, including the current values of the class variables.
* This implies that a change in these variables will change the price hash,
* even if they do not affect the price calculation.
*
* This filter allows using CallbackUtil instead, which generates a more stable signature
* that does not depend on the internal state of objects, but only on the method names and
* class names. This results in a more consistent and reliable price hash, reducing unnecessary
* cache misses; but can cause compatibility issues with plugins that rely on the legacy behavior.
*
* IMPORTANT: see also the documentation for the 'woocommerce_variation_prices_price' filter.
*
* @since 10.5.0
*
* @param bool $use_legacy True to use the legacy algorithm (default), false to use CallbackUtil
* @param WC_Product $product The product object.
* @param bool $for_display If taxes should be calculated or not.
*/
$use_legacy_algorithm = apply_filters( 'woocommerce_use_legacy_get_variations_price_hash', true, $product, $for_display );
if ( $use_legacy_algorithm ) {
global $wp_filter;
foreach ( $filter_names as $filter_name ) {
if ( ! empty( $wp_filter[ $filter_name ] ) ) {
$price_hash[ $filter_name ] = array();
foreach ( $wp_filter[ $filter_name ] as $priority => $callbacks ) {
$price_hash[ $filter_name ][] = array_values( wp_list_pluck( $callbacks, 'function' ) );
}
}
}
} else {
foreach ( $filter_names as $filter_name ) {
$signatures = CallbackUtil::get_hook_callback_signatures( $filter_name );
if ( ! empty( $signatures ) ) {
$price_hash[ $filter_name ] = $signatures;
}
}
}
/**
* Filters the hash used for caching variation prices.
*
* IMPORTANT: see the documentation for the 'woocommerce_variation_prices_price' filter.
*
* @since 2.5.0
*
* @param array $price_hash Array of factors used to generate the cache key hash.
* @param WC_Product $product The variable product object.
* @param bool $for_display Whether prices are for display (with tax adjustments) or calculations.
*/
$price_hash = apply_filters( 'woocommerce_get_variation_prices_hash', $price_hash, $product, $for_display );
return md5( wp_json_encode( $price_hash ) );
}