1
votes

i have made custom import products which are variable type. Then i have make a file that has to update the variation product price. I am using the update_post_meta method. The values are appearing in the edit product page in the field of each variation, but it's not seems to update the price in the front page of the product.

I have to make a update product (by clicking the update button) in admin panel to work with the new prices.

I have tried using $product->variable_product_sync(); but it didn't seemed to work. Any ideas?

Sample of my code:

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

Any help or solution on this?

2

2 Answers

1
votes

Solved!! Finally i found it through the woocommerce api. If you are using woocommerce 2.7 or newer you can use the following line:

$product->save();
0
votes

Please use the following script to update the variation prices. Click here get the full code. 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;

}