Site icon dev98

Creating Configurable Products

Basically there are different ways for creating Configurable Products in Magento2 (like in Magento1).
You can create it using the Admin Panel, using a Setup\InstallData or Setup\UpgradeData class or during a custom import.

ADDING THE ATTRIBUTE

Let’s assume we are creating our attribute using the Setup\UpgradeData class in our module.
The code we might initially create could look something like the following:

    protected function addAttributeDiameter()
    {
        $this->eavSetup->addAttribute(
            ProductModel::ENTITY,
            'dev98_diameter',
            [
                'type' => 'int',
                'backend' => '',
                'frontend' => '',
                'label' => 'Diameter',
                'input' => 'select',
                'class' => '',
                'source' => '',
                'global' => 1,
                'visible' => true,
                'required' => false,
                'user_defined' => true,
                'default' => null,
                'searchable' => true,
                'filterable' => true,
                'comparable' => true,
                'visible_on_front' => true,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => 'simple,configurable',
                'system' => 1,
                'group' => 'General',
            ]
        );

        // Add Options
        $attributeId = $this->eavSetup->getAttributeId(
            ProductModel::ENTITY, 'dev98_diameter'
        );
        $options = [
            'attribute_id' => $attributeId,
            'values' => ["10", "20", "30", "40", "50", "60", "70", "80", "90",],
        ];
        $this->eavSetup->addAttributeOption($options);
    }

The above code will create an attribute with the attribute-code ‘dev98_diameter’, which is available for simple and configurable products, within the group ‘General’ and is searchable, filterable etc… Futhermore we are adding some attribute options as well.

CREATING A CONFIGURABLE PRODUCT

Now if you add this attribute to an attribute-set and try creating a configurable product for this attribute it won’t work out as you might expect.
You will not see the attribute in the overlay ‘Create Product Configurations’  where you choose the attributes you want your product to be configurable with.

In Magento 2 the Admin Edit Form for Products changed quite a bit.

The most important change is, that the type of a product e.g. simple, configurable, etc. is no longer selected but determined automatically when saving the product.

So the product type you select using the DropDown in the Admin Product Grid is obsolete and not really used.

IMPROVING OUR UPDATEDATA CLASS

Knowing that the product is virtual at the moment of creating our configurable product, we have to add to type ‘virtual’ to the apply_to parameter in our ‘Setup\UpgradeData’ class.

       $this->eavSetup->addAttribute(
            ProductModel::ENTITY,
            'dev98_diameter',
            [
                'type' => 'int',
                'backend' => '',
                'frontend' => '',
                'label' => 'Diameter',
                'input' => 'select',
                'class' => '',
                'source' => '',
                'global' => 1,
                'visible' => true,
                'required' => false,
                'user_defined' => true,
                'default' => null,
                'searchable' => true,
                'filterable' => true,
                'comparable' => true,
                'visible_on_front' => true,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => 'simple,configurable,virtual', // Add virtual type here
                'system' => 1,
                'group' => 'General',
            ]
        );

After running the ‘setup:upgrade’ command, flushing the cache and reloading our Admin Product Edit Form we can now see the attribute ‘dev98_diameter’ in the ‘Create Product Configurations’ overlay.

From here-on we can create configurable products with our attribute as we please.

SUMMARY

To sum it up in short, you have to make your product attribute apply to type virtual to create configurable products.
At least when you are creating you attributes programmatically like in InstallData/ UpgradeData or using a attributes import.

Exit mobile version