2017-01-09 86 views
2

我需要以可編程方式更新銷售價格,變量產品和所有變體。以編程方式將銷售價格添加到產品變體

需要添加什麼樣的元字段?

我試圖更新主要產品如:

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'); 

後端:產品細節,一切就OK了 enter image description here

但後端(產品列表)和前端讓我老價

enter image description here

回答

2

更新:價格也在緩存在短暫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); 
+0

謝謝,但這還不夠。仍然沒有工作。可以在woo表中設置參數嗎? – mariobros

+0

我已經嘗試清除WC瞬變,但不起作用。如果我以編程方式清除銷售價格...產品繼續出現打折(反之亦然,如果我設置銷售價格...折扣不會出現)...似乎是緩存問題,但我沒有使用任何緩存插件! – mariobros

+0

不錯!它工作正常。謝謝 – mariobros

3

而且,我已經找到工作的其他解決方案相同:

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