0

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

2 Answers 2

1

After creating new attribute(s) reset EAV cache with this method: \Magento\Eav\Model\Config::clear

Inside Magento\Eav\Model\Config there is a property $attributes which keeps preloaded attributes data.

2
  • Thanks MTP - this fixed it! Clearing the cache using: $this->cacheTypeList->cleanType('eav') as I tried initially did not work. However, using the clear function of the Eav Config class fixed it. Thanks so much I was stuck on this for ages.
    – Ben Dodson
    Feb 17, 2022 at 8:48
  • CleanType('eav') only clears Magento cache (which might be stored in files or Redis). I had a similar case when I wasted few hours on debugging because of this internal storage.
    – MTP
    Feb 17, 2022 at 12:33
0

You can use the above code to get attribute Id by attribute Code, Use the code to get Id as given below:

public function __construct(\Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository){
...
$this->attributeRepository = $attributeRepository;
...
}

/**
 * Get attribute
 *
 * @param string $attributeCode
 * @return \Magento\Catalog\Api\Data\ProductAttributeInterface
 */
public function getAttribute($attributeCode)
{
    return $this->attributeRepository->get($attributeCode)->getAttributeId();
}

This will return you the Id of attribute.

1
  • Thank you Shikhar. I am able to fetch the ID without issue for attributes already on the system. However, when one is added programatically with the code above, I get an error fetching it's ID directly after creating it. I get the same response trying to fetch the ID using the attribute repository or with eav setup factory.
    – Ben Dodson
    Feb 16, 2022 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.