Skip to content

Instantly share code, notes, and snippets.

@ApoGouv
Last active March 21, 2018 09:54
Show Gist options
  • Save ApoGouv/0ba5fc9a34ccb8f7c9206cfdcbbbc00c to your computer and use it in GitHub Desktop.
Save ApoGouv/0ba5fc9a34ccb8f7c9206cfdcbbbc00c to your computer and use it in GitHub Desktop.
WordPress - WooCommerce product prices queries
/*
Update _price to be equal to _sales_price when there is a sales price set
*/
UPDATE `wp_postmeta` AS p
LEFT JOIN `wp_postmeta` AS sp ON p.post_id = sp.post_id
SET p.meta_value = sp.meta_value
WHERE p.meta_key = '_price'
AND sp.meta_key = '_sale_price'
AND sp.meta_value != ''
/*
Update _sales_price to none ( '' ) when there is a sales price set
*/
UPDATE `wp_postmeta` AS sp
SET sp.meta_value = ''
WHERE sp.meta_key = '_sale_price'
AND sp.meta_value != ''
/*
Get Products _sale_price, _regular_price and _price when _sale_price is not empty
*/
SELECT sp.post_id AS ID, sp.meta_value AS SalesPrice, rp.meta_value AS RegulPrice, p.meta_value AS Price
FROM `wp_postmeta` AS sp
LEFT JOIN `wp_postmeta` AS rp ON sp.post_id = rp.post_id
LEFT JOIN `wp_postmeta` AS p ON sp.post_id = p.post_id
WHERE sp.meta_key = '_sale_price'
AND sp.meta_value != ''
AND rp.meta_key = '_regular_price'
AND p.meta_key = '_price'
/*
Update _regular_price to be equal to _sales_price when there is a sales price set
*/
UPDATE `wp_postmeta` AS rp
LEFT JOIN `wp_postmeta` AS sp ON rp.post_id = sp.post_id
SET rp.meta_value = sp.meta_value
WHERE rp.meta_key = '_regular_price'
AND sp.meta_key = '_sale_price'
AND sp.meta_value != ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment