Closed
Description
Preconditions
When creating product attribute and their options dynamically magento does not validate the data before inserting it into the database,
- Magento2.2.3
Steps to reproduce
- Create a product eav attribute dynamically using class (Magento\Catalog\Model\ResourceModel\Eav\Attribute) and set the entity type Id to the type id of catalog_product (See sample code below) (eg: attribute code = colors)
- generate a list of possible options and then add those options into that product EAV attribute by using (Magento\Eav\Setup\EavSetup -> AddOption()) (optionarray = 'Black, Blue, White')
- Generate some more options (pick some of those which are already existed) and add then add them into the product attribute by following the same step describe in step 2. ('White, Green, Purple')
Expected result
- Magento should validate the option Array and check if any of the item already exist or not, If it exist then it should not insert that (Possible options need to be in the attribute code should be 'Black, Blue, White, Green, Purple')
Actual result
- Magento create duplicate option for the same store even the option already exists. Data in the current option array are ( 'Black, Blue, White, White, Green, Purple')) check the repetative entry for the option value 'White.'
here are the sample code:
For step 1:
Inject the EaV Attribute Factory in your constructor and perform this action
$attributeData = [
'attribute_code' => $code,
'is_global' => 1,
'frontend_label' => $code,
'frontend_input' => 'select',
'default_value_text' => '',
'default_value_yesno' => 0,
'default_value_date' => '',
'default_value_textarea' => '',
'is_unique' => 0,
'apply_to' => 0,
'is_required' => 0,
'is_configurable' => 1,
'is_searchable' => 1,
'is_comparable' => 1,
'is_user_defined' => 1,
'is_visible_in_advanced_search' => 1,
'is_used_for_price_rules' => 0,
'is_wysiwyg_enabled' => 0,
'is_html_allowed_on_front' => 1,
'is_visible_on_front' => 0,
'used_in_product_listing' => 0,
'used_for_sort_by' => 1,
'is_filterable' => 1,
'is_filterable_in_search' => 1,
'backend_type' => 'varchar'
];
/*** Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory */
$this->_eavAttributeFactory->create()
->addData($attributeData)
->setEntityTypeId($this->getEntityTypeId('catalog_product'))
->save();
Step 2:
$attribute_code = 'COLOURS';
$options = array('Black', 'Green');
/*** Magento\Eav\Setup\EavSetup */
$this->eavSetupFactory->create()
->addAttributeOption(
[
'values' => $options,
'attribute_id' => $this->getAttributeIdbyCode($attribute_code) := '4'
]
);
Step 3:
$attribute_code = 'COLOURS';
$options = array('Black', 'White');
/*** Magento\Eav\Setup\EavSetup */
$this->eavSetupFactory->create()
->addAttributeOption(
[
'values' => $options,
'attribute_id' => $this->getAttributeIdbyCode($attribute_code) := '4'
]
);
Metadata
Metadata
Assignees
Labels
The issue has been fixed in 2.3 release lineGate 2 Passed. Manual verification of the issue description passedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedGate 1 Passed. Automatic verification of issue format passedGate 4. Acknowledged. Issue is added to backlog and ready for developmentIndicates that Pull Request has been created to fix issueThe issue has been reproduced on latest 2.2 releaseThe issue has been reproduced on latest 2.3 release