2017-01-09 69 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); 
相关问题