0

I'm writing cron module to import products (Magento 2). In this module I'm adding attribute, attribute value and assign this new attribute value to product. It sounds easy but new added attributes and it's values are not visible. For example when I add attribute option

$attribute = $this->_eavConfig->getAttribute('catalog_product', $code);      
$attributeId = $attribute->getAttributeId();

$option = array();
$option['attribute_id'] = $attributeId;
$option['value'][$attr->value][0] = 'value';
$option['value'][$attr->value][1] = 'value';
$this->_eavSetup->addAttributeOption($option);

and then try to find new added option

$options = $attribute->getSource()->getAllOptions(true);
foreach ($options as $option)
{
    if ($option['label'] == 'value') ...//new value missing
}

there is not new option value in $options table. I can get new option id directly from tables eav_attribute_option and eav_attribute_option_value. But It doesn't solve the problem because setting this option id to product wont work. "Product" also doesn't see this new attribute/option value. How can I force my script to refresh new attributes so they can be visible?

4
  • can you provide more code from where you add the attribute option? And are you useing magento 1 or magento 2? please add the corresponding tag.
    – HelgeB
    Commented Feb 19, 2019 at 9:40
  • @HelgeB I edited post and add extra information.
    – jpu
    Commented Feb 20, 2019 at 14:59
  • what should $attr->value be in your code? In the snippet you pasted that is not defined
    – HelgeB
    Commented Feb 20, 2019 at 15:34
  • $attr->value is same as 'value' => $option['value']['value'][1] = 'value';
    – jpu
    Commented Feb 22, 2019 at 7:19

3 Answers 3

0

I've found problem. Before I add attribute I'm checking if it exists:

$attribute = $eavConfig->getAttribute('catalog_product', $code);
if (!$attribute->getAttributeId())
{
$eavSetup->addAttribute(
    \Magento\Catalog\Model\Product::ENTITY,
    $code, ...
}

//get attribute after add
$attribute = $eavConfig->getAttribute('catalog_product', $code);
$attribute->getAttributeId() <---- this is null

but when I skip this existing checking:

$attribute = $eavConfig->getAttribute('catalog_product', $code);
if (!$attribute->getAttributeId()) 

getAttribute after addAttribute returns added attribute id ($attribute->getAttributeId()). So It look like first $eavConfig->getAttribute cause this problem. Any idea why?

0

The following code assumes that a product attribute with the attribute_code $code exists. For adding a new attribute please check some appropriate questions, e.g. Magento 2 - Create product attribute programmatically

For adding new attribute values it is better to use the values key in the option array. This is expects entries with key sortOrder and value label, while the approach you are using expects the optionId as key.

Change your code like this:

$attribute = $this->_eavConfig->getAttribute('catalog_product', $code);      
$attributeId = $attribute->getAttributeId();

$option = array();
$i=1; //sortOrder counter

$option['attribute_id'] = $attributeId;
//use the 'values' key to add new option values; that array contains the sort_order as key and the label as value
$option['values'][$i++] = 'value1';
$option['values'][$i++] = 'value2';
//...
$this->_eavSetup->addAttributeOption($option);

For better understanding of what'S happening there you can check the method addAttributeOption($option) in Magento\Eav\Setup\EavSetup.

I hope that helps. Feedback would be nice.

2
  • Thx for reply, I thinf I've found problem, please look.
    – jpu
    Commented Feb 25, 2019 at 19:56
  • It would be better to update the question than adding an answer, in that way it's easier to understand what happens. So what's the value of the variable $code? Does that attribute exists and has the correct type?
    – HelgeB
    Commented Feb 25, 2019 at 20:07
0

I think, I've finally figured out what is the problem. This is a some kind of "cache". When I inject eav in constructor

\Magento\Eav\Model\Config $eavConfig,
...
$this->_eavConfig = $eavConfig;

and then use this object (variable)

first some code for adding attributes values 
...
$attribute = $this->_eavConfig->getAttribute(....) 
//getting here attribute options doesn't return new option
$options = $attribute->getSource()->getAllOptions(true);

but when I create new eav obeject

$eavConfig = $this->_objectManager->create('\Magento\Eav\Model\Config');  
$attribute = $eavConfig->getAttribute('catalog_product', $code);
//getting here attribute options return also new added option
$options = $attribute->getSource()->getAllOptions(true);

So every time you have to create new object $eavConfig = $this->_objectManager->create('\Magento\Eav\Model\Config');

Some actualization

This is not the end of strange things. Before import I was getting all attributes

$eavAttribute = $this->_eavConfig->getAttribute('catalog_product', $code);

during save product this part of code (for new added attributes) caused error "attribute with attributecode does not exist". After changing to

$eavConfig = $this->_objectManager->create('\Magento\Eav\Model\Config');
$eavAttribute = $eavConfig->getAttribute('catalog_product', $code);

error disappears...

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.