33

I have created a variable product ("parent" product) in WooCommerce version 3+. From a WordPress plugin, I would like to programmatically create the product variations ("children" product) with new attribute values.

The variation attributes are already set in WooCommerce.
So every time one variation is created, the new attribute's values should be created programmatically too and set in the parent Variable product.

How can this be done? is it possible?


Update : I had written more lines of code on this that I wished, and tried many things to solve it, using woocommerce objects, and added missing data about terms, termmeta, the relationship from term with post, in the database using the WordPress database object - but nothing has sufficed to make it work. And I couldn't pin-point where I went wrong - that is why I couldn't provide a narrower problem - things for which stackoverflow is more made for.

4
  • 1
    Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! See also: How to Ask
    – j08691
    Commented Dec 11, 2017 at 21:29
  • 1
    You should close one of these - stackoverflow.com/questions/47518333/…
    – Adam
    Commented Dec 11, 2017 at 21:36
  • Although I personally think this is a fair question as is, it would have been more useful to provide at least your best attempt. That could guide others not only in terms of helping you, but also those facing the same issue
    – Mike A
    Commented Jan 24, 2021 at 9:45
  • @MikeA I understand, and that is what I usually do when I write a question on Stackoverflow. But for this specific one, in this case, providing my best attempts would have resulted in 2 big chunk of code that would have been definitely counter-productive to the community. You can trust me on that one!
    – Cedric
    Commented Jan 25, 2021 at 12:51

13 Answers 13

75
+150

Update January 2020: Changed to WC_Product method get_name() instead of get_title()
Update September 2018: Handling taxonomy creation (Thanks to Carl F. Corneil)

From a defined variable product ID You will find below, a custom function that will add (create) a Product variation. The variable parent product needs to have set for it the needed attributes.

You will need to provide some information as:

  • the array of attributes/values
  • the Sku, prices and stock….

This data has to be stored in a formatted multi dimensional array (see an example at the end).

This function will check if the attributes values (term name) already exist and if not:

  • it create it for the product attribute
  • set it in the parent variable product.

The custom function code:

/**
 * Create a product variation for a defined variable product ID.
 *
 * @since 3.0.0
 * @param int   $product_id | Post ID of the product parent variable product.
 * @param array $variation_data | The data to insert in the product.
 */

function create_product_variation( $product_id, $variation_data ){
    // Get the Variable product object (parent)
    $product = wc_get_product($product_id);

    $variation_post = array(
        'post_title'  => $product->get_name(),
        'post_name'   => 'product-'.$product_id.'-variation',
        'post_status' => 'publish',
        'post_parent' => $product_id,
        'post_type'   => 'product_variation',
        'guid'        => $product->get_permalink()
    );

    // Creating the product variation
    $variation_id = wp_insert_post( $variation_post );

    // Get an instance of the WC_Product_Variation object
    $variation = new WC_Product_Variation( $variation_id );

    // Iterating through the variations attributes
    foreach ($variation_data['attributes'] as $attribute => $term_name )
    {
        $taxonomy = 'pa_'.$attribute; // The attribute taxonomy

        // If taxonomy doesn't exists we create it (Thanks to Carl F. Corneil)
        if( ! taxonomy_exists( $taxonomy ) ){
            register_taxonomy(
                $taxonomy,
               'product_variation',
                array(
                    'hierarchical' => false,
                    'label' => ucfirst( $attribute ),
                    'query_var' => true,
                    'rewrite' => array( 'slug' => sanitize_title($attribute) ), // The base slug
                ),
            );
        }

        // Check if the Term name exist and if not we create it.
        if( ! term_exists( $term_name, $taxonomy ) )
            wp_insert_term( $term_name, $taxonomy ); // Create the term

        $term_slug = get_term_by('name', $term_name, $taxonomy )->slug; // Get the term slug

        // Get the post Terms names from the parent variable product.
        $post_term_names =  wp_get_post_terms( $product_id, $taxonomy, array('fields' => 'names') );

        // Check if the post term exist and if not we set it in the parent variable product.
        if( ! in_array( $term_name, $post_term_names ) )
            wp_set_post_terms( $product_id, $term_name, $taxonomy, true );

        // Set/save the attribute data in the product variation
        update_post_meta( $variation_id, 'attribute_'.$taxonomy, $term_slug );
    }

    ## Set/save all other data

    // SKU
    if( ! empty( $variation_data['sku'] ) )
        $variation->set_sku( $variation_data['sku'] );

    // Prices
    if( empty( $variation_data['sale_price'] ) ){
        $variation->set_price( $variation_data['regular_price'] );
    } else {
        $variation->set_price( $variation_data['sale_price'] );
        $variation->set_sale_price( $variation_data['sale_price'] );
    }
    $variation->set_regular_price( $variation_data['regular_price'] );

    // Stock
    if( ! empty($variation_data['stock_qty']) ){
        $variation->set_stock_quantity( $variation_data['stock_qty'] );
        $variation->set_manage_stock(true);
        $variation->set_stock_status('');
    } else {
        $variation->set_manage_stock(false);
    }
    
    $variation->set_weight(''); // weight (reseting)

    $variation->save(); // Save the data
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Usage (example with 2 attributes):

$parent_id = 746; // Or get the variable product id dynamically

// The variation data
$variation_data =  array(
    'attributes' => array(
        'size'  => 'M',
        'color' => 'Green',
    ),
    'sku'           => '',
    'regular_price' => '22.00',
    'sale_price'    => '',
    'stock_qty'     => 10,
);

// The function to be run
create_product_variation( $parent_id, $variation_data );

Tested and works.

Part 2: Create programmatically a variable product and two new attributes in WooCommerce

You will get this in backend:

enter image description here

And it will work perfectly in front end.

Related: Create programmatically a product using CRUD methods in Woocommerce 3

24
  • 1
    @LiocTheAztec I find some code is usually acceptable, but sure, no problem. I've posted my suggestion as a new answer, hopefully you or others will find it useful. Thanks for your code, it worked great!
    – Davey
    Commented Jan 31, 2018 at 16:40
  • 1
    @RichardMišenčík Change it simply to 'post_title' => $product->get_name()… see it on WC_Product related method documentation Commented Jan 9, 2020 at 23:40
  • 1
    @SaadAbbasi Product Attributes are a very special custom taxonomy and it's not possible to attach them with an SQL query as the data to be included in the variable product is an indexed multidimensional array under _product_attributes meta key Commented Nov 25, 2020 at 11:51
  • 2
    @LoicTheAztec i given variant product id and attributes array like you mentioned, but on product edit page attribute are not showing and varaitions are also not made. Can you help me ?
    – mobeen
    Commented Jun 11, 2021 at 14:13
  • 2
    data saved but not showing in product view or edit.
    – Arman H
    Commented Aug 22, 2021 at 8:56
18

I'm just going to throw this out there, since i couldn't get any of the above examples working. Don't ask me why as other people seem to have success. So, i took the minimalist approach and tried to figure out the bare essentials for a product attribute + variation (by creating it manually in wp and looking at the db) and came up with this.

$article_name = 'Test';

$post_id = wp_insert_post( array(
    'post_author' => 1,
    'post_title' => $article_name,
    'post_content' => 'Lorem ipsum',
    'post_status' => 'publish',
    'post_type' => "product",
) );
wp_set_object_terms( $post_id, 'variable', 'product_type' );

$attr_label = 'Test attribute';
$attr_slug = sanitize_title($attr_label);

$attributes_array[$attr_slug] = array(
    'name' => $attr_label,
    'value' => 'alternative 1 | alternative 2',
    'is_visible' => '1',
    'is_variation' => '1',
    'is_taxonomy' => '0' // for some reason, this is really important       
);
update_post_meta( $post_id, '_product_attributes', $attributes_array );

$parent_id = $post_id;
$variation = array(
    'post_title'   => $article_name . ' (variation)',
    'post_content' => '',
    'post_status'  => 'publish',
    'post_parent'  => $parent_id,
    'post_type'    => 'product_variation'
);

$variation_id = wp_insert_post( $variation );
update_post_meta( $variation_id, '_regular_price', 2 );
update_post_meta( $variation_id, '_price', 2 );
update_post_meta( $variation_id, '_stock_qty', 10 );
update_post_meta( $variation_id, 'attribute_' . $attr_slug, 'alternative 1' );
WC_Product_Variable::sync( $parent_id );

$variation_id = wp_insert_post( $variation );
update_post_meta( $variation_id, '_regular_price', 2 );
update_post_meta( $variation_id, '_price', 2 );
update_post_meta( $variation_id, '_stock_qty', 10 );
update_post_meta( $variation_id, 'attribute_' . $attr_slug, 'alternative 2' );
WC_Product_Variable::sync( $parent_id );

This is not using global product attributes, but article specific ones. Hope it helps someone as i was about ready to tear my hair out before i got it working.

EDIT: I'd say only use this if you can't get the official methods working. They change these things with time (field names such as '_regular_price" and so on) and chances are it's not super future proof to do it this way.

5
  • 1
    I am working with this script but getting error "Invalid value posted for attribute" and product is not added to the cart. how can we solve the issue? Commented Aug 15, 2019 at 9:51
  • This is kind of outside the scope of this answer, but if i had to guess i'd say that the variation attribute of the specific variant doesn't match the attributes you assigned it could have (like "alternative 1 | alternative 2"). Either way, we're dealing with updating meta fields directly on the post level here and honestly this (my solution) should really be a last resort unless you really can't get it working with the methods provided by the official WooCommerce API. Try looking at the product at the database level (phpmyadmin or such).
    – PKSWE
    Commented Aug 22, 2019 at 10:56
  • I've had the best success with a strategy similar to yours (insert a 'product_variation' post, and then set its metadata).
    – Will
    Commented Jul 28, 2020 at 14:58
  • How can i add variation image here
    – Amal
    Commented Nov 19, 2021 at 13:44
  • My best guess is you would need to use a combination of adding the image to your media library using the normal wordpress API and then using the set_image_id method of a variation like this; woocommerce.github.io/code-reference/classes/…
    – PKSWE
    Commented Nov 29, 2021 at 17:53
17

Not sure why, but none of these solutions worked for me, so I decided to create my own:

<?php
/**
 * Create a variable product on woocommerce
 * @return int Product ID
 */
function pricode_create_product(){
    $product = new WC_Product_Variable();
    $product->set_description('T-shirt variable description');
    $product->set_name('T-shirt variable');
    $product->set_sku('test-shirt');
    $product->set_price(1);
    $product->set_regular_price(1);
    $product->set_stock_status();
    $product->save();
    return $product;
}

/**
 * Create Product Attributes 
 * @param  string $name    Attribute name
 * @param  array $options Options values
 * @return Object          WC_Product_Attribute 
 */
function pricode_create_attributes( $name, $options ){
    $attribute = new WC_Product_Attribute();
    $attribute->set_id(0);
    $attribute->set_name($name);
    $attribute->set_options($options);
    $attribute->set_visible(true);
    $attribute->set_variation(true);
    return $attribute;
}

/**
 * [pricode_create_variations description]
 * @param  [type] $product_id [description]
 * @param  [type] $values     [description]
 * @return [type]             [description]
 */
function pricode_create_variations( $product_id, $values, $data ){
    $variation = new WC_Product_Variation();
    $variation->set_parent_id( $product_id );
    $variation->set_attributes($values);
    $variation->set_status('publish');
    $variation->set_sku($data->sku);
    $variation->set_price($data->price);
    $variation->set_regular_price($data->price);
    $variation->set_stock_status();
    $variation->save();
    $product = wc_get_product($product_id);
    $product->save();

}
//Adding product
$product = pricode_create_product();

//Creating Attributes 
$atts = [];
$atts[] = pricode_create_attributes('color',['red', 'green']);
$atts[] = pricode_create_attributes('size',['S', 'M']);

//Adding attributes to the created product
$product->set_attributes( $atts );
$product->save();

//Setting data (following Alexander's rec
$data = new stdClass();
$data->sku = 'sku-123';
$data->price = '10';
//Create variations
pricode_create_variations( $product->get_id(), ['color' => 'red', 'size' => 'M'], $data );

Hope it can help others.

6
  • Confirm, other solution didn't work for me. I am on Woocommerce 5.5.2 and Wordpress 5.8 and this solution works like a charm. Thank you @alejandro-giraldo !
    – fearis
    Commented Aug 2, 2021 at 11:06
  • This have issue with taxonomies (Products -> Attributes). For predefined attributes: php $taxName = 'colour'; $id = wc_attribute_taxonomy_id_by_name($taxName); $taxonomy = 'pa_colour'; // The taxonomy $term_name = "black"; // The term $term_id = get_term_by( 'name', $term_name, $taxonomy )->term_id; $atts[] = pricode_create_attributes( $taxonomy ,[$term_id], $id); pricode_create_variations( $product->get_id(), ['pa_colour' => 'black', 'pa_size' => 'm']);
    – fearis
    Commented Aug 2, 2021 at 14:35
  • you might be right, I did this for a specific trouble that I had, and didn't need basic attributes , but is totally functional for custom taxonomies. Thanks for the feedback! Commented Aug 2, 2021 at 17:40
  • where does $data in pricode_create_variations come from? It is not set anywhere. Commented Jan 14, 2022 at 7:48
  • 1
    @AlexanderBehling you were right, I did solution for a specific trouble that I had on a project, and the data was added from an API, I just added a change in the code, for the rest of the people which could be looking answers for this. Thanks! Commented Jan 17, 2022 at 22:40
8

Expanding on LoicTheAztec's answer, you can check if the attribute combination exists with the following modification to his code.

function create_update_product_variation( $product_id, $variation_data ){

    if(isset($variation_data['variation_id'])) {

      $variation_id = $variation_data['variation_id'];

    } else {

      // if the variation doesn't exist then create it

      // Get the Variable product object (parent)
      $product = wc_get_product($product_id);

      $variation_post = array(
          'post_title'  => $product->get_title(),
          'post_name'   => 'product-'.$product_id.'-variation',
          'post_status' => 'publish',
          'post_parent' => $product_id,
          'post_type'   => 'product_variation',
          'guid'        => $product->get_permalink()
      );

      // Creating the product variation
      $variation_id = wp_insert_post( $variation_post );

    }

    // ...

}

Example usage

// The variation data
$variation_data =  array(
    'attributes' => array(
        'size'  => 'M',
        'color' => 'Green',
    ),
    'sku'           => '',
    'regular_price' => '22.00',
    'sale_price'    => '1',
    'stock_qty'     => 1,
);

// check if variation exists
$meta_query = array();
foreach ($variation_data['attributes'] as $key => $value) {
  $meta_query[] = array(
    'key' => 'attribute_pa_' . $key,
    'value' => $value
  );
}

$variation_post = get_posts(array(
  'post_type' => 'product_variation',
  'numberposts' => 1,
  'post_parent'   => $parent_id,
  'meta_query' =>  $meta_query
));

if($variation_post) {
  $variation_data['variation_id'] = $variation_post[0]->ID;
}

create_update_product_variation( $product_id, $variation_data );
2
  • it will be great if you could please let me know how can i create $variation_data if i have data in that manner imgur.com/cT1sFGJ Commented Aug 15, 2019 at 5:07
  • Great addition, but does not find attributes with spaces (e.g. color: "Royal Blue") because postmeta is stored as slugs. Fix: 'value' => sanitize_title($value) (convert $value to slug) Commented Oct 31, 2020 at 13:24
4

If you want to generate products for testing in WooCommerce, you can use WooCommerce Smooth Generator, made by WooCommerce itself.

https://github.com/woocommerce/wc-smooth-generator

Example:

// Generate WC_Product object and save it to database
// 70% change generated product is simple
// 30% chance generated product is variable
$product = \WC\SmoothGenerator\Generator\Product::generate();

// Returns WC_Product object of Simple product and don't save it  to database
$product = \WC\SmoothGenerator\Generator\Product::generate_simple_product();

// Returns WC_Product object of Variable Product and saves it to database
$variable_product = \WC\SmoothGenerator\Generator\Product::generate_variable_product();

Src: https://github.com/woocommerce/wc-smooth-generator/blob/master/includes/Generator/Product.php

If you want to create products programatically, you can Product class with your needs.

1
  • That's the best solution, using the WooCommerce standards.
    – David
    Commented Jul 1, 2020 at 6:40
2

it works but needs a little corrections (2 commas in the fuctions array), I did edit some of the code to my needs.

(working on wp 4.9 | wc 3.5)

First the products needs to have the attribute already created and associated, my taxonomy is "pa_r" friendly name "R" backend attribute associeated img

the function with the correction

function create_product_variation( $product_id, $variation_data ){
// Get the Variable product object (parent)
$product = wc_get_product($product_id);

$variation_post = array(
    'post_title'  => $product->get_title(),
    'post_name'   => 'product-'.$product_id.'-variation',
    'post_status' => 'publish',
    'post_parent' => $product_id,
    'post_type'   => 'product_variation',
    'guid'        => $product->get_permalink()
);

// Creating the product variation
$variation_id = wp_insert_post( $variation_post );

// Get an instance of the WC_Product_Variation object
$variation = new WC_Product_Variation( $variation_id );

// Iterating through the variations attributes
foreach ($variation_data['attributes'] as $attribute => $term_name )
{
    $taxonomy = 'pa_'.$attribute; // The attribute taxonomy

    // If taxonomy doesn't exists we create it (Thanks to Carl F. Corneil)
    if( ! taxonomy_exists( $taxonomy ) ){
        register_taxonomy(
            $taxonomy,
            'product_variation',
            array(
                'hierarchical' => false,
                'label' => ucfirst( $taxonomy ),
                'query_var' => true,
                'rewrite' => array( 'slug' => '$taxonomy') // The base slug
            )
        );
    }

    // Check if the Term name exist and if not we create it.
    if( ! term_exists( $term_name, $taxonomy ) )
        wp_insert_term( $term_name, $taxonomy ); // Create the term

    $term_slug = get_term_by('name', $term_name, $taxonomy )->slug; // Get the term slug

    // Get the post Terms names from the parent variable product.
    $post_term_names =  wp_get_post_terms( $product_id, $taxonomy, array('fields' => 'names') );

    // Check if the post term exist and if not we set it in the parent variable product.
    if( ! in_array( $term_name, $post_term_names ) )
        wp_set_post_terms( $product_id, $term_name, $taxonomy, true );

    // Set/save the attribute data in the product variation
    update_post_meta( $variation_id, 'attribute_'.$taxonomy, $term_slug );
}

## Set/save all other data

// SKU
if( ! empty( $variation_data['sku'] ) )
    $variation->set_sku( $variation_data['sku'] );

// Prices
if( empty( $variation_data['sale_price'] ) ){
    $variation->set_price( $variation_data['regular_price'] );
} else {
    $variation->set_price( $variation_data['sale_price'] );
    $variation->set_sale_price( $variation_data['sale_price'] );
}
$variation->set_regular_price( $variation_data['regular_price'] );

// Stock
if( ! empty($variation_data['stock_qty']) ){
    $variation->set_stock_quantity( $variation_data['stock_qty'] );
    $variation->set_manage_stock(true);
    $variation->set_stock_status('');
} else {
    $variation->set_manage_stock(false);
}

$variation->set_weight(''); // weight (reseting)

$variation->save(); // Save the data
}

I made an array with the data I need in the variations [id_post, attribute, sku, regular_price, stock]

$hijos = array(
[9623,'265/70 R16','NE-CT-CO-USA-016-005','0',53],
[9624,'235/65 R17','NE-AU-CO-EUR-017-050','189000',56]
);

and the foreach to create dynamically all the variations to my products

foreach ($hijos as $vari) {
// The variation data
$variation_data =  array(
    'attributes' => array(
        'r'  => $vari[1],
    ),
    'sku'           => $vari[2],
    'regular_price' => str_replace('.', '', $vari[3]),
    'stock_qty'     => $vari[4]
);
// var_dump($variation_data);
create_product_variation( $vari[0], $variation_data );
}
4
  • how can i create variation data from the given data if have product id attribute name, options , plus or minus, price 1647 Copper Gskt No + 0.0000<br> 1647 Copper Gskt Yes + 14.9500<br> 1647 Drain Line No + 0.0000<br> 1647 Drain Line Yes + 3.0000<br> 1647 Feed Line No + 0.0000<br> 1647 Feed Line Yes + 45.0000<br> 1647 Drain Line No + 0.0000<br> 1647 Drain Line Yes + 45.0000<br> Commented Aug 11, 2019 at 4:52
  • in your array [id_post, attribute, sku, regular_price, stock] what about the attribute values for example. colors is an attribute and black red white are the attribute values Commented Aug 15, 2019 at 8:06
  • which add_action do I have to use?
    – getNordic
    Commented Dec 20, 2019 at 18:52
  • Doesn't work for me, i'm getting Uncaught Error: Call to a member function get_title() on boolean in wp-content/themes/x/functions.php:308. Line 308 is this 'post_title' => $product->get_title(), Commented Jan 7, 2020 at 17:20
1

You might run into a problem here if the the taxonomy of the product is not registered beforehand somewhere else. If you want to make sure the taxonomy exists, you could add a conditional to LoicTheAztec's answer.

Something like this.

foreach ($variation_data['attributes'] as $attribute => $term_name )
{
    //echo 'attribute ' . $attribute . ' term name ' . $term_name;

    $taxonomy = 'pa_' . $attribute; // The attribute taxonomy

    // Check if the Taxonomy exists, and if not we create it.

    if (! taxonomy_exists($taxonomy)){
        register_taxonomy(  
            $taxonomy,  //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces). 
            'product',        //post type name
            array(  
                'hierarchical' => false,  
                'label' => ucfirst($taxonomy),  //Display name
                'query_var' => true,
                'rewrite' => array(
                    'slug' => $taxonomy, // This controls the base slug that will display before each term
                    'with_front' => false // Don't display the category base before 
                ),
            )  
        ); 

    }
...
1
  • how to prepare variation_data? Commented Aug 15, 2019 at 8:07
1

In addition to Alejandro Giraldo answer to use taxonomies instead of custom product attributes you can use this modified version

<?php
/**
 * Create a variable product on woocommerce
 * @return int Product ID
 */
function pricode_create_product(){
    $product = new WC_Product_Variable();
    $product->set_description('T-shirt variable description');
    $product->set_name('T-shirt variable');
    $product->set_sku('test-shirt');
    $product->set_price(1);
    $product->set_regular_price(1);
    $product->set_stock_status();
    return $product->save();
}

/**
 * Create Product Attributes 
 * @param  string $name    Attribute name
 * @param  array $options Options values
 * @return Object          WC_Product_Attribute 
 */
function pricode_create_attributes( $name, $options ){
    $attributes = array();
    if(!empty($options)){
        if(is_string($options)){
            $term = wp_create_term(
                $options,
                wc_attribute_taxonomy_name($name)
            );
            if(is_array($term)){
                $attributes[] = (int)$term['term_id'];
            }
        }
        else{
            for($i = 0; $i < count($options); $i++){
                $term = wp_create_term(
                    $options[$i],
                    wc_attribute_taxonomy_name($name)
                );
                if(is_array($term)){
                    $attributes[] = (int)$term['term_id'];
                }
            }
        }
    }
    $attribute = new WC_Product_Attribute();
    /*
        Set the attribute id to the id of the taxonomy to use
        with wc_attribute_taxonomy_id_by_name you get the id of the taxonomy stored in {$wpdb->prefix}woocommerce_attribute_taxonomies
        with wc_attribute_taxonomy_name you convert the Attribute name to the attribute name woocommerce use 
        @see https://woocommerce.github.io/code-reference/namespaces/default.html#function_wc_attribute_taxonomy_id_by_name
        @see https://woocommerce.github.io/code-reference/namespaces/default.html#function_wc_attribute_taxonomy_name
    /*
    $attribute->set_id(wc_attribute_taxonomy_id_by_name(wc_attribute_taxonomy_name($name)));
    $attribute->set_name(wc_attribute_taxonomy_name($name));
    $attribute->set_options($attributes);
    $attribute->set_visible(true);
    $attribute->set_variation(true);
    return $attribute;
}

/**
 * [pricode_create_variations description]
 * @param  [type] $product_id [description]
 * @param  [type] $values     [description]
 * @return [type]             [description]
 */
function pricode_create_variations( $product_id, $values ){
    $variation = new WC_Product_Variation();
    $variation->set_parent_id( $product_id );
    $variation->set_attributes($values);
    $variation->set_status('publish');
    $variation->set_sku($data->sku);
    $variation->set_price($data->price);
    $variation->set_regular_price($data->price);
    $variation->set_stock_status();
    $variation->save();
    $product = wc_get_product($product_id);
    $product->save();

}
//Adding product
$product = pricode_create_product();

//Creating Attributes 
$atts = [];
$atts[] = pricode_create_attributes('color',['red', 'green']);
$atts[] = pricode_create_attributes('size',['S', 'M']);

//Adding attributes to the created product
$product->set_attributes( $atts );
$product->save();

//Create variations
pricode_create_variations( $product->get_id(), [wc_attribute_taxonomy_name('color') => sanitize_title('red'), wc_attribute_taxonomy_name('size') => sanitize_title('M')]);
1

This is the write way to create variable product

// Name and image would be enough

$product = new WC_Product_Variable();

$product->set_name( 'Wizard Hat' );
$product->set_image_id( 90 );

// one available for variation attribute
$attribute = new WC_Product_Attribute();
$attribute->set_name( 'Magical' );
$attribute->set_options( array( 'Yes', 'No' ) );
$attribute->set_position( 0 );
$attribute->set_visible( true );
$attribute->set_variation( true ); // here it is
    
$product->set_attributes( array( $attribute ) );

// save the changes and go on
$product->save();

// now we need two variations for Magical and Non-magical Wizard hat
$variation = new WC_Product_Variation();
$variation->set_parent_id( $product->get_id() );
$variation->set_attributes( array( 'magical' => 'Yes' ) );
$variation->set_regular_price( 1000000 ); // yep, magic hat is quite expensive
$variation->save();

$variation = new WC_Product_Variation();
$variation->set_parent_id( $product->get_id() );
$variation->set_attributes( array( 'magical' => 'No' ) );
$variation->set_regular_price( 500 );
$variation->save();
0

above answer (LoicTheAztec) helped me a lot but there was some issues instead of using

update_post_meta( $variation_id, 'attribute_'.$taxonomy, $term_slug );

use :
update_post_meta( $variation_id, 'attribute_'.$attribute, $term_name );

because taxonomy is already is been modified and this is causing problem to update post meta after changing this update values and not selecting automatically in admin product variation edit after this changes it work great !

1
  • does this solve the issue where the term name is not being selected on variation on the admin?
    – Elland
    Commented Aug 1, 2020 at 16:14
0

Coming in late to the party but adding to the answer from LoicTheAztec (which works perfectly) if your new variation does not end up with a selected attribute, use the following line just before the save method:

$variation->set_weight(''); //LoicTheAztec 

$variation->set_attributes($variation_data["attributes"]); // Select the attributes

$variation->save(); //LoicTheAztec 
0

The accepted article works like a charm. If you manage to create the variations but it comes out without an option selected in the variation and when you try to save a single one resets to empty, I highly suggest checking this thread Creating WooCommerce product variation adds an empty attribute value , I ve been struggling with the same issue for over 2 hours. Happy coding

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Apr 7, 2022 at 13:04
0

In addition to return_false answer: since WC 3.0 in order to solve the issue where the term name is not being selected on variation on the admin you should use set_default_attributes.

$variation->set_weight(''); //LoicTheAztec 
$variation->set_default_attributes($variation_data["attributes"]); // Select default attributes
$variation->save(); //LoicTheAztec 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.