简体   繁体   中英

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'

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!

There is another cool way to do it without touching MySQL.

Check out WordPress Bulk Edit

Change the Screen Options to show all the products you have. If you have a LOT of products then you can do so by 500 by page.

Select All -> Bulk Options (edit) -> Select a Category and Apply

It also allows you to increase the price of all by a fixed amount or a percentage. I hope this helps someone.

Not as flexible as MySQL but does a fantastic job in certain scenarios.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM