Webkul
Services
Technologies Bagisto QloApps
iOS Android SAP BigCommerce Wix
Trusted by
Samsung Intel Toyota Walmart Disney Nokia Nestlé Decathlon
See all customers
Back to Top
DEV

Add Custom Catalog Input Type in Magento 2

Add Custom Catalog Input Type in Magento 2

Today we will see how to add custom catalog input type in Magento 2. An event adminhtml_product_attribute_types is dispatched when the catalog input types are displayed. This event can be used to add custom catalog input types.

19_feb_paul_blog

First in etc/config.xml file inside your module

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <general>
            <validator_data>
                <input_types>
                    <custom>custom</custom>
                </input_types>
            </validator_data>
        </general>
    </default>
</config>

Next in etc/adminhtml/events.xml file inside your module

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="adminhtml_product_attribute_types">
        <observer name="webkul.custom.attribute"
                  instance="Webkul\CustomAttributeType\Observer\AddCustomAttributeType" />
    </event>
</config>

At last in your observer file path mentioned above

<?php
namespace Webkul\CustomAttributeType\Observer;

use Magento\Framework\Event\ObserverInterface;

class AddCustomAttributeType implements ObserverInterface
{
    /**
     * Add new attribute type to manage attributes interface
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return $this
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // adminhtml_product_attribute_types
        try {
            $response = $observer->getEvent()->getResponse();
            $types = $response->getTypes();
            $types[] = [
                'value' => 'custom',
                'label' => __('Custom')
            ];
            $response->setTypes($types);
            return $this;
        } catch(\Exception $e) {
            echo $e->getMessage();
        }
    }
}

This will add a new catalog input type.

For more details you can see this magento file

Magento/Weee/Observer/AddWeeeTaxAttributeTypeObserver.php

That’s it.

Happy coding

. . .

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