问题
I have a SQL script that is supposed to synchronize and adjust regular price comparing the text document uploaded daily on server.
Problem is that new prices are written to database, and I see them when looking at database. They even show in Regular price field. Problem is that frontend shows old prices. Old prices are shown on frontend untill I manually just re-update each product in backend. Since i have thousands of products it is ineffective and tedious.
What am I missing?
回答1:
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…
来源:https://stackoverflow.com/questions/48861390/updating-product-prices-cache-issue-using-a-sql-query-in-woocommerce