以编程方式将销售价格添加到产品变体中 [英] Add sale price programmatically to product variations
问题描述
我需要以编程方式更新可变产品及其所有变体的销售价格.
I need to update the sale price programmatically, on variable product and all his variations.
我需要添加哪种元字段?
What kind of meta field do I need to add?
我正在尝试更新主要产品,例如:
I'm trying to update main product such as:
update_post_meta($post_id, '_regular_price', '100');
update_post_meta($post_id, '_price', '50');
update_post_meta($post_id, '_sale_price', '50');
然后我更新每个单独的版本
and then I update every single variations
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');
后端:产品详细信息,一切正常
Back end: product detail, everything ok
但是后端(产品列表)和前端却给我旧价格
However backend (product list) and frontend get me old price
推荐答案
更新:价格也缓存在临时
wp_options
表中.
Update: Prices are also cached in transient
wp_options
table.
让我们假设您的产品ID为 222
,您将在wp_options
表中获得该瞬态meta_keys(用于此产品ID):
Let say your product ID is 222
, you will have that transients meta_keys in wp_options
table (for this product 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
更新为过期的时间戳:
What you can try to do is to update expiration date meta_value
to an outdated timestamp, this way:
// 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
这样,您将强制系统重建此过时的缓存瞬态.
This way you will force the system to rebuild this outdated cached transient.
此外,您应该尝试在父产品ID(设置了变体的主要产品)中添加/更新:
Additionally you should try to add/update in your parent product ID (the main product where variation are set) these:
// 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);
这篇关于以编程方式将销售价格添加到产品变体中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!