在PHP中的WooCommerce产品的更新价格
问题描述:
我试图更改我的WooCommerce网站上的各种产品的价格,但我遇到了问题。当我运行该脚本时,如果我在数据库和目标中更改correctly
,则Web前端仍然显示为错误的价格,即使某些价格显示为“免费”。在PHP中的WooCommerce产品的更新价格
最奇怪的是,通过输入产品版本,如果我看到价格正确。
我给你的代码示例:
$stock = $value['stock'];
$regular_price = $value['rates']['2']['rate_pvp'];
update_post_meta($post_id, '_regular_price', $regular_price);
update_post_meta($post_id, '_price', $regular_price);
$product->set_price($regular_price);
if($stock>0){
update_post_meta($post_id, '_stock_status', 'instock');
} else {
update_post_meta($post_id, '_stock_status', 'outofstock');
}
update_post_meta($post_id, '_stock', $stock);
echo $post_id . ':' . $value['variation_sku'] . ':' . $stock . '.............................OK<br/>';
wc_delete_product_transients();
答
我有同样的问题。我认为你应该重置wordpress的查询对象。在这里,我的示例工作正常。
$args = array('post_type' => 'product', 'product_cat' => $key);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();
global $product;
update_post_meta($product->id, '_regular_price', (float)$value);
update_post_meta($product->id, '_price', (float)$value);
endwhile;
wp_reset_query();
我希望这是有用的。
+0
对我来说,谢谢..但woocommerce 3.0.X无效($ product-> id)它需要改变它($ product-> get_id()) – mikesneider
任何缓存插件? – 4EACH
没有,只有woocommerce –
尝试在网站上按CTRL-SHIFT-R来刷新浏览器缓存 – samdd