Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce MySQL to update price of products in specific category

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'
like image 953
Lance Avatar asked Sep 14 '25 15:09

Lance


1 Answers

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!

like image 177
Raunak Gupta Avatar answered Sep 17 '25 05:09

Raunak Gupta