gpt4 book ai didi

php - 用 PHP 更新 WooCommerce 产品的价格

转载 作者:可可西里 更新时间:2023-11-01 00:56:13 24 4
gpt4 key购买 nike

我正在尝试更改我的 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();

我希望这是有用的。

关于php - 用 PHP 更新 WooCommerce 产品的价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42456040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com