Update product variation woocommerce programmatically

This tutorial explains how to update product variation woocommerce programmatically. Here it reads the data from the CSV and mass 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.


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.


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;


Share this Tutorial