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.