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

时间:2017-01-09 11:59:03

标签: php wordpress woocommerce 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');

后端:产品细节,一切都好 enter image description here

然而,后端(产品列表)和前端让我老了

enter image description here

3 个答案:

答案 0 :(得分:3)

此外,我还找到了其他相同的解决方案:

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

答案 1 :(得分:2)

  

更新:价格也会在暂时wp_options表中缓存。

     

假设您的商品标识为 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);

答案 2 :(得分:0)

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 )