woocommerce_use_legacy_get_variations_price_hash filter-hookWC 10.5.0

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. That algorithm includes the callback array as it comes from $wp_filter in the hashed data, which is then JSON encoded. For callbacks that are class methods, JSON encoding captures the object's PUBLIC property values only; private and protected properties are not captured. Note that dynamically created properties are public, and that a class implementing JsonSerializable controls what is captured through its jsonSerialize() method. A change in any captured value will change the price hash, even if it does 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

Usage

add_filter( 'woocommerce_use_legacy_get_variations_price_hash', 'wp_kama_woocommerce_use_legacy_get_variations_price_hash_filter', 10, 3 );

/**
 * Function for `woocommerce_use_legacy_get_variations_price_hash` filter-hook.
 * 
 * @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.
 *
 * @return bool
 */
function wp_kama_woocommerce_use_legacy_get_variations_price_hash_filter( $use_legacy, $product, $for_display ){
	// filter...
	return $use_legacy;
}
$use_legacy(true|false)
True to use the legacy algorithm (default), false to use CallbackUtil
$product(WC_Product)
The product object.
$for_display(true|false)
If taxes should be calculated or not.

Changelog

Since 10.5.0 Introduced.

Where the hook is called

WC_Product_Variable_Data_Store_CPT::get_price_hash()
woocommerce_use_legacy_get_variations_price_hash
woocommerce/includes/data-stores/class-wc-product-variable-data-store-cpt.php 685
$use_legacy_algorithm = apply_filters( 'woocommerce_use_legacy_get_variations_price_hash', true, $product, $for_display );

Where the hook is used in WooCommerce

Usage not found.