benjiiiii
benjiiiii

Reputation: 478

WooCommerce REST API Setting Product Image - WooCommerce.Net

I'm trying to add products using the WooCommerce .Net Rest API. I have managed to add a product in using this. However, I can't get it to add the product image.

I have the following code for adding a product:

        Product p = new Product();

        p.name = "Ben";
        p.description = "Testing Dan's Example Code.";
        p.short_description = "I hope it works.";
        p.price = 3;

        await wc.Product.Add(p);

I can't find anything on how to add an image to this product though.

p.images = ??;

I have found the following JSON which is used to add images but I can't work out the c# equivalent.

     "images":[
     {
        "src":"https://www.example.com/image.jpg",
        "position":0
     }
     ],

Does anyone have any ideas regarding this?

EDIT: I have attempted to write my own way of uploading the image and have got the following:

        List<Image> productImageList = new List<Image>();

        productImageList.Add(new Image()
        {
            name = "TEST",
            src = "www.test.com",
            position = 0
        });

However, image does not contain a definition for these names. Is there a WooCommerce Rest version of Image that would work?

EDIT2: To answer my own question above - Yes there is.

       productImageList.Add(new ProductImage()
        {
            name = "TEST",
            src = "https://res.cloudinary.com/pricecheck/image/upload/c_pad,h_800,w_800,d_noimg.jpg/TOAQU093-1.jpg",
            position = 0
        });

The above code will allow me to add a product to an image. However, the image doesn't retain its Cloudinary source once it has been uploaded. The image is added to the wordpress library and the source becomes this.

Upvotes: 1

Views: 4765

Answers (1)

Ajay Ghaghretiya
Ajay Ghaghretiya

Reputation: 822

productImageList.Add(new ProductImage()
        {
            name = "TEST",
            src = "https://res.cloudinary.com/pricecheck/image/upload/c_pad,h_800,w_800,d_noimg.jpg/TOAQU093-1.jpg",
            position = 0
        });

Prepare the Image JSON object as per the above. I have not more idea to how to create image JSON object.

Then, You can be passed the image object as below into the product object to create the product in WooCommerce.

        Product p = new Product();

        p.name = "Ben";
        p.description = "Testing Dan's Example Code.";
        p.short_description = "I hope it works.";
        p.price = 3;
        p.images = productImageList;
        await wc.Product.Add(p);

Please Read the official docs of WooCommerce REST API Here

Upvotes: 1

Related Questions