I am creating an importer which will add products from an API to our Magento site. Part of the requirements is to programatically create an attribute if it does not yet exist on the Magento side. Unfortunately, when trying to retrieve the ID of attributes created this way... I get:
"The attribute with a "XXX" attributeCode doesn't exist. Verify the attribute and try again. "
This is despite said attribute appearing in admin dash with the correct attributeCode.
I have code working to create an attribute:
$eavSetup->addAttribute(
$productEntity,
$attName,
[
'type' => 'int',
'label' => $attName,
'input' => 'text',
'source' => '',
'required' => false,
'filterable' => false,
'visible_on_front' => true,
'used_in_product_listing' => true,
'attribute_group_name' => 'Characteristics',
'attribute_set_name' => 'Sandvik',
'backend' => '',
'is_user_defined' => 1
]
);
echo ("Added attribute");
$this->cacheTypeList->cleanType('eav');
$attId = $this->getAttId($productEntity, $attName);
$eavSetup->addAttributeToGroup($productEntity, 317, 4241, $attId);
The getAttId function used is as follows:
private function getAttId($productEntity, $attName) {
try {
$attributeId = $this->eavAttribute->getIdByCode($productEntity, $attName);
return $attributeId;
}
catch (Error $e) {
return null;
}
}
As you can see I am already clearing the eav cache in between creating the attribute and fetching it's ID.
Any help would be greatly appreciated