Reading list Switch to dark mode

    Create Configurable attribute in magento2

    Updated 18 January 2023

    Customizable options for a product provide a way to offer customers a selection of options with a variety of text, selection, and date input types. All product types can contain customizable options.

    To create a Configurable attribute in magento2  please read the blog carefully –

    Here I am going to explain how to create custom attribute for configurable products. Like if you want to programatically create your own  custom attributes for configurable products then no need to worry about this,  you just need to follow some of the very easy steps and you can learn how to create configurable attributes just like that.
    So in this example I am going to create a configurable attribute “Fabric Color” with Red and Blue options as an example. For creating configurable attribute please follow the steps given below-

    Firstly, create a Controller File app / code / {vendor} / {module} / Controller / Adminhtml / {Method} / {Action.php}

    Step 1 : Create constructor in your controller file and add the required dependency classes as mentioned below :

    Searching for an experienced
    Magento 2 Company ?
    Find out More
    /**
     * @var \Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory
     */
    protected $_eavAttributeFactory;
    
    /**
     * @param Context                                                       $context
     * @param \Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory     $eavAttributeFactory
     */
    public function __construct(
        Context $context,
        \Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory $eavAttributeFactory
    ) 
    {
        $this->_eavAttributeFactory = $eavAttributeFactory;
        parent::__construct(
            $context
        );
    }

    Step 2 : Save the Custom attribute data in execute method of your controller.

    $attributeData = [
        'attribute_code' => 'fabric_color',
        'is_global' => 1,
        'frontend_label' => 'Fabric Color',
        'frontend_input' => 'select',
        'default_value_text' => '',
        'default_value_yesno' => 0,
        'default_value_date' => '',
        'default_value_textarea' => '',
        'is_unique' => 0,
        'apply_to' => 0,
        'is_required' => 1,
        'is_configurable' => 1,
        'is_searchable' => 0,
        'is_comparable' => 0,
        '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' => 0,
        'is_filterable' => 0,
        'is_filterable_in_search' => 0,
        'backend_type' => 'int',
        'option' => [
            'order' => [
                'option_0' => 1,
                'option_1' => 2
            ],
            'value' => [
                'option_0' => [
                    '0' => 'Admin Red',
                    '1' => 'Store Red'
                ],
                'option_1' => [
                    '0' => 'Admin Blue',
                    '1' => 'Store Blue'
                ]
            ]
        ],
        'default' => [
            '0' => 'option_0'
        ]
    ];
    $model = $this->_eavAttributeFactory->create();
    $model->addData($attributeData);
    $entityTypeID = $this->_objectManager->create('Magento\Eav\Model\Entity')
    ->setType('catalog_product')
    ->getTypeId();
    $model->setEntityTypeId($entityTypeID);
    $model->save();

    In the above code you may change  the key and values according to the condition that suits best for your desired goal.

    For example you may change the options in the select input according to your choice.

    So In this way with the help of the above provided steps  you will be able to create your own custom attribute for configurable products.

    Create Configurable attribute in Magento2

    You can also check:

    https://webkul.com/blog/add-custom-options-product-magento-2/

    https://docs.magento.com/user-guide/v2.3/catalog/product-create-configurable.html

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home