php-MySQL查询以更改WooCommerce中的可变产品价格

我想更改woocommerce中可变产品的价格.我尝试使用下面的MySQL查询来做到这一点,

UPDATE wp_postmeta 
SET meta_value = price_value 
WHERE post_id = variation_id AND meta_key = '_sale_price';

但仅在后端(数据库和管理区域)中不更新.实际上在前端它也不显示销售价格.
由于我不擅长SQL,是否有任何代码片段或WooCommerce挂钩可让我更改可变产品的价格?

谢谢.

解决方法:

If you want to change the Product Price which is on Sale you have
to update both _sale_price and _price with same value.

//for Regular Price
UPDATE wp_postmeta 
SET meta_value = diff_price_value 
WHERE post_id = variation_id AND meta_key = '_regular_price';

//for Price
UPDATE wp_postmeta 
SET meta_value = price_value 
WHERE post_id = variation_id AND meta_key = '_price';

//for Sale Price
UPDATE wp_postmeta 
SET meta_value = price_value 
WHERE post_id = variation_id AND meta_key = '_sale_price';

添加
如果要将以上所有查询合并为一个查询,可以使用以下查询:

UPDATE wp_postmeta
SET meta_key = CASE
                   WHEN meta_key = "_regular_price" THEN diff_price_value
                   WHEN meta_key = "_price" THEN price_value
                   WHEN meta_key = "_sale_price" THEN price_value
                   ELSE meta_key
               END
WHERE post_id = variation_id
  AND meta_key IN ("_regular_price", "_price", "_sale_price");

另外,您还必须删除存储在option_name中_transient_timeout_wc_var_prices _ {{post_id}}和_transient_wc_var_prices _ {{post_id}}下的wp_options表中的WooCommerce产品价格缓存.

DELETE
FROM `wp_options`
WHERE (`option_name` LIKE '_transient_wc_var_prices_%'
    OR `option_name` LIKE '_transient_timeout_wc_var_prices_%')

以上所有查询均已通过测试并为我工作.

在运行此查询之前,请先进行数据库备份

参考:Copy WooCommerce products Sale prices to regular prices and reset Sale prices

希望这可以帮助!

上一篇:php-Woocommerce wp_query通过ID获取订单


下一篇:php-基于Woocommerce中的自定义单选按钮的动态运费