Trying to locate and bulk edit WooCommerce prices in MySQL

霸气de小男生 提交于 2019-12-04 08:16:16

This is how you would print the prices:

SELECT p.id, p.post_title, m.meta_key, m.meta_value
FROM wp_posts p
INNER JOIN wp_postmeta m ON p.id=m.post_id 
    AND m.meta_key='_price'

What do you want to do with the prices? Update them?

To set the prices to a specific value, it is quite simple. Set price to whatever you want.

UPDATE wp_postmeta m 
    JOIN wp_posts p ON m.post_id=p.id 
    AND m.meta_key = '_price' 
    AND p.post_type = 'product'
SET m.meta_value = <price>

We released PW WooCommerce Bulk Edit to the WordPress.org repo a few days ago. This would help in your situation without needing to get into the database.

The plugin is free and would help clean up your prices. Full disclosure: there is a paid version that has more fields/features.

https://wordpress.org/plugins/pw-bulk-edit

I needed to do something like this. I had to update prices for 110 products. All products had a variation on size with the price being the same for all sizes. This is the query I used to get all the fields I needed to change on the backend. I searched online and I did a search for all meta keys that had price in the name. The price on the frontend was not changing after the update SQL. What I discovered was that if I did a bulk edit (of regular price since this does not work for variation prices) on one product then all the prices reflected the change in the database. Strange. Maybe a caching thing.

SELECT p.id, IF( p.post_parent =0, p.id, p.post_parent ) AS parent, p.post_type, p.post_title, m.meta_key, m.meta_value
FROM wp_posts p
INNER JOIN wp_postmeta m ON p.id = m.post_id
WHERE
    (m.meta_key IN ('_price','_regular_price') AND p.post_type = 'product_variation') OR
    (m.meta_key IN ('_max_variation_price','_min_variation_price','_max_variation_regular_price','_min_variation_regular_price') AND p.post_type = 'product')
ORDER BY parent
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!