簡體   English   中英

用PHP更新WooCommerce產品的價格

[英]Update price of a WooCommerce product in PHP

我想在我的WooCommerce網站上更改各種產品的價格,但我遇到了問題。 當我運行腳本時,如果我在數據庫和目標中correctly更改,網絡的前端仍然顯示為錯誤的價格,甚至一些價格顯示為“免費”。

最奇怪的是,如果我看到價格正確,進入產品版本。

我給你代碼示例:

$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();  

我希望這很有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM