0

I have created custom rest api to create/update attribute options by attribute_id.

Here is my webapi.xml file code.

 <route url="/V1/attribute/AttributeOption" method="POST">
 <service class="Vendor\Module\Api\AttributeInterface" method="AttributeOption"/> 
</route>

Api/AttributeInterface.php

<?php
namespace Vendor\Module\Api;
interface AttributeInterface
{
 /**
 * POST for attribute api
 * @param mixed $param
 * @return array
 */
 public function AttributeOption($params);
}

Model/Attribute.php

 <?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;
 protected $objectManager;
 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 AttributeOption($params) { 
 // looking for logic to create / update options from request
 }
}

Here is my json Request:

 {
"params":{ 
 "Type": "create",
 "attribute_id" : "159",
 "OptionValues": [ 
 { 
 "OptionId": "01", 
 "OptionName": "Test" 
 },
 { 
 "OptionId": "Null",
 "OptionName" : "Test2"
 } 
 ] 
 }
}

I need to create the option for particular attribute by reading the above json,

if OptionId = null i need to create the new option otherwise

I need to update the label for option_id.

Can anyone help me with this please.

asked Jan 22, 2019 at 16:20

1 Answer 1

1

Try like below:

$OptionValues = json_decode('{"params":{"Type": "create", "attribute_id" : "159", "OptionValues": [{ "OptionId": "01", "OptionName": "Test"}, { "OptionId": "Null", "OptionName" : "Test2"}]}}');
foreach($OptionValues->params->OptionValues as $optValue){
 $optionVal = $option->OptionName;
 if($option->OptionId == NULL){
 //Create Code
 }
 else{
 //Update Code
 }
}

Now update your code according to your requirement.

answered Jan 23, 2019 at 8:37
2
  • hi @sukumar, is it possible to update me the code for attribute option create/update Commented Jan 23, 2019 at 8:56
  • hi @Sukumar, can you help me on this issue?magento.stackexchange.com/questions/274520/… Commented May 15, 2019 at 7:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.