2

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

7
  • Already there is core REST Api end point available to create attribute, why don't you use the below api end point "POST /V1/products/attributes"
    – mydons
    Commented Jan 10, 2019 at 7:38
  • I have written custom api, can we not do like this? Commented Jan 10, 2019 at 8:11
  • we can write custom api for something which is not available in magento. But for your use case magento has already provided an API. (Product attribute creation) which you can use it without writing a single line of code. Is there anything missing in the existing magento core REST API end point
    – mydons
    Commented Jan 10, 2019 at 8:38
  • @mydons, my json request is only that much, using that i need to create, I don't want to pass all data as request, so looking for custom api and created the endpoints, because as my response also custom one. can you pls provide me. Commented Jan 10, 2019 at 8:41
  • @mydons, can you provide me simple request json for creating attribute? i checked in swagger, there it is much data. Commented Jan 10, 2019 at 13:36

1 Answer 1

5

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"
 }
}
3
  • why can't we create from custom api?, actually I need to send custom response, so looking for custom code completely Commented Jan 10, 2019 at 17:18
  • You can based on the native API in your custom API and manage your custom response. It is totally possible. Commented Jan 14, 2019 at 11:13
  • The above solution which @mydons provided is perfect and you can add your custom attribute in extension_attribute without creating custom API@jafar :) Commented Jan 16, 2019 at 5:40

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.