I have set different prices for different B2B groups , and prices are reflected accordingly. The issue I am facing is when I update the user group then on the front end prices are changed according to new group except on the product listing page. On this page I am showing range of prices for variable product. when I select the one variation its price is reflected but it is the older one, i.e. price of variation that is previously assigned to the user.
I don't know if it is cached somewhere and when I update the user group this specific request still gets the older price for the specific group, and if I update the product from admin panel then this issue is resolved.
Indeed I think it is because of caches. Especially for variable products, the price ranges are cached and sometimes it takes more time to update than with a simple product.
Generally I think it is not something to be concerned about. If you add the product to cart, the price should always be correct, but the price range can be temporarily wrong, so I think it is just a temporary display issue.
If you are looking to clear those caches I think one way to do it is to create / activate a dynamic rule discount % in B2BKing and then deactivate it.
Can I have any code snippet so that when group of the user is changed it clears the cache because the issue is not occurring for temporary time and is not fixed automatically, and for the front end user it shows the wrong price for the variation. And its a bit confusing for the user if listing shows different prices but when add to cart these are different.
I tried writing a code snippet that clears all product price caches when a user's group is updated:
add_action('updated_user_meta', 'clear_b2bking_related_caches', 10, 4);
add_action('added_user_meta', 'clear_b2bking_related_caches', 10, 4);
function clear_b2bking_related_caches($meta_id, $user_id, $meta_key, $_meta_value) {
// Target specific B2BKing meta keys
$watched_meta_keys = array(
'b2bking_b2buser',
'b2bking_customergroup'
);
// Check if the updated meta key is one we're watching
if (in_array($meta_key, $watched_meta_keys)) {
// Clear product transient version
WC_Cache_Helper::get_transient_version('product', true);
// Update B2BKing dynamic rules flag
update_option('b2bking_dynamic_rules_have_changed', 'yes');
// Add randomization to variation prices hash
add_filter('woocommerce_get_variation_prices_hash', 'randomize_variation_prices_hash', 99);
}
}
function randomize_variation_prices_hash($price_hash) {
// Add random element to force cache refresh
$price_hash[] = mt_rand();
return $price_hash;
}
Please try adding this to functions.php or to any code snippets plugin.
Let me know if you see any further issues after adding it,
I want to re-open this issue as I have some progress over it, actually I am using Woodmart theme and it has its own cache mechanism that it caches the swatches data on each product refresh and then show that data to every user irrespective of their B2B King group its in swatches.php,
So what I have done is stop making of cache so that fresh prices are shown and it works I am sharing the example code of a method, you can check the method set_tansient that was the issue in my case .
The only issue I am facing now is when 2 users refresh there page simultaneously it then fetch the prices of a group two both users , if the one with the wrong prices refresh again the prices are listed correctly again. I am not sure if there is another cache mechanism that is time base or what ? will you be able to guide/assist me in this matter.
I am not aware of any other specific cache mechanism.
Something else that comes to mind:
I think the following code may introduce performance issues, but it would be useful to know if it can solve the issue or not:
function randomize_variation_prices_hash2($price_hash) {
// Add random element to force cache refresh
$price_hash[] = mt_rand();
return $price_hash;
}
add_action('init', function() {
WC_Cache_Helper::get_transient_version('product', true);
});
add_filter('woocommerce_get_variation_prices_hash', 'randomize_variation_prices_hash2', 99);
Basically this should clear the Woo variations cache on each page load (instead of just when the user is saved), so it would be helpful to know if it helps or not.
Hi,
I have set different prices for different B2B groups , and prices are reflected accordingly. The issue I am facing is when I update the user group then on the front end prices are changed according to new group except on the product listing page.
On this page I am showing range of prices for variable product. when I select the one variation its price is reflected but it is the older one, i.e. price of variation that is previously assigned to the user.
I don't know if it is cached somewhere and when I update the user group this specific request still gets the older price for the specific group, and if I update the product from admin panel then this issue is resolved.
Hello Hassan,
Indeed I think it is because of caches. Especially for variable products, the price ranges are cached and sometimes it takes more time to update than with a simple product.
Generally I think it is not something to be concerned about. If you add the product to cart, the price should always be correct, but the price range can be temporarily wrong, so I think it is just a temporary display issue.
If you are looking to clear those caches I think one way to do it is to create / activate a dynamic rule discount % in B2BKing and then deactivate it.
Kind regards,
Stefan
Can I have any code snippet so that when group of the user is changed it clears the cache because the issue is not occurring for temporary time and is not fixed automatically, and for the front end user it shows the wrong price for the variation. And its a bit confusing for the user if listing shows different prices but when add to cart these are different.
Hi again,
I tried writing a code snippet that clears all product price caches when a user's group is updated:
Please try adding this to functions.php or to any code snippets plugin.
Let me know if you see any further issues after adding it,
Kind regards,
Stefan
Hi Stefan,
I want to re-open this issue as I have some progress over it, actually I am using Woodmart theme and it has its own cache mechanism that it caches the swatches data on each product refresh and then show that data to every user irrespective of their B2B King group its in swatches.php,
So what I have done is stop making of cache so that fresh prices are shown and it works I am sharing the example code of a method, you can check the method set_tansient that was the issue in my case .
The only issue I am facing now is when 2 users refresh there page simultaneously it then fetch the prices of a group two both users , if the one with the wrong prices refresh again the prices are listed correctly again. I am not sure if there is another cache mechanism that is time base or what ? will you be able to guide/assist me in this matter.
Hello Hassan,
Currently how are you disabling the caching mechanism? I would suggest to do it like this by setting it to -1:
I am not aware of any other specific cache mechanism.
Something else that comes to mind:
I think the following code may introduce performance issues, but it would be useful to know if it can solve the issue or not:
Basically this should clear the Woo variations cache on each page load (instead of just when the user is saved), so it would be helpful to know if it helps or not.