![](/img/trans.png)
[英]Is there a way to make a sale price null if the value is 0 wooCommerce including product variations
[英]Add sale price programmatically to 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');
但是后端(產品列表)和前端給我舊的價格
此外,我找到了其他工作相同的解決方案:
$product_variable = new WC_Product_Variable($post_id);
$product_variable->sync($post_id);
wc_delete_product_transients($post_id);
更新:價格也緩存在臨時
wp_options
表中。假設您的產品 ID 是
222
,您將在wp_options
表中擁有該瞬態 meta_keys(對於此產品 ID):'_transient_timeout_wc_product_children_22' '_transient_wc_product_children_22' '_transient_timeout_wc_var_prices_222' // <=== <=== HERE '_transient_wc_var_prices_222' // <=== <=== <=== HERE
您可以嘗試做的是將過期日期
meta_value
更新為過時的時間戳,如下所示:// Set here your product ID $main_product_id = 222 $transcient_product_meta_key = '_transient_wc_var_prices_'. $main_product_id; update_option( $transcient_product_meta_key, strtotime("-12 hours") ); wp_cache_delete ( 'alloptions', 'options' ); // Refresh caches
通過這種方式,您將強制系統重建這個過時的緩存瞬態。
此外,您應該嘗試在您的父產品 ID(設置變體的主要產品)中添加/更新這些:
// Set here your Main product ID (for example the last variation ID of your product)
$post_id = 22;
// Set here your variation ID (for example the last variation ID of your product)
$variation_id = 24;
// Here your Regular price
$reg_price = 100;
// Here your Sale price
$sale_price = 50;
update_post_meta($post_id, '_min_variation_price', $sale_price);
update_post_meta($post_id, '_max_variation_price', $sale_price);
update_post_meta($post_id, '_min_variation_regular_price', $reg_price);
update_post_meta($post_id, '_max_variation_regular_price', $reg_price);
update_post_meta($post_id, '_min_variation_sale_price', $sale_price);
update_post_meta($post_id, '_max_variation_sale_price', $sale_price);
update_post_meta($post_id, '_min_price_variation_id', $variation_id);
update_post_meta($post_id, '_max_price_variation_id', $variation_id);
update_post_meta($post_id, '_min_regular_price_variation_id', $variation_id);
update_post_meta($post_id, '_max_regular_price_variation_id', $variation_id);
update_post_meta($post_id, '_min_sale_price_variation_id', $variation_id);
update_post_meta($post_id, '_max_sale_price_variation_id', $variation_id);
// Optionally
wc_delete_product_transients($variation_id);
對於 2021 年 4 月,這是代碼:
$variation = wc_get_product_object( 'variation', $variation_id );
$variation->set_props(
array(
'regular_price' => $price,
'sale_price' => $sale_price,
)
);
$variation->save();
來源:函數 save_variations( $post_id, $post )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.