I am trying to increase the price by a percentage of certain products that are in a specific category. I figured out the select statement that should work, but am having trouble combining it the update statement. Here is what I have:
SELECT * from `wp_term_relationships` where term_taxonomy_id=376 and object_id in(select ID from `wp_posts` where `post_type`='product' and post_status='publish' and ID=wp_term_relationships.object_id)
This gives me the products that I need. Can I run an UPDATE (like below) on those products or do I need to combine them somehow?
update wp_postmeta set meta_value = meta_value * 1.40 where meta_key='_regular_price'
Here is a MySQL query which'll serve your purpose.
To update _regular_price
UPDATE
`wp_postmeta`
SET
`meta_value` = ROUND(`meta_value` * 1.40, 2)
WHERE
meta_key = '_regular_price'
AND `post_id` IN (
SELECT
`object_id` AS product_id
FROM
`wp_term_relationships`
WHERE
term_taxonomy_id = 376
AND `object_id` IN (
SELECT
`ID`
FROM
`wp_posts`
WHERE
`post_type` = 'product'
AND `post_status` = 'publish'
AND `ID` = `object_id`
)
);
To update _price
UPDATE
`wp_postmeta`
SET
`meta_value` = ROUND(`meta_value` * 1.40, 2)
WHERE
meta_key = '_price'
AND `post_id` IN (
SELECT
`object_id` AS product_id
FROM
`wp_term_relationships`
WHERE
term_taxonomy_id = 376
AND `object_id` IN (
SELECT
`ID`
FROM
`wp_posts`
WHERE
`post_type` = 'product'
AND `post_status` = 'publish'
AND `ID` = `object_id`
)
);
Also you have to delete WooCommerce product price caching which is stored in wp_options
table under _transient_timeout_wc_var_prices_{{post_id}}
and _transient_wc_var_prices_{{post_id}}
in option_name
DELETE
FROM `wp_options`
WHERE (`option_name` LIKE '_transient_wc_var_prices_%'
OR `option_name` LIKE '_transient_timeout_wc_var_prices_%')
Above query is tested and worked for me.
Before running this query do take a database backup
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With