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?

  • 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 Feb 19 at 9:40
  • @HelgeB I edited post and add extra information. – jpu Feb 20 at 14:59
  • what should $attr->value be in your code? In the snippet you pasted that is not defined – HelgeB Feb 20 at 15:34
  • $attr->value is same as 'value' => $option['value']['value'][1] = 'value'; – jpu Feb 22 at 7:19
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.

  • Thx for reply, I thinf I've found problem, please look. – jpu Feb 25 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 Feb 25 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');

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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