1

I am trying to save some values in my attribute by programmatically. My attribute name is storage. type is dropdown I am trying to save values like 32GB or 16GB this values do not saved but if I used 'Black' or 'White' values ,it saved this kind of data

I am using Magento 2.3.4 Here is code

$optionValue = '32GB';
$attributeCode= 'storage';
    try{
                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                $entityType = 'catalog_product';
                $attributeInfo = $objectManager->get(\Magento\Eav\Model\Entity\Attribute::class)
                                     ->loadByCode($entityType, $attributeCode);
    
    
              $attributeFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Eav\Attribute');
                $storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
              $attributeId = $attributeInfo->getAttributeId();
              $attribute_arr = [$optionValue];
    
              $option = array();
              $option['attribute_id'] = $attributeId;
              foreach($attribute_arr as $key=>$value){
                  $option['value'][$value][0]=$value;
                  foreach($storeManager as $store){
                      $option['value'][$value][$store->getId()] = $value;
                  }
              }
             
              if ($option) {
                  $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                $eavSetupFactory = $objectManager->create('\Magento\Eav\Setup\EavSetup');
                $eavSetupFactory->addAttributeOption($option);
              }
            }catch(Exception $e){
              echo $e->getMessage();
            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.