Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce 2.4 – Price cache workaround - Dynamic key
/**
* Alters the key used to store cached prices. This code snippet allows
* to use dynamic cache keys, thus making it possible to store multiple
* sets of prices in the cache.
* You can add this code to the functions.php file in your child theme
* folder, or to a custom plugin.
*
* @param array cache_key_args The arguments that form the cache key.
* @param WC_Product product The product for which the key is being generated.
* @param bool display Indicates if the prices are being retrieved for display
* purposes.
* @return array
* @since WooCommerce 2.4
* @author Aelia <support@aelia.co>
* @link http://aelia.co/2015/08/11/wc-2-4-workaround-for-price-cache
*/
function set_custom_price_cache_key($cache_key_args, $product, $display) {
// This is an example. You would have to implement your logic
// to determine if the customer is a wholesaler
$customer_is_wholesaler = my_custom_function();
// We add the "wholesaler" flag to the element that constitute the
// cache key. This will create two cached copies of the product prices:
// - One price list for wholesaler
// - One price list for non-wholesaler
// The appropriate copy will be retrieved automatically.
$cache_key_args[] = (string)$customer_is_wholesaler;
return $cache_key_args;
}
add_filter('woocommerce_get_variation_prices_hash', 'set_custom_price_cache_key', 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment