• Resolved buddhika23

    (@buddhika23)


    Hi,

    I am trying to create products in WooCommerce same time I do create in my inventory
    application. It was very easy to achieve the initial objective, but I have set of attributes also need to be created and mapped to these products at the same time (in another request also fine).
    I tried the product POST method with attribute property filled with an array of attribute objects, it didn’t work. Then tried to update an existing product with attributes, that also failed (both of these cases tried to link a global attribute as well as create a new attribute). Again tried with the bulk endpoint, which also didn’t give me any better than to Create and Update endpoints.
    Please help me to figure it out a possible work around or the correct method of doing this.

    Ex:

    {
        "name":"My Test 123",
        "regular_price":20,
        "attributes":[{
            "name":"att1",
            "slug":"pa_att1",
            "order_by":"name",
            "options": [
                    "test"
                ]
        }]
    }
    • This topic was modified 5 years, 1 month ago by buddhika23. Reason: Code segment was not in proper format

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Remi Corson

    (@corsonr)

    Automattic Happiness Engineer

    Hi @buddhika23,

    If I am capturing this correctly, you’d like to create a product and attributes mapped to it at the same with the RESP API, with one or two calls. To do so, I’d suggest you check this post that provides a working snippet:

    https://rudrastyh.com/woocommerce/rest-api-create-product-with-attributes.html

    Thread Starter buddhika23

    (@buddhika23)

    Thank you very much @corsonr for your very quick response.

    Yes, your understanding is exactly what I meant in my question.

    Actually this is the post, I referred in my attached sample code too. Unfortunately this method didn’t work for me. In this sample, it has consumed the version 2, I tried with v2 as well as v3.

    Thank you again for your quick suggestion.

    Remi Corson

    (@corsonr)

    Automattic Happiness Engineer

    Hi @buddhika23,

    what about something this? This code is from the REST API doc:

    $data = [
    ‘name’ => ‘Ship Your Idea’,
    ‘type’ => ‘variable’,
    ‘description’ => ‘Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.’,
    ‘short_description’ => ‘Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.’,
    ‘categories’ => [
    [
    ‘id’ => 9
    ],
    [
    ‘id’ => 14
    ]
    ],
    ‘images’ => [
    [
    ‘src’ => ‘http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_front.jpg’
    ],
    [
    ‘src’ => ‘http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_back.jpg’
    ],
    [
    ‘src’ => ‘http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_3_front.jpg’
    ],
    [
    ‘src’ => ‘http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_3_back.jpg’
    ]
    ],
    ‘attributes’ => [
    [
    ‘id’ => 6,
    ‘position’ => 0,
    ‘visible’ => false,
    ‘variation’ => true,
    ‘options’ => [
    ‘Black’,
    ‘Green’
    ]
    ],
    [
    ‘name’ => ‘Size’,
    ‘position’ => 0,
    ‘visible’ => true,
    ‘variation’ => true,
    ‘options’ => [
    ‘S’,
    ‘M’
    ]
    ]
    ],
    ‘default_attributes’ => [
    [
    ‘id’ => 6,
    ‘option’ => ‘Black’
    ],
    [
    ‘name’ => ‘Size’,
    ‘option’ => ‘S’
    ]
    ]
    ];

    print_r($woocommerce->post(‘products’, $data));

    Thread Starter buddhika23

    (@buddhika23)

    Thank you @corsonr again.

    I tried the same payload by just changing the ids of the attributes to match with my data, still it doesn’t reflect in the product attributes.
    Please check the requested payload and response payload as below.

    {
       "name":"Ship Your Idea",
       "type":"variable",
       "description":"Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.",
       "short_description":"Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.",
       "categories":[
          {
             "id":9
          },
          {
             "id":14
          }
       ],
       "images":[
          {
             "src":"http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_front.jpg’"
          },
          {
             "src":"http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_back.jpg’"
          },
          {
             "src":"http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_3_front.jpg’"
          },
          {
             "src":"http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_3_back.jpg’"
          }
       ],
       "attributes":[
          {
             "id":2,
             "position":0,
             "visible":false,
             "variation":true,
             "options":[
                "Black",
                "Green"
             ]
          },
          {
             "name":"Size",
             "position":0,
             "visible":true,
             "variation":true,
             "options":[
                "S",
                "M"
             ]
          }
       ],
       "default_attributes":[
          {
             "id":2,
             "option":"Black"
          },
          {
             "id":1,
             "option":"S"
          }
       ]
    }

    Response Payload

    {
        "id": 56,
        "name": "Product",
        "slug": "product-5",
        "permalink": "http://localhost:8080/woo/product/product-5/",
        "date_created": "2021-03-18T19:09:47",
        "date_created_gmt": "2021-03-18T13:39:47",
        "date_modified": "2021-03-18T19:09:47",
        "date_modified_gmt": "2021-03-18T13:39:47",
        "type": "simple",
        "status": "publish",
        "featured": false,
        "catalog_visibility": "visible",
        "description": "",
        "short_description": "",
        "sku": "",
        "price": "",
        "regular_price": "",
        "sale_price": "",
        "date_on_sale_from": null,
        "date_on_sale_from_gmt": null,
        "date_on_sale_to": null,
        "date_on_sale_to_gmt": null,
        "on_sale": false,
        "purchasable": false,
        "total_sales": 0,
        "virtual": false,
        "downloadable": false,
        "downloads": [],
        "download_limit": -1,
        "download_expiry": -1,
        "external_url": "",
        "button_text": "",
        "tax_status": "taxable",
        "tax_class": "",
        "manage_stock": false,
        "stock_quantity": null,
        "in_stock": true,
        "backorders": "no",
        "backorders_allowed": false,
        "backordered": false,
        "sold_individually": false,
        "weight": "",
        "dimensions": {
            "length": "",
            "width": "",
            "height": ""
        },
        "shipping_required": true,
        "shipping_taxable": true,
        "shipping_class": "",
        "shipping_class_id": 0,
        "reviews_allowed": true,
        "average_rating": "0",
        "rating_count": 0,
        "upsell_ids": [],
        "cross_sell_ids": [],
        "parent_id": 0,
        "purchase_note": "",
        "categories": [
            {
                "id": 15,
                "name": "Uncategorized",
                "slug": "uncategorized"
            }
        ],
        "tags": [],
        "images": [
            {
                "id": 0,
                "date_created": "2021-03-19T00:39:47",
                "date_created_gmt": "2021-03-18T13:39:47",
                "date_modified": "2021-03-19T00:39:47",
                "date_modified_gmt": "2021-03-18T13:39:47",
                "src": "http://localhost:8080/woo/wp-content/uploads/woocommerce-placeholder-450x450.png",
                "name": "Placeholder",
                "alt": "Placeholder",
                "position": 0
            }
        ],
        "attributes": [],
        "default_attributes": [],
        "variations": [],
        "grouped_products": [],
        "menu_order": 0,
        "price_html": "",
        "related_ids": [
            34,
            37,
            32,
            36,
            31
        ],
        "meta_data": [],
        "_links": {
            "self": [
                {
                    "href": "http://localhost:8080/woo/wp-json/wc/v2/products/56"
                }
            ],
            "collection": [
                {
                    "href": "http://localhost:8080/woo/wp-json/wc/v2/products"
                }
            ]
        }
    }

    Have I missed anything important? Please help to find it out.

    • This reply was modified 5 years, 1 month ago by buddhika23.
    Remi Corson

    (@corsonr)

    Automattic Happiness Engineer

    Hi @buddhika23,

    I have to admit I’m not sure why that doesn’t work here. I see that you’re testing locally, maybe your server settings are messing things up?

    Hi @buddhika23

    We’ve not heard back from you in a while, so I’m marking this thread as resolved. Hopefully, you were able to find a solution to your problem! If you have further questions, please feel free to open a new topic.

    Thread Starter buddhika23

    (@buddhika23)

    Hi @maykato,

    I am really sorry for my delayed reply.

    I managed to resolve the issue. I was an issue with the content type of the request.

    I used Postman to test the api which I generated through Swagger, it had added multipart/form-data with application/json which I added manually.
    Once I removed multipart/form-data it started to working as I expected.

    Thank you very much for the fast responses I received.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Create a product with attributes using REST API’ is closed to new replies.