1

I am a noob to the Magento ecosystem. I have read the documentation for the Magento 2 api and understand that I have to create a configurable product but I am not understanding the flow for creating a configurable product with multiple colours and sizes with individual images and stock levels. I have integrated with Woo and I created a product with an image with one api call and then the variants with stock levels and images with another api call. Magento looks like a more complex beast but any help would be appreciated. Thank you in adavance.

Kind Regards, Jason.

1 Answer 1

0

There is an official Magento document that can walk you through the creation step-by-step located here: https://devdocs.magento.com/guides/v2.3/rest/tutorials/configurable-product/config-product-intro.html

I think that would be the most helpful thing for you, but there are some principles from that document that can be summarized here!

The basic idea is that you first want to make the configurable product, or the parent. Here is their example:

POST <host>/rest/default/V1/products



{
  "product": {
    "sku": "MS-Champ",
    "name": "Champ Tee",
    "attribute_set_id": 9,
    "status": 1,
    "visibility": 4,
    "type_id": "configurable",
    "weight": "0.5",
    "extension_attributes": {
        "category_links": [
            {
                "position": 0,
                "category_id": "11"
            },
            {
                "position": 1,
                "category_id": "12"
            },
            {
                "position": 2,
                "category_id": "16"
            }
        ]
    },
    "custom_attributes": [
        {
            "attribute_code": "description",
            "value": "The Champ Tee keeps you cool and dry while you do your thing. Let everyone know who you are by adding your name on the back for only $10."
        },
        {
            "attribute_code": "tax_class_id",
            "value": "2"
        },
        {
            "attribute_code": "material",
            "value": "148"
        },
        {
            "attribute_code": "pattern",
            "value": "196"
        },
        {
            "attribute_code": "color",
            "value": "52"
        }
    ]
  }
}

Then, you create the individual children, or simple products. Make sure to give them the appropriate attribute values, such as distinct colors and sizes. There will need to be one simple product for every combination. Small and green, large and red, small and red, etc. Note that visibility 1 makes it so the product cannot be browsed individually, which is generally the desired effect for configurable children products.

POST <host>/rest/default/V1/products



{
  "product": {
    "sku": "MS-Champ-S",
    "name": "Champ Tee Small",
    "attribute_set_id": 9,
    "price": 25,
    "status": 1,
    "visibility": 1,
    "type_id": "simple",
    "weight": "0.5",
    "extension_attributes": {
        "category_links": [
            {
                "position": 0,
                "category_id": "11"
            },
            {
                "position": 1,
                "category_id": "12"
            },
            {
                "position": 2,
                "category_id": "16"
            }
        ],
        "stock_item": {
            "qty": "10",
            "is_in_stock": true
        }
    },
    "custom_attributes": [
        {
            "attribute_code": "description",
            "value": "The Champ Tee keeps you cool and dry while you do your thing. Let everyone know who you are by adding your name on the back for only $10."
        },
        {
            "attribute_code": "tax_class_id",
            "value": "2"
        },
        {
            "attribute_code": "material",
            "value": "148"
        },
        {
            "attribute_code": "pattern",
            "value": "196"
        },
        {
            "attribute_code": "color",
            "value": "52"
        },
        {
            "attribute_code": "size",
            "value": "168"
        }
    ]
  }
}

You can use this example and then define the other options by tweaking the relevant attributes (color, size, etc). Then you create the options for the configurable product, or parent, itself.

    POST <host>/rest/default/V1/configurable-products/MS-Champ/options


{
  "option": {
    "attribute_id": "141",
    "label": "Size",
    "position": 0,
    "is_use_default": true,
    "values": [
      {
        "value_index": 9
      }
    ]
  }
}

Then finally, place the simple products into place after the options have been created for them.

POST <host>/rest/default/V1/configurable-products/MS-Champ/child



{
  "childSku": "MS-Champ-S"
}

Good luck!

edit:

  1. As for creating attribute values I would refer to this question: Create product attribute with options in magento2 through rest API
  2. Yes, value_index is the attribute_set_id.
  3. attribute_id 141 in this example is size. You can use the following to pull attribute ids, this information is in the "Step 1 Plan your product" section of the link.

Get attribute sets.

GET <host>/rest/<store_code>/V1/eav/attribute-sets/list?
searchCriteria[filter_groups][0][filters][0][field]=attribute_set_name&
searchCriteria[filter_groups][0][filters][0][value]=Top&
searchCriteria[filter_groups][0][filters][0][condition_type]=eq

Get attributes by set.

GET <host>/rest/default/V1/products/attribute-sets/9/attributes

Get catalog categories.

GET <host>/rest/default/V1/categories?
searchCriteria[filter_groups][0][filters][0][field]=id&
searchCriteria[filter_groups][0][filters][0][value]=1&
searchCriteria[filter_groups][0][filters][0][condition_type]=gte

And this link should help you to get the actual values/options for the attributes by the attribute_code. Magento 2 rest api get product's attributes options

5
  • Thank you for taking the time to reply and taking the time to write the in-depth advice above. Sorry but I have another question and also apologies if I am missing something. How do I add the attribute values Red, Green, Small, Medium etc...? I'm guessing attribute code refers to the colours or sizes which have been added with another api call? In Option is value_index the value of the attribute_set_id you want to add the attribute Size to? Also where does attribute_id 141 refer to in option? I can't see any link in the previous example. Sorry for the multiple questions. Thank you.
    – user84548
    Nov 25, 2019 at 22:27
  • No problem, I went ahead and updated the post with some more information. I'm happy to answer further questions as best I can. If you are happy with the answer, I'd appreciate it if you mark it as accepted! Nov 26, 2019 at 14:44
  • Thank you for taking the time to reply and answering my questions. I have voted but it has shown the following text "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score.". Hopefully it will still add to your score. Once again, thank you very much for your help.
    – user84548
    Nov 27, 2019 at 14:43
  • You have to mark it as 'answered,' which is distinct from voting. Should appear as a green checkmark? And of course, I'm happy to help. Nov 27, 2019 at 15:21
  • I have clicked the above tick. Is that correct?
    – user84548
    Nov 30, 2019 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.