I try to add options for size and color attributes in my importer module but I don't how :
private function addOption($attributeCode, $value)
{
$ob = $this->_objectManager;
/* @var $m \Magento\Eav\Model\Entity\Attribute\OptionManagement */
$m = $this->optionManagement;
/* @var $option \Magento\Eav\Model\Entity\Attribute\Option */
$option = $this->attributeOption;
$option->setLabel($value);
$option->setValue($value);
$m->add(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE,
$attributeCode,
$option);
This report an error (I modified exception reporting on OptionMaganger.php
to Exception->message)
Cannot save attribute size Notice: Undefined index: delete in /var/www/html/magento2/vendor/magento/module-swatches/Model/Plugin/EavAttribute.php on line 177
- The OptionManagement and Option come from
_contstructor
- With OptionManagement I can retrieve the existing items, so should be ok..
setLabel()
and setValue()
are default, but I tried setData, load option instance and pass by OptionManagement->getItems
to add(...) "again",
but error still exists...
Any idea, how can I append EAV Options (swatches?) during the import process? (not in module setup)
Update:
Other way I can add option:
$attributeCode = 137; /* on size, 90 on color ... */
$languageValues[0]='Admin Label';
$languageValues[1]='Default Store Label - XXXXL';
$ob = $this->_objectManager;
private function addOption($attributeCode,$languageValues){
$ob = $this->_objectManager;
/* @var $attr \Magento\Eav\Model\Entity\Attribute */
$attr = $ob->create('\Magento\Eav\Model\Entity\Attribute');
$attr->load($attributeCode);
$option = [];
$option['value'][$languageValues[0]] = $languageValues;
$attr->addData(array('option' => $option));
$attr->save();
}
This way Magento2 can save an option to attribute, but I don't know what is the "official" way :)