skip to Main Content

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

2

Answers


  1. Do it simple like

    public static function create_product_variants($id,$product,$variant_data){
            
        foreach ($variant_data as $object){
            $variant = new WC_Product_Variation();
            $variant->set_parent_id($product->get_id());
            $attributes = array_combine($object->attribute_name, $object->variant)
            $variant->set_attributes($attributes);
            $variant->save();
        }
        
    }
    

    if the object is

    [
        {
          attribute_name: ["Color","Size"]
          price: "10"
          quantity: "20"
          variant: ["red","24"]
        }
    ]
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search