Magento2 Attributes – How to Update Attribute Option Programmatically attributescustom-attributesmagento2 How to update attribute option (update label for a store) programmatically? Best Answer Take a look at \Magento\Eav\Model\ResourceModel\Entity\Attribute::_saveStoreLabels(AbstractModel $object). We can see how to save store lables. As we can see, we can insert data into table eav_attribute_label directly. $bind = ['attribute_id' => $object->getId(), 'store_id' => $storeId, 'value' => $label]; $connection->insert($this->getTable('eav_attribute_label'), $bind); Related SolutionsMagento – Get option Value from Option label for product/store Please use the following code to get the value from label $attribute_f = Mage::getModel('eav/config')->getAttribute('catalog_product', 'atr_name'); foreach ( $attribute_f->getSource()->getAllOptions(true, true) as $opt_menuf) { $f_attribute[$opt_menuf['value']] = $opt_menuf['label']; } Magento – How Magento2 add attribute option programmatically (not in setup) use Magento\Eav\Setup\EavSetupFactory; use Magento\Store\Model\StoreManagerInterface; declare: protected $_eavSetupFactory; constructor : public function __construct( \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attributeFactory, \Magento\Framework\ObjectManagerInterface $objectmanager, ModuleDataSetupInterface $setup, \Magento\Catalog\Model\ProductFactory $productloader ) { $this->_eavSetupFactory = $eavSetupFactory; $this->_storeManager = $storeManager; $this->_attributeFactory = $attributeFactory; $this->_objectManager = $objectmanager; $this->setup = $setup; $this->_productloader = $productloader; } execute methode : public function execute(EventObserver $observer) { /** @var $brand \Ktpl\Brand\Model\Brand */ $brand = $observer->getEvent()->getBrand(); $option_id = ""; $data = []; $attribute_arr = [$brand['brand_id'] => $brand['brand_title']]; $optionTable = $this->setup->getTable('eav_attribute_option'); $attributeInfo=$this->_attributeFactory->getCollection() ->addFieldToFilter('attribute_code',['eq'=>"shop_by_brand"]) ->getFirstItem(); $attribute_id = $attributeInfo->getAttributeId(); $eavAttribute = $this->_objectManager->create('Magento\Eav\Model\Config'); $option=array(); $option['attribute_id'] = $attributeInfo->getAttributeId(); $option['value'] = array(0=>array()); // 0 means "new option_id", other values like "14" means "update option_id=14" - this array index is casted to integer $storeManager = $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface'); $stores = $storeManager->getStores(); $storeArray[0] = "All Store Views"; foreach ($stores as $store) { $storeArray[$store->getId()] = $store->getName(); } if (empty($brand['optionId'])) { foreach($attribute_arr as $key => $value){ $option['value'][0][0]=$value; foreach($storeArray as $storeKey => $store){ $option['value'][0][$storeKey] = $value; } } } else { foreach($attribute_arr as $key => $value){ foreach($storeArray as $storeKey => $store){ $option['value'][$brand['optionId']][$storeKey] = $value; } } } $eavSetup = $this->_eavSetupFactory->create(); $eavSetup->addAttributeOption($option) } Related TopicMagento – Magento 2: Set attribute dropdown option programmaticallyMagento2 – Get Attribute Option Label by Attribute Option IDMagento – Magneto2 : Add color swatch attribute option with colorcode value programmaticallyMagento2 – Update Attribute Option Label with ID
Best Answer
Take a look at
\Magento\Eav\Model\ResourceModel\Entity\Attribute::_saveStoreLabels(AbstractModel $object)
. We can see how to save store lables.As we can see, we can insert data into table
eav_attribute_label
directly.