php - 以编程方式将销售价格添加到产品变体中

标签 php wordpress woocommerce product-variations

我需要以编程方式更新可变产品及其所有变体的销售价格。

我需要添加什么样的元字段?

我正在尝试更新主要产品,例如:

update_post_meta($post_id, '_regular_price', '100');
update_post_meta($post_id, '_price', '50');
update_post_meta($post_id, '_sale_price', '50');

然后我更新每一个变体

update_post_meta($variation_id, '_regular_price', '100');
update_post_meta($variation_id, '_price', '50');
update_post_meta($variation_id, '_sale_price', '50');
update_post_meta($variation_id, 'attribute_pa_taglia', $term_slug);
update_post_meta($variation_id, '_stock', $stock);
update_post_meta($variation_id, '_stock_status', 'instock');
update_post_meta($variation_id, '_manage_stock', 'yes');

后端:产品详情,一切正常 enter image description here

但是后端(产品列表)和前端给我旧的价格

enter image description here

最佳答案

此外,我还找到了其他效果相同的解决方案:

$product_variable = new WC_Product_Variable($post_id);
$product_variable->sync($post_id);
wc_delete_product_transients($post_id);

关于php - 以编程方式将销售价格添加到产品变体中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41547590/

相关文章:

javascript - jQuery 菜单不加载 PHP 子类别

javascript - PHP - 读取文本文件时出现问题

php - 如何优化 PHP 中的 ArrayIterator 实现?

mysql - Wordpress 4.9.6(或 4.9)是否适用于 MySQL 5.20?

html - 在 Wordpress 中设置下拉菜单样式的问题

php - Woocommerce 查询待处理订单

wordpress - 自动删除 woocommerce 客户 session 和 transient

php - WordPress 本地主机重定向

wordpress - 如何添加图像以编程方式发布?

javascript - jQuery/Javascript - 检测 WooCommerce 商店通知 html 是否可见