0

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?

Viktor Borítás
  • 135
  • 2
  • 11
Wed
  • 323
  • 1
  • 9
  • 20
  • 1
    Variable product prices are cached in woocommerce – LoicTheAztec Feb 19 '18 at 07:48
  • @LoicTheAztec how would you solve it programmatically? – Wed Feb 19 '18 at 07:54
  • you need to delete price cashing `_transient_timeout_wc_var_prices_` and `_transient_wc_var_prices_` check this answer last part https://stackoverflow.com/a/41548895/5019802 – Raunak Gupta Feb 19 '18 at 08:11
  • @RaunakGupta What I see when looking at database is that mostly single products are affected by this non-change in price and variable products seem ok. – Wed Feb 19 '18 at 08:43
  • Correction: After closer look I see that there are simple products and variable products that are affected by this non-changed price. – Wed Feb 19 '18 at 08:47

1 Answers1

3

Variable product prices are cached in wp_options table as transient…

So you will need also to delete through SQL using for each variable product ID something like this:

DELETE FROM `wp_options` WHERE `wp_options`.`option_name` LIKE '_transient_timeout_wc_var_prices_1234'
DELETE FROM `wp_options` WHERE `wp_options`.`option_name` LIKE '_transient_wc_var_prices_1234'

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:

  • _price and _sale_price will have the discounted product price.
  • _regular_price will have the normal product price (non discounted)

2) The product is NOT on sale:

  • _price and _regular_price will have the normal product price.
  • _sale_price will be empty

So _price and _regular_price need always to be updated…

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • What I see when looking at database is that mostly single products are affected by this non-change in price and variable products seem ok. – Wed Feb 19 '18 at 08:42
  • Correction: After closer look I see that there are simple products and variable products that are affected by this non-changed price. Does your solution include simple products as well? – Wed Feb 19 '18 at 08:47
  • @Wed Single products are not affected… I have just tried now, updating a simple product price in the database and the price displayed change also immediately in frontend/backend of wordpress/woocommere … so there is something else that is caching the data in your website… – LoicTheAztec Feb 19 '18 at 08:47
  • unfortunately for me even single products are affected. Take an example of this simple product. Backend: https://pasteboard.co/H8kYwZB.png Frontend: https://pasteboard.co/H8kZREp.jpg I appriciate your help, This is really perplexing for me. – Wed Feb 19 '18 at 08:58
  • @Wed I have updated my answer regarding simple products (other product types), may be this will help you… Now as I don't have your problem, I can't help you anymore on this. – LoicTheAztec Feb 19 '18 at 09:03
  • What i figured out is if I just update this single product its price is different ion backend and frontend, but if i make a change in backend (i.e. add ",00" at the end of regular price amount) it changes in frontend. – Wed Feb 19 '18 at 09:04
  • 1
    @Wed What you can try to see if it makes something: via phpMyAdmin chose a product ID (`post_id`) in `wp_postmeta` table and update values for keys `_price` and `_regular_price` with the same amount… Now check in backend/frontend… be sure that you have disable cache plugins or hosting cache. – LoicTheAztec Feb 19 '18 at 09:09