phpsqlwoocommerceproduct

Updating product prices cache issue using a SQL query in Woocommerce


I have a SQL script that is supposed to synchronize and adjust regular price comparing the text document uploaded daily on server.

New prices are written to database, and I see them when looking at database. They show in _regular_price field properly. Problem is that frontend shows old prices .. until I manually just re-update each product in backend. Since i have thousands of products it is ineffective and tedious.

What am I missing?


Solution

  • Variable product prices are cached in wp_options table as transient…

    So you will need also to delete them via PhpMyAdmin with the following SQL query (for all variable products):

    DELETE FROM `wp_options` WHERE `wp_options`.`option_name` LIKE '_transient_timeout_wc_var_prices_%'
    DELETE FROM `wp_options` WHERE `wp_options`.`option_name` LIKE '_transient_wc_var_prices_%'
    

    Where 1234 (at the end) is the variable product ID.

    So programmatically (where $product_id is the dynamic variable product ID):

    global $wpdb;
    
    $wpdb->query( "
        DELETE FROM {$wpdb->prefix}options 
        WHERE {$wpdb->prefix}options.option_name LIKE '_transient_timeout_wc_var_prices_$product_id'
    " );
    
    $wpdb->query( "
        DELETE FROM {$wpdb->prefix}options 
        WHERE {$wpdb->prefix}options.option_name LIKE '_transient_wc_var_prices_$product_id'
    " );
    

    This will remove the targeted variable product cache…


    Other products (simple for example) not cached… When updating prices there is 2 cases:

    1. The product is on sale:
    1. The product is NOT on sale:

    So _price and _regular_price need always to be updated…