magento 2: value not saved for attribute by programmatically
-
14-04-2021 - |
Question
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();
}
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange