Entity Attributes
Get product attribute by code:
$attribute = $this->magentoSystemRepository->getEntityTypes()->getEntityTypeByCode(Magento2EntityType::TYPE_CATALOG_PRODUCT)->getAttributes()->getByCode($magentoAttributeCode);');
For select and multiselect options you can set the attribute value of an entity in 2 ways:
By directly passing the code or id:
$entity->setAttributeValue($attributeCode, $value)
This way Magento will try to find an option with either the code or id equal to the value you pass. This can give an issue when the value is an integer as well and there are values that match both on value as code.
For example if you have an attribute "diameter" with following options:
Id | Admin value |
---|---|
... | ... |
20 | 18 |
21 | 19 |
22 | 20 |
... | ... |
So when passing "20" to "setAttributeValue" Attlaz will be unable to determine the correct value and will throw an error. Starting from version 1.2.0 of the adapter you can pass a wrapper object "Attlaz\Adapter\Magento2\Model\Magento2SelectAttributeValue" to the method. With the constructor of this object you can pass the value and the type of that value (either code or id).
If we want to pass size with value 18 in the above example you could do (code is the default type):
$entity->setAttributeValue("diameter", new \Attlaz\Adapter\Magento2\Model\Magento2SelectAttributeValue(18));
Or
$entity->setAttributeValue("diameter", new \Attlaz\Adapter\Magento2\Model\Magento2SelectAttributeValue(20, Magento2SelectAttributeValue::TYPE_ID));