Support » Plugin: LiteSpeed Cache » Woocommerce product update interval

  • Resolved trmkr

    (@trmkr)


    Hello,

    Is there any option to purge product and category pages for product prices updates as quantity updates on Cache > Woocommerce > Product Update Interval settings?

    Thank you!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Support qtwrk

    (@qtwrk)

    How exactly did you update the price ?

    Thread Starter trmkr

    (@trmkr)

    Hello,

    The price and quantities are updated with REST API. Therefore, page cache is not updated during price revisions.

    Regards

    Plugin Support qtwrk

    (@qtwrk)

    please try add this to theme’s functions.php

    
    
    add_action('woocommerce_update_product', 'lscwp_purge_product_update', 10, 2);
    function lscwp__purge_product_update($product_id, $product) {
    if (defined('LSCWP_V')){
    do_action( 'litespeed_purge_post', $product_id );
    }
    }
    
    • This reply was modified 2 months, 3 weeks ago by qtwrk.
    Thread Starter trmkr

    (@trmkr)

    Thank you! It works šŸ™‚

    There is a typo in the function name. Also, can I purge category pages of that product?

    Regards

    Plugin Support qtwrk

    (@qtwrk)

    oops , didn’t notice

    yes , but it will be bit of complicated

    you need to get the category name/id based on product id , then purge it

    add this into function

    $lscwp_categories = wp_get_post_categories( $product_id , array( 'fields' => 'all' ) );
    
     
    if( $lscwp_categories ){ 
        foreach($lscwp_categories as $lscwp_c){
            if (defined('LSCWP_V')) {
          do_action( 'litespeed_purge_url', get_category_link( $lscwp_c ->term_id ) );
        }   
      }
    }
    }

    didn’t try , but I think it should works …

    • This reply was modified 2 months, 3 weeks ago by qtwrk.
    Thread Starter trmkr

    (@trmkr)

    Thank you! Unfortunately, the category cache is not updated. My final code is:

    add_action('woocommerce_update_product', 'lscwp_purge_product_update', 10, 2);
    function lscwp_purge_product_update($product_id, $product) {
    if (defined('LSCWP_V')){
    do_action( 'litespeed_purge_post', $product_id );
    }
    $lscwp_categories = wp_get_post_categories( $product_id , array( 'fields' => 'all' ) );
    $lscwp_cats = array();
    if( $lscwp_categories ){ 
        foreach($lscwp_categories as $lscwp_c){
            if (defined('LSCWP_V')) {
          do_action( 'litespeed_purge_url', get_category_link( $lscwp_c ->term_id ) );
        }   
      }
    }
    }

    I couldn’t understand function of lscwp_cats variable above.

    Plugin Support qtwrk

    (@qtwrk)

    it was a code snippet I grabbed from my handbook for something else,

    I guess I didn’t modify it properly.

    The idea behind the scene is :

    Use product ID to retrieve the category IDs

    Since one product can linked to multiple categories, so you need form an array and loop through them,

    Then use category ID to obtain the URI, then fire the purge URI

    • This reply was modified 2 months, 3 weeks ago by qtwrk.
    Thread Starter trmkr

    (@trmkr)

    I modified the code below. It works šŸ™‚

    add_action('woocommerce_update_product', 'lscwp_purge_product_update', 10, 2);
    function lscwp_purge_product_update($product_id, $product) {
    if (defined('LSCWP_V')){
    do_action( 'litespeed_purge_post', $product_id );
    }
    $lscwp_categories = get_the_terms($product_id, 'product_cat');   
    if( $lscwp_categories ){ 
        foreach($lscwp_categories as $lscwp_c){
            if (defined('LSCWP_V')) {
          		do_action( 'litespeed_purge_url', get_category_link( $lscwp_c ->term_id ) );
        	}   
      	}
    }
    }
Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.