woocommerce_use_legacy_get_variations_price_hash
Filters whether to use the legacy callback serialization algorithm.
The legacy algorithm for variation price hash calculation 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_algorithm True to use the legacy algorithm, 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_algorithm, $product, $for_display ){
// filter...
return $use_legacy_algorithm;
}
- $use_legacy_algorithm(true|false)
- True to use the legacy algorithm, 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. |
| Since 10.5.0 | the hook is introduced and all stores use the legacy algorith by default. |
| Since 11.3.0 | the new stores use optimized algorithm by default. |
Where the hook is called
$use_legacy_algorithm = (bool) apply_filters( 'woocommerce_use_legacy_get_variations_price_hash', $use_legacy_algorithm, $product, $for_display );