Magento2 : How to update attribute option programmatically?
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);
I was able to update product attribute option label by using ProductAttributeRepositoryInterface
interface.
/**
* @var \Magento\Catalog\Api\ProductAttributeRepositoryInterface
*/
private $productAttributeRepository;
public function updateOption($attributeCode, $optionId, $optionLabel) {
$attr = $this->productAttributeRepository->get($attributeCode);
$options = $attr->getOptions();
$sortOrder = 0;
foreach ($options as $option) {
if ($option->getValue() == $optionId) {
$option->setLabel($optionLabel);
$option->setSortOrder($sortOrder);
$attr->setOptions([$option]);
$this->productAttributeRepository->save($attr);
break;
}
$sortOrder++;
}
}
You can use setStoreLabels
method additionally or instead of setLabel
if you want to update labels for several store views.
Also note that the save($attr)
is implemented in a way that all option values are deleted and inserted again (eav_attribute_option_value
table). And this is what happens when you edit option values via admin page, so I think it's not a real problem.
Another issue here is that Magento doesn't load option's SortOrder parameter (on ver. 2.1.9), so we have to manually calculate the option's sort order. You can also use \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory
to load actual SortOrder value.
/**
* @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory
*/
private $optionCollectionFactory;
...
$collection = $this->optionCollectionFactory->create()->setIdFilter($optionId)->load();
foreach ($collection as $optionResourceModel) {
$sortOrder = $optionResourceModel->getSortOrder();
}
If You Want to Add Attribute with InstallerScript this will Work perfectly.
In Your [Vedor]\[Module]\Setup\InstallData.php
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'myattribute',
[
'type' => 'text',
'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
'frontend' => '',
'label' => 'My Attribute',
'input' => 'multiselect',
'class' => '',
// instead values change your key as value and add your option like below with in `option_1` array put key as store_id and label as value
'option' => ['value' =>
[
'option_1'=>[
0=>123, // here 0 is store id and 123 is value
1=>456,
13=>4444,
14=>12121,
15=>1212,
16=>123
],
'option_2'=>[
0=>789,
1=>546,
13=>2211,
14=>32123,
15=>123123123,
16=>5152
],
],
'order'=>//Here We can Set Sort Order For Each Value.
[
'option_1'=>1,
'option_2'=>2
]
],
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => true,
'wysiwyg_enabled' => true,
'unique' => false,
'apply_to' => ''
]
);