Site icon Hip-Hop Website Design and Development

Woocommerce how to update variable product price programitically?

I want to update woocommerce variable product price by code. Here I wrote some code,, its create product but not update variable price:

$post = array(

    'post_title'   => 'Product Title',

    'post_content' => '',

    'post_status'  => 'publish',

    'post_type'    => "product"

);

$new_post_id = wp_insert_post( $post );

wp_set_object_terms ($new_post_id,'variable','product_type');



/**

 * Add product attribute.

 */

$attr_names = array(

    'Length'      => array( '12', '13','14','15' )

);

$attr_data = array();

foreach ( $attr_names as $attr_name => $attr_values ) {

    $attr_sanitized_name = 'pa_' . sanitize_title( $attr_name );

    $attr_data += array(

        $attr_sanitized_name => array(

            'name'          => $attr_name,

            'value'         => implode( '|', $attr_values ),

            'is_visible'    => 1,

            'is_variation'  => 1,

            'is_taxonomy'   => 0,

            'position'      => 0,

        )

    );

}

$variation_id = update_post_meta( $new_post_id, '_product_attributes', $attr_data, TRUE );

update_post_meta($variation_id, '_regular_price', '100');