Read More
Read More
Menu Close

    Create Configurable attribute in magento2

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

    Step 1 : Create constructor in your controller file.

    /**
     * @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 attribute data in execute method.

    $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();

    So In this way you can create your own custom attribute for configurable products.

    Searching for an experienced
    Magento 2 Company ?
    Read More
    . . .
    Discuss on Helpdesk

    Leave a Comment

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


    Be the first to comment.

    Back to Top