2

I am adding new colors to the colors attribute via API. This works fine, but I want to send all my options in 1 request. Now I have to do multiple POST request to rest/V1/products/attributes/color/options

This is my code

protected $resourcePath = 'rest/V1/products/attributes';

foreach($options as $option) {
$optionData = [];

$optionData = [
    AttributeOptionInterface::LABEL => $option,
    AttributeOptionInterface::SORT_ORDER => 100,
    AttributeOptionInterface::IS_DEFAULT => false,
    AttributeOptionInterface::STORE_LABELS => [
        [
            AttributeOptionLabelInterface::LABEL => $option,
            AttributeOptionLabelInterface::STORE_ID => 1,
        ],
    ],
];

    $this->addColorAttribute($optionData);
}

public function addColorAttribute($optionData)
{
    $attributeCode = 'color';

    $serviceInfo = [
        'rest' => [
            'resourcePath' => $this->resourcePath . '/' . $attributeCode .'/options',
            'httpMethod' => Request::HTTP_METHOD_POST,
        ]
    ];

    return $this->client->request($serviceInfo,
        [
            'attributeCode' => $attributeCode,
            'option' => $optionData,
        ]
    );
}

How could I send all my $optionData to the API in 1 request so all the options are addded to the product attribute color ?

  • I may be overlooking it, but from what I can tell there is no support for bulk processing in the Magento2 API. – quickshiftin Jun 2 '16 at 18:54
1

Send them all into either put or Post at /V1/products/attributes/{attributeCode}

and it will create them,

FYI im having problems updating the labels later, which looks like its not possible via the API right now

1

You can directly create attribute with all attribute options by send the below json to this endpoint "POST /V1/products/attributes"

{
  "attribute": {
    "is_wysiwyg_enabled": false,
    "is_html_allowed_on_front": false,
    "used_for_sort_by": false,
    "is_filterable": true,
    "is_filterable_in_search": true,
    "is_used_in_grid": true,
    "is_visible_in_grid": false,
    "is_filterable_in_grid": true,
    "position": 0,
    "apply_to": [],
    "is_searchable": "1",
    "is_visible_in_advanced_search": "1",
    "is_comparable": "1",
    "is_used_for_promo_rules": "0",
    "is_visible_on_front": "0",
    "used_in_product_listing": "1",
    "is_visible": true,
    "scope": "global",
    "attribute_code": "att_with_options",
    "frontend_input": "select",
    "entity_type_id": "4",
    "is_required": false,
    "options": [
      {
        "label": "Option1"
      },
      {
        "label": "Option2"
      },
      {
        "label": "Option3"
      }
    ],
    "is_user_defined": true,
    "default_frontend_label": "Att Label",
    "frontend_labels": null,
    "backend_type": "int",
    "source_model": "Magento%5C%5CEav%5C%5CModel%5C%5CEntity%5C%5CAttribute%5C%5CSource%5C%5CTable",
    "default_value": "",
    "is_unique": "0"
  }
}

this code should work for you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.