I need to copy the regular_price to the sale_price of all variables in a variable product.
The sale_price needs to be filled minus 10%.
The sale_price is empty now.
I tried this sql script, but it’s not working. Nothing is changing.
UPDATE wp_postmeta as pm
JOIN wp_postmeta as pm2 ON pm.post_id = pm2.post_id
SET pm.meta_value = ( pm2.meta_value - 350 )
WHERE pm.meta_key LIKE '_sale_price'
AND pm2.meta_key LIKE '_regular_price'
AND pm.post_id IN
( SELECT p2.ID
FROM wp_posts AS p
JOIN wp_posts AS p2 ON p2.post_parent = p.ID
WHERE p.post_type = 'product'
AND p.post_status = 'publish'
AND p.ID = 19
AND p2.post_type = 'product_variation'
AND p2.post_status = 'publish' );
Thank you in advance!
Fixed it myself with this code:
UPDATE wp_postmeta AS sale_price_meta
JOIN (
SELECT pm1.post_id, pm1.meta_value AS regular_price
FROM wp_postmeta pm1
INNER JOIN wp_posts p ON p.ID = pm1.post_id
WHERE pm1.meta_key = '_regular_price'
AND p.post_type = 'product_variation'
) AS regular_price_data
ON sale_price_meta.post_id = regular_price_data.post_id
SET sale_price_meta.meta_value = regular_price_data.regular_price * 0.9
WHERE sale_price_meta.meta_key = '_sale_price'
AND (sale_price_meta.meta_value IS NULL OR sale_price_meta.meta_value = '');
1