Skip to content

Magento2 Attribute option does not validate for existing records before insert #16852

Closed
@zeeshan2523806

Description

@zeeshan2523806

Preconditions

When creating product attribute and their options dynamically magento does not validate the data before inserting it into the database,

  1. Magento2.2.3

Steps to reproduce

  1. 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)
  2. 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')
  3. 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

  1. 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

  1. 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.'

image

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

Component: EavFixed in 2.3.xThe issue has been fixed in 2.3 release lineIssue: Clear DescriptionGate 2 Passed. Manual verification of the issue description passedIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentProgress: PR CreatedIndicates that Pull Request has been created to fix issueReproduced on 2.2.xThe issue has been reproduced on latest 2.2 releaseReproduced on 2.3.xThe issue has been reproduced on latest 2.3 releasehelp wanted

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions