首页 文章

更新变化产品价格 - 在产品页面中不可见 - Woocommerce

提问于
浏览
1

我已经制作了可变类型的自定义导入产品 . 然后我制作了一个必须更新变化产品价格的文件 . 我正在使用update_post_meta方法 . 这些值出现在每个变体字段的编辑产品页面中,但似乎并未更新产品首页中的价格 .

我必须在管理面板中制作更新产品(通过单击更新按钮)以使用新价格 .

我尝试过使用$ product-> variable_product_sync();但它似乎没有用 . 有任何想法吗?

我的代码示例:

foreach ($variations as $variationProduct) { 
   $variationProductId =  $variationProduct["variation_id"];
   $productPrice = number_format($productPrice, 2, '.', '');
   update_post_meta( $variationProductId, '_regular_price', $productPrice);
}

对此有何帮助或解决方案?

2 回答

  • 0

    解决了!!最后我通过woocommerce api找到了它 . 如果您使用的是woocommerce 2.7或更新版本,您可以使用以下行:

    $product->save();
    
  • 1

    请使用以下脚本更新变化价格 . 点击这里获取完整代码 . https://www.pearlbells.co.uk/bulk-update-product-variation-price-woocommerce/

    function getExistingProducts($updatedPrices,$skuArray) {
    
    $loop = new WP_Query(array('post_type' => array('product', 'product_variation'), 'posts_per_page' => -1));
    
    while ($loop->have_posts()) : $loop->the_post();
    
        $id = get_the_ID();
        $product = wc_get_product( $id );
        $sku = get_post_meta($id, '_sku', true);
    
        if( in_array( $sku, $skuArray  ) ) {
    
            $attributes = $product->get_attributes();
            $attributes['medium-quantity-price']['value'] = $updatedPrices[$sku][4];
            $attributes['low-quantity-price']['value'] = $updatedPrices[$sku][3];
         $attributes['v-low-quantity-price']['value'] = $updatedPrices[$sku][2];
            update_post_meta( $id,'_product_attributes',$attributes);
            echo ' Update Sku : '.$sku.' '.PHP_EOL;
    
        }
    
    endwhile;
    
    }
    

相关问题