Magento – Custom text field for configurable product Magento 2

magento2

I am very new in Magento and from last few days I am trying to figure out how to add a Custom text field in the frontend for configurable product. I tried to do that but text-field text area is not showing up in the configurable product list of attributes. I can add drop down only.

I am looking for something that for example a customer want to buy a t-shirt and the text inside the t-shirt will be configurable from frontend product view page.

For example : custom text cost $5. There will be a checkbox in the product view page. When the customer click on it a text box will appear where he can add his text for the t-shirt. The $5 will automatically add with the checkout price.

I hope I am able to make you understand what I am trying to do. But, search a lot to figure it out but failed.

Thanks

Best Answer

Check out this example it might help you

Step 1 - Create a file as Mypackage/Mymodule/Setup/InstallData.php and add the following code then

    namespace Mypackage\Mymodule\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
    {

    /**
     * Customer setup factory
     *
     * @var \Magento\Customer\Setup\CustomerSetupFactory
     */
    private $_eavSetupFactory;

    /**
     * Init
     *
     * @param \Magento\Customer\Setup\CustomerSetupFactory
     * $customerSetupFactory
     */
    public function __construct(
        \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
    ) {
        $this->_eavSetupFactory = $eavSetupFactory;
    }

    /**
     * Installs DB schema for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->_eavSetupFactory->create([
            'setup' => $setup
        ]);

        /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
        $eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY,
            'is_feature', [
                'group'                   => 'Product Details',
                'type'                    => 'int',
                'backend'                 => '',
                'frontend'                => '',
                'label'                   => 'Is Feature',
                'input'                   => 'select',
                'source'                  =>
                    'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                'visible'                 => true,
                'default'                 => '0',
                'frontend'                => '',
                'unique'                  => false,
                'note'                    => '',
                'required'                => false,
                'sort_order'              => '',
                'global'                  =>
                    \Magento\Eav\Model\Entity\Attribute\
                    ScopedAttributeInterface::SCOPE_GLOBAL,
                'used_in_product_listing' => true,
                'visible_on_front'        => true
            ]);
    }
}

Step 2 - Go to project root directory and Run the following commands :

sudo rm -rf var/di var/generation var/cache
sudo php bin/magento setup:upgrade
sudo php bin/magento setup:di:compile
sudo php bin/magento clear:cache
Related Topic