Skip to content

Selecting a product variation attribute #797

@dmainmon

Description

@dmainmon

I can add product attributes and product variations. How do you select the default attributes for a variation? The top row of the image below shows what I'd like to achieve. The options for the drop downs are set in the product attributes.

Image

I've tried the following code:


 var variationList = new List<Variation>();
 List<ProductDefaultAttribute> default_attributes = new List<ProductDefaultAttribute>();


 variationList.Add(new Variation
  {
       regular_price = 33.3,
       price = 23.99,
       description =  "Some product " ,
       weight = 1.2,
       sku = "SKU num"
      // attributes = myVarAttrList

   });


`List<VariationAttribute> myVarAttrList = new List<VariationAttribute>();
        VariationAttribute varAttrib = new VariationAttribute();
        varAttrib.option = "Not a Sample";
        varAttrib.name = "Sample";
     //   varAttrib.id = 2;

        myVarAttrList.Add(varAttrib);

        foreach (Variation variat in variationList)
        {
          //  variat.attributes.Add(varAttrib);  this causes and Object reference not set exception
            variat.attributes = myVarAttrList;

            var addVar = wc.Product.Variations.Add(variat, Convert.ToUInt32(RecID));

          
        }`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions