3

I'm new in Magento 2, I want to add an attribute using REST api. Is it possible? I use the follow code for products:

<?php 
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);


$url = "http://mywebsite/";
$token_url = $url."rest/V1/integration/admin/token";
$product_url = $url. "rest/V1/products";
$username = "myUserName";
$password = "myPassword";



$ch = curl_init();
$data = array("username" => $username, "password" => $password);
$data_string = json_encode($data);
$ch = curl_init($token_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
    );
$token = curl_exec($ch);
$adminToken=  json_decode($token);


$sampleProductData = array(
        'sku'               => 'config_product-small-red',
        'name'              => 'Simple Product ' . uniqid(),
        'visibility'        => 4, /*'catalog',*/
        'type_id'           => 'simple',
        'price'             => 99.95,
        'status'            => 1,
        'attribute_set_id'  => 4,
        'weight'            => 1,
        'custom_attributes' => array(
            array( 'attribute_code' => 'category_ids', 'value' => ["42","41","32"] ),
            array( 'attribute_code' => 'description', 'value' => 'Simple Description' ),
            array( 'attribute_code' => 'short_description', 'value' => 'Simple Short Description' ),
          array( 'attribute_code' => 'size', 'value' => '5' ), //5=small
          array( 'attribute_code' => 'color', 'value' => '8' ), //8-red
            ),
    'extension_attributes' => array(
        "stock_item"=>array(
            "qty"=>10,
            "is_in_stock"=>true,
        ),

    )
    );
$productData = json_encode(array('product' => $sampleProductData));
$setHaders = array('Content-Type:application/json','Authorization:Bearer '.$adminToken);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $product_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $productData);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $setHaders);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);

?>

How can I do the same for product attributes?

1 Answer 1

2

We can use POST : /V1/products/attributes

http://devdocs.magento.com/swagger/ > catalogProductAttributeRepositoryV1

For example body JSON:

{
  "attribute": {
    "is_wysiwyg_enabled": true,
    "is_html_allowed_on_front": true,
    "used_for_sort_by": true,
    "is_filterable": true,
    "is_filterable_in_search": true,
    "is_used_in_grid": true,
    "is_visible_in_grid": true,
    "is_filterable_in_grid": true,
    "position": 0,
    "apply_to": [
      "string"
    ],
    "is_searchable": "string",
    "is_visible_in_advanced_search": "string",
    "is_comparable": "string",
    "is_used_for_promo_rules": "string",
    "is_visible_on_front": "string",
    "used_in_product_listing": "string",
    "is_visible": true,
    "scope": "string",
    "extension_attributes": {},
    "attribute_id": 0,
    "attribute_code": "string",
    "frontend_input": "string",
    "entity_type_id": "string",
    "is_required": true,
    "options": [
      {
        "label": "string",
        "value": "string",
        "sort_order": 0,
        "is_default": true,
        "store_labels": [
          {
            "store_id": 0,
            "label": "string"
          }
        ]
      }
    ],
    "is_user_defined": true,
    "default_frontend_label": "string",
    "frontend_labels": [
      {
        "store_id": 0,
        "label": "string"
      }
    ],
    "note": "string",
    "backend_type": "string",
    "backend_model": "string",
    "source_model": "string",
    "default_value": "string",
    "is_unique": "string",
    "frontend_class": "string",
    "validation_rules": [
      {
        "key": "string",
        "value": "string"
      }
    ],
    "custom_attributes": [
      {
        "attribute_code": "string",
        "value": "string"
      }
    ]
  }
}

You also can read more here: https://magento.stackexchange.com/a/149927/33057

7
  • Thank you Khoa TruongDinh but 1 - where I have to put the above json in my php code? 2 - what are the mandatory labels ? Commented Jan 4, 2017 at 8:44
  • Which Tool you use for testing API? Commented Jan 4, 2017 at 8:45
  • I am using postman for chrome but I execute the the above code from an remote server and working well. So I'm trying to customize the code in order to be able to add the attribute remotely. Commented Jan 5, 2017 at 6:10
  • So, you need a PHP data array? Commented Jan 5, 2017 at 6:12
  • Yes please .... Commented Jan 5, 2017 at 7:04

Your Answer

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

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