skip to Main Content

I have a price update on all products to sync the shop prices with a web service XML. This is the part that updates the price. It’s run with a server cronjob every hour. The problem is that, although I can see the prices updated, in wp-admin edit product pages, in the admin product list, and in the front end it still shows the old prices.

$product = new WC_Product( $pid );
update_post_meta($pid, '_regular_price', $set_price);
update_post_meta($pid, '_price', $set_price);
$product->set_regular_price($set_price);
$product->set_price($set_price);
$product->save();

What am I doing wrong here?

Just an update something i just notice, for example on products that have the the sale_price set, on the admin edit page its all ok but on admin list product page and frontend the sale price shows the regular_price.

After more digging it seems that the issue maybe related to the save instruction. All the fields are populated correctly with the changes, so if i go to edit product page and add a change, i.e. add a 0 in regular price and then save it assumes the changes made.

Is there a way that i can see the query executed by wordpress/woocomerce when pressing the save button on a edit product page?

2

Answers


  1. Yeah, sounds like a Cache problem…

    Clear Cache in Your WordPress Caching Plugin

    If you are using a WordPress caching plugin on your site, then you need to clear your plugin cache. Most caching plugins allow you to easily do that from the plugin’s Settings page.

    Clear Cache in WP Rocket

    WP Rocket is the best WordPress caching plugin on the market. The best part about using WP Rocket is that it proactively builds your WordPress cache in the background, so your users are always served a cached version.

    It also makes it very easy to clear caches files with a single click.

    Simply go to the Settings » WP Rocket page and click on the ‘Clear Cache’ button under the dashboard tab.

    enter image description here

    WP Rocket will now clear all cached files for you.

    Login or Signup to reply.
  2. Yes, it is a cache problem for WooCommerce when you programmatically changed the product prices.

    Not sure you have solved it or not, I have the same issue as your one, I update the price programmatically with WC hooks but the price was not updated in the related products slider.

    I solve the issue by adding following code to my theme:

    // Handling price caching
    add_filter( 'woocommerce_get_variation_prices_hash', 'add_price_multiplier_to_variation_prices_hash', 99, 3 );
    function add_price_multiplier_to_variation_prices_hash( $price_hash, $product, $for_display ) {
        $price_hash[] = get_price_multiplier();
        return $price_hash;
    }
    

    all credits belong to this post, you can get more information on why use that.

    Hope it helps anyone who runs into the same issue. Have a good day!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search