Magento – Create product attribute with options in magento2 through rest API

magento2product-attributerest apiwebapi

I have created a custom module to create product attribute with option thruogh rest Api.

Here is my code samples.

etc/webapi.xml

<route url="/V1/attribute/attributeCreate" method="POST">
    <service class="Vendor\Module\Api\AttributeInterface" method="AttributeOptionCreate"/>
    <resources>
        <resource ref="Vendor_Module::custom"/>
    </resources>
</route>

Api/AttributeInterface.php

namespace Vednor\Module\Api;

interface AttributeInterface
{
/**
 * POST for attribute api
 * @param mixed $param
 * @return array
 */

public function AttributeCreate($params);

/**
 * POST for attribute option api
 * @param mixed $param
 * @return array
 */

public function AttributeDelete($params);

}

etc/di.xml

 <?xml version="1.0"?>
  <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc /config.xsd">

<preference for="Vendor\Module\Api\AttributeInterface"
            type="Vendor\Module\Model\Attribute" />
</config>

Model/Attribute.php

  namespace Vendor\Module\Model;
  use Vendor\Module\Api\AttributeInterface;
  use Magento\Eav\Setup\EavSetupFactory;
  use Magento\Store\Model\StoreManagerInterface;
  class Attribute implements AttributeInterface
  {
   protected $_eavSetupFactory;
   protected $_storeManager;
   protected $_attributeFactory;
   protected $eavAttributeFactory;
   protected $attributeOptionManagement;
   protected $productFactory;

   private $productAttributeRepository;
    public function __construct(
    \Magento\Framework\ObjectManagerInterface $objectManager,
    \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attributeFactory,
    \Magento\Eav\Model\Entity\AttributeFactory $eavAttributeFactory,
    \Magento\Eav\Api\AttributeOptionManagementInterface $attributeOptionManagement,
    \Magento\Catalog\Model\ProductFactory $productFactory,
    \Magento\Catalog\Model\Product\Attribute\Repository $productAttributeRepository
) {
    $this->_objectManager = $objectManager;
    $this->_eavSetupFactory = $eavSetupFactory;
    $this->_storeManager = $storeManager;
    $this->_attributeFactory = $attributeFactory;
    $this->eavAttributeFactory = $eavAttributeFactory;
    $this->attributeOptionManagement = $attributeOptionManagement;
    $this->productFactory = $productFactory;        
    $this->productAttributeRepository = $productAttributeRepository;
}

public function AttributeCreate($params) {
    return 'Response: ' . json_encode($params); exit;
    $resultArr = array();
    $paramArr = array();
    $result =  json_encode($params);
    $resultSet = json_decode($result,true);
   }

 }

I have my json request like below

{
  "params":{  
     "Attribute":"brand",
      "OptionValues":{  
         "Value": "test1",
         "Value": "test2",
         "Value": "test3"
   }
  }
}

I am looking for logic to create attribute with option in my Model file.

I need to create attribute with the respective type, like dropdown, text and boolean.

As i checked finding code from installer only. can we create attributes from custom api like above.

Can anyone help me on this pls. Thanks in Advance

Best Answer

Try the below API End Point.

https://yourmagentohost.com/rest/V1/products/attributes/

Request Type POST

Example Request JSON for creating product attribute with dropdown values.

{
 "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": "brand",
   "frontend_input": "select",
   "entity_type_id": "4",
   "is_required": false,
   "options": [
    {
      "label": "test1"
    },
    {
      "label": "test2"
    },
    {
      "label": "test3"
    }
  ],
 "is_user_defined": true,
 "default_frontend_label": "Brand",
 "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"
 }
}
Related Topic