WooCommerce.NET icon indicating copy to clipboard operation
WooCommerce.NET copied to clipboard

How do I create a product with variations?

Open PtrStruct opened this issue 4 years ago • 5 comments

So I'm currently using this library to create and upload products, and I can't seem to figure out how to create a product with variations. I've managed to create a product with attributes, but it doesn't add any variations.

The end goal is that it adds a variation for each option in the Attributes.options which is currently Orange Green and Blue

Also.. Why does the Variations property of Product take a list of int and not a list of Variation?

Make sure you have included the below details when open an issue. Thank you.

  • Wordpress version, WooCommerce version and WooCommerce.NET version WooCommerce Version: 4.2.0 WordPress Version Version: 5.4.1 WooCommerce.NET Version: 0.8.2

  • Steps to replicate the issue

Add a product like this

RestAPI rest = new RestAPI($"{s}", $"{t}", $"{c}");
WCObject wc = new WCObject(rest);
//Get all products
//var products = await wc.Product.GetAll();
//Add new product
Product p = new Product()
{
    type = "variable",
    name = "T-shirt",
    description = "A really cool t-shirt",
    price = 9.99M,
    stock_quantity = 10,
    in_stock = true,
    attributes = new List<ProductAttributeLine>
    {
        new ProductAttributeLine
        {
            name = "Color",
            position = 1,
            variation = true,
            visible = true,
            options = new List<string> { "Orange", "Green", "Blue" }
        }
    },

    //Not sure why this take a list of ints?
    variations = new List<int> { 1 },
    images = new List<ProductImage>
    {
        new ProductImage
        {
            src = "https://htmlcolorcodes.com/assets/images/html-color-codes-color-palette-generators-7a5b8241.jpg"
        },
        new ProductImage
        {
            src = "https://2893mhgw4jf3rm0k57gtbu1e-wpengine.netdna-ssl.com/wp-content/uploads/2019/05/color-tech-featured-762x429.jpg"
        },
    }
};
await wc.Product.Add(p);
  • Details of the error message if there is any

There is no error message, it's just that when I go to check the product details there are no variations of the product, there are attributes, but no variations.

Here is a picture of what it looks like

PtrStruct avatar Jun 12 '20 02:06 PtrStruct

Create product first, and then Variations, you can't do both in one go.

XiaoFaye avatar Jun 14 '20 22:06 XiaoFaye

How do I create a variation for the product? Do I need to specify the product ID? Because most of my products just go off of the next id and I don't set the ID explicitly.

PtrStruct avatar Jun 15 '20 00:06 PtrStruct

You must get product ID first.

My process is as follows:

  • download all my products to list of products public Task<List<T>> GetAll(object parentId, Dictionary<string, string> parms = null);
  • get product ID for item, that I want to add variations
  • download all variations of products await _wc.Product.Variations.GetAll((int)product.id, new Dictionary<string, string>() { { "per_page", "100" } })
  • check if variation of product is not exist
  • if not exist, add new variation public Task<T> Add(T item, int parentId, Dictionary<string, string> parms = null);

mirco261 avatar Jun 24 '20 09:06 mirco261

i dont know update product Variations code in c#. please help me. please mail to me at [email protected]

tiamweb avatar Sep 22 '20 16:09 tiamweb

Hi @mirco261 how can I get product id? V3 version?

MorfeoIT avatar Aug 03 '21 16:08 MorfeoIT