1

When creating attribute I notice that in magento2 no option for the select product type.

i.e I want my attribute will available for configurable product only.

But that attribute is visible for all simple product too.

Even I create a custom attribute when creating adding a configurable product.

When adding a simple product why that attribute is visible?

* How to create an attribute for configurable product only?

8

Yes , You can create specific attribute for specific product type programmatically.

Below i have created Boolean type of attribute for configurable product only.

You need to pass 'apply_to' parameter and its value as configurable

try below code in your InstallData.php file :

<?php

namespace Vendor\ModuleName\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'attribute_for_config',
            [
                'group' => 'Product Details',
                'type' => 'varchar',
                'backend' => '',
                'frontend' => '',
                'sort_order' => 50,
                'label' => 'Can be wrapped?',
                'input' => 'boolean',
                'class' => '',
                'source' => '',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => '',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to'=>'configurable'
            ]
        );
    }
}

Note : Same way if you needed this for all types of product you need to pass all values in same parameters like : 'apply_to'=>'simple,configurable,bundle,grouped'

  • k, thanks. by the way can i edit an attribute if already create ? – Rahul Chaurasia Jul 8 '17 at 10:56
  • Yes you can edit/update attribute by create UpgradeData.php file. refer this link for more reference about updatescript - coolryan.com/magento/2016/02/09/… – Manthan Dave Jul 8 '17 at 11:03
  • 1 more question. I have to create the new module or it will work with module already installed? Because code will be write in installData.php ? or i have to write in upgradedata.php I was create a new module as mention in given above link. but module was not creating table in database. – Rahul Chaurasia Jul 8 '17 at 16:11
  • well we have created attribute for product , so there is no issue with the module which is installed. but still if you want to do it with your module then copy your installData file in your module and delete the one which we have created, make sense ? – Manthan Dave Jul 8 '17 at 16:28
  • as you said i wrote the code in InstallData.php but new attribute not added. Can you tell me exactly where i have to write above given code? I am okey if work when i write code in other module's file – Rahul Chaurasia Jul 8 '17 at 20:37
1

Next time use Mage2Gen https://mage2gen.com/

You can easily create a module with Mage2Gen which installs Product Attributes and if you configure the Advanced options you can specify the Allowed Product Types.

Sample Test Attribute

$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'test',
        [
            'type' => 'int',
            'backend' => '',
            'frontend' => '',
            'label' => 'Test',
            'input' => 'boolean',
            'class' => '',
            'source' => '',
            'global' => 1,
            'visible' => true,
            'required' => true,
            'user_defined' => true,
            'default' => null,
            'searchable' => false,
            'filterable' => false,
            'comparable' => false,
            'visible_on_front' => false,
            'used_in_product_listing' => false,
            'unique' => false,
            'apply_to' => 'configurable',
            'system' => 1,
            'group' => 'General',
            'option' => array('values' => array(""))
        ]
    );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.