Categories: How ToMagento 2

How to add Custom Catalog Input Type Using Observer In Magento 2

Considering the available CMS’s available for eCommerce, Magento has still remained as the first pick for store owner whether it is small or large scale. Due to its flexible architecture, it allows us to customize the Magento according to our Business needs by extending the Native code. Magento owns different observers generally known as “listener”, that helps you to detect specific events and in response to that execute specific action using code. Compared to Magento 1, Magento 2 have the pretty well-integrated observer system.
Magento Custom Development is our favorite part where we used to create and play with code. Recently, we came across such a requirement where the customer wants to add his own Input type in Magento 2 backend. After working on the client’s requirement for some time, finally, we were ready with our code for adding Custom Catalog Input Type Using Observer In Magento 2.

In the first step, we need to create “events.xml” file at this path.
app\code\Vendor\Extension\etc\adminhtml\events.xml

<?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="attribute.type" instance="Vendor\Extension\Observer\AddAttributeType" />
    </event>
</config>

Now, in second step, we need to add custom option type using “Config.xml” file at below path.
app\code\Vendor\Extension\etc\config.xml

<?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>

In this third and last step, we need to create one more file to add our option to input type dropdown. For that purpose, we need to create “AddCustomAttributeType.php” file at this path.
app\code\Vendor\Extension\Observer\AddCustomAttributeType.php

<?php
namespace Vendor\Extension\Observer;
use Magento\Framework\Event\ObserverInterface;
class AddAttributeType implements ObserverInterface
{
    
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        
        try {
            $response = $observer->getEvent()->getResponse();
            $types = $response->getTypes();
            $types[] = [
                'value' => 'custom',
                'label' => __('Custom_Attribute')
            ];
            $response->setTypes($types);
            return $this;
        } catch(\Exception $e) {
              echo $e->getMessage();
        }
    }
}

That’s it. Simply clear cache and you are done with adding custom input type.
Let us know if you are facing an issue while implementing this code by commenting below.
Happy Coding!

Click to rate this post!
[Total: 4 Average: 4]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

3 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

4 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

5 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

7 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago