Public Ticket #3780121

Different prices for different Groups

Closed
Comments
7 Tickets
Hassan started the conversation 2 years ago on December 9, 2024 at 11:28am

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. 

2,762
KingsPlugins replied 2 years ago on December 9, 2024 at 7:13pm

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

7 Tickets
Hassan replied 2 years ago on December 10, 2024 at 5:25am

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.   

2,762
KingsPlugins replied 2 years ago on December 12, 2024 at 7:43pm

Hi again,

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,

 

Kind regards,

Stefan

7 Tickets
Hassan replied 2 years ago on December 23, 2024 at 2:16pm

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 .

if ( ! function_exists( 'woodmart_swatches_list' ) ) {    function woodmart_swatches_list( $attribute_name = false ) {       global $product;
       $id = $product->get_id();
       if ( empty( $id ) || ! woodmart_loop_prop( 'show_quick_shop', true ) || ! $product->is_type( 'variable' ) ) {          return false;       }
       if ( ! $attribute_name ) {          $attribute_name = woodmart_grid_swatches_attribute();       }
       if ( empty( $attribute_name ) && 'variation_form' !== woodmart_get_opt( 'quick_shop_variable_type', 'select_options' ) ) {          return false;       }
       // Swatches cache.       $cache          = apply_filters( 'woodmart_swatches_cache', true );       $transient_name = 'woodmart_swatches_cache_' . $id;
       if ( $cache ) {          $available_variations = get_transient( $transient_name );       } else {          $available_variations = array();       }
       if ( ! $available_variations ) {          $available_variations = $product->get_available_variations();          if ( $cache ) {             set_transient( $transient_name, $available_variations, apply_filters( 'woodmart_swatches_cache_time', WEEK_IN_SECONDS ) );          }       }
       if ( empty( $available_variations ) ) {          return false;       }
       if ( woodmart_get_opt( 'quick_shop_variable', true ) && 'variation_form' === woodmart_get_opt( 'quick_shop_variable_type', 'select_options' ) ) {          return woodmart_swatches_form_grid_template( $available_variations );       } else {          return woodmart_swatches_grid_template( $attribute_name, $available_variations );       }    }
}


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.

2,762
KingsPlugins replied 2 years ago on December 26, 2024 at 7:01pm

Hello Hassan,

Currently how are you disabling the caching mechanism? I would suggest to do it like this by setting it to -1:

add_filter( 'woodmart_swatches_cache_time', function() {
    return -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:

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.

Sign In