简体   繁体   English

以编程方式将销售价格添加到产品变体

[英]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但是后端(产品列表)和前端给我旧的价格

在此处输入图片说明

Furthermore, I've find other solution that work same:此外,我找到了其他工作相同的解决方案:

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

Update: Prices are also cached in transient wp_options table.更新:价格也缓存在临时wp_options表中。

Let say your product ID is 222 , you will have that transients meta_keys in wp_options table (for this product ID):假设您的产品 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

What you can try to do is to update expiration date meta_value to an outdated timestamp, this way:您可以尝试做的是将过期日期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

This way you will force the system to rebuild this outdated cached transient.通过这种方式,您将强制系统重建这个过时的缓存瞬态。


Additionally you should try to add/update in your parent product ID (the main product where variation are set) these:此外,您应该尝试在您的父产品 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);

For April 2021, this is the code:对于 2021 年 4 月,这是代码:

        $variation = wc_get_product_object( 'variation', $variation_id );
        $variation->set_props(
                array(
                    'regular_price' => $price,
                    'sale_price' => $sale_price,
                     )
            );
        $variation->save();

Source: function save_variations( $post_id, $post )来源:函数 save_variations( $post_id, $post )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果值为 0 wooCommerce 包括产品变化,有没有办法制定销售价格 null - Is there a way to make a sale price null if the value is 0 wooCommerce including product variations 在 WooCommerce 3 中以编程方式设置产品销售价格 - Set product sale price programmatically in WooCommerce 3 在 Woocommerce 3 中以编程方式设置产品销售价格和购物车项目价格 - Set programmatically product sale price and cart item prices in Woocommerce 3 获取 WooCommerce 可变产品变体的销售日期 - Get on sale dates for the variations of a WooCommerce variable product 在 Woocommerce 单品中添加带有销售日期后销售价格的自定义文本 - Add custom text with sale dates after sale price in Woocommerce Single product 产品正常价格优惠券折扣(非促销价) - Coupon discount on product regular price (not sale price) 以编程方式将产品添加到购物车并更改价格 - Programmatically add product to cart with price change 通过 WooCommerce 中的特定数据以编程方式添加产品变体 - Add product variations programmatically via specific data in WooCommerce 在 WooCommerce 结帐问题中显示产品销售价格 - Display product sale price in WooCommerce checkout issues Woocommerce 通过正确的产品分类更改销售价格 - Woocommerce change sale price with correct product sorting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM