• Resolved Dheeraj Singh

    (@dheeraj-singh)


    Hi,

    We are using a custom function to update the total_sales every time the product is viewed. When the cache is on the value of total_sales does not update. Wondering if it is possible to update the total_sales post_meta in the below code even when the cache is switched on.

    add_action( 'woocommerce_before_single_product', 'prefix_save_product_views' );
    function prefix_save_product_views(  ) {
    
        $product_id = get_the_ID();
        $increment = 1;
        $current_visit_count = get_post_meta( $product_id, 'total_sales', true );
    
        $total_visit_count = (int)$current_visit_count + $increment;
        update_post_meta( $product_id, 'total_sales', $total_visit_count );
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support qtwrk

    (@qtwrk)

    I assume you meant every time the product is SOLD , instead of “viewed” ?

    if (defined('LSCWP_V')){
    do_action( 'litespeed_purge_post', $product_id );
    }

    add this to your function ought do it.

    Thread Starter Dheeraj Singh

    (@dheeraj-singh)

    Thanks.

    I indeed meant every time a product is viewed. I am using the code i pasted to save product views in the total_sales post meta every time a product is viewed.

    Will the above code you provided still work and Would Litespeed cache continue to cache product pages after that.

    Plugin Support qtwrk

    (@qtwrk)

    then you should make it to be ajax call , since ajax call will not be cached , and you can load it via ajax call on cached page as well

    otherwise you will need to implement ESI block , which is bit of more complicated than ajax call

    please check this page

    Thread Starter Dheeraj Singh

    (@dheeraj-singh)

    Ok. I will try and figure it out.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom function for post views’ is closed to new replies.