questions

Add product variations programmatically via specific data in WooCommerce

Rate this post

What i am trying to do is save a new variation but when saving it, it throws an error Call to a member function get_name() on string; The variations are being created but without any attribute value.

public static function create_product_variants($id,$product,$variant_data){
        
        for($i=0; $i< count($variant_data); $i++){
            $variant = new WC_Product_Variation();
            $variant->set_parent_id($id);
            
            foreach($variant_data[$i] as $key => $value){
                if($key == 'attribute_name'){
                    $attribute_name = $value;
                }
                if($key == 'variant'){
                    if(gettype($value) == 'string'){
                        $variants = [$value];
                    }else{
                        $variants = $value;
                    }   
                }
            }
            if(count($variants) == count($attribute_name)){
                $variant_attributes = array();
            
                for($j=0; $j< count($variants); $j++){
                    $variant_attributes[$attribute_name[$j]] = $variants[$j];
                }
                $variant->set_attributes($variant_attributes);
            }
            $id = $variant->save();
            
        }
}

The data passing through $variant_data is like:

[
    {
      attribute_name: ["Color","Size"]
      price: "10"
      quantity: "20"
      variant: ["red","24"]
    }
]

$id is product_id

 

✔️Solution:

There are some complications and mistakes in your code. Instead try the following revisited code:

public static function create_product_variants( $id, $product, $data ){
    foreach( $data as $variation_data ){
        if( isset($variation_data['attribute_name']) && isset($variation_data['variant']) ){
            $variation = new WC_Product_Variation();
            
            $variation->set_parent_id($id); // Set parent ID
            
            $variation->set_regular_price($variation_data['price']); // Set price
            $variation->set_price($variation_data['price']); // Set price
            
            // Enable and set stock
            if ( isset($variation_data['quantity']) ) {
                $variation->set_manage_stock(true);
                $variation->set_stock_quantity($variation_data['quantity']);
                $variation->set_stock_status('instock');
            }
            
            $attributes      = array(); // Initializing
            $attribute_names = (array) $variation_data['attribute_name'];
            $attribute_terms = (array) $variation_data['variant']; 
        
            // Formatting attributes data array
            foreach( $attribute_names as $key => $attribute_name ){
                $attributes[sanitize_title($attribute_name)] = $attribute_terms[$key];
            }
            
            $variation->set_attributes($attributes); // Set attributes
            $variation_id = $variation->save(); // Save to database (return the variation Id)
        }
    }
}

It should better work.

The variations will be created/saved, the the method get_name() should not throw any error this time.

Leave a Reply

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

Back to top button