Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to update pm_virtual.meta_value = 'no' in Woocommerce

I need to update the value of pm_virtual.meta_value = 'no', only for those products that have pm_downloadable.meta_value = 'yes'. How do I do this?

SELECT
  p.ID,
  p.post_title,
  pm_downloadable.meta_value AS downloadable_trait,
  pm_virtual.meta_value AS virtual_trait
FROM
  xhdps_posts AS p
  INNER JOIN xhdps_postmeta AS pm_downloadable ON p.ID = pm_downloadable.post_id
  LEFT JOIN xhdps_postmeta AS pm_virtual ON p.ID = pm_virtual.post_id
  AND pm_virtual.meta_key = '_virtual'
WHERE
  p.post_type = 'product'
  AND p.post_status = 'publish'
  AND pm_downloadable.meta_key = '_downloadable'
  AND pm_downloadable.meta_value = 'yes'
  AND pm_virtual.meta_value = 'no'

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use an UPDATE`` statement along with a JOIN` to filter the products based on the specified conditions.

UPDATE xhdps_postmeta AS pm_virtual
INNER JOIN xhdps_postmeta AS pm_downloadable ON pm_virtual.post_id = pm_downloadable.post_id
INNER JOIN xhdps_posts AS p ON pm_downloadable.post_id = p.ID
SET pm_virtual.meta_value = 'no'
WHERE 
    p.post_type = 'product'
    AND p.post_status = 'publish'
    AND pm_downloadable.meta_key = '_downloadable'
    AND pm_downloadable.meta_value = 'yes'
    AND pm_virtual.meta_key = '_virtual';
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading