Bulk update product variation price woocommerce

This tutorial explains how to bulk update product variation price woocommerce. Here it reads the data from the CSV and bulk update the product variation price woocommerce programmatically.

The program reads data from the sample CSV and update the product variation price of the following attribute as shown below.Please also refer other wordpress tutorials.


bulk_update_product_variation_price_woocommerce_list
Bulk update product variation price woocommerce

Step by step explanation

  • Establish database connection
  • Read CSV data
  • Bulk update product variation price woocommerce

Establish database connection

Create updateprice.php file inside the root folder and establish the database connection as shown below.

Update product variations price woocommerce

Read CSV data

Read the CSV data and set the sku as index.Create other array with contain only the skus as shown below. Copy the code from here.

update_product_variation_prices_woocommerce_programmatically

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;
    
}

Share this Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *

*