I want to create a toggle button yes/no in the product or category admin side page. also eager to know its description of flow and binding process. I followed many tutorials but don't get the exact solution that I need. like the below images. enter image description here


4 Answers 4


Try this code, it works, tested:



namespace Vendor\ToggleAttribute\Setup\Patch\Data;

use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Eav\Model\Entity\Attribute\Source\Boolean;

class ToggleProductAttribute implements DataPatchInterface
     * Constants block
    const ENTITY_TYPE_ID = 'catalog_product';
    const ATTRIBUTE_CODE = 'somecode';

     * ModuleDataSetupInterface
     * @var ModuleDataSetupInterface
    private ModuleDataSetupInterface $moduleDataSetup;

     * EavSetupFactory
     * @var EavSetupFactory
    private EavSetupFactory $eavSetupFactory;

     * ToggleProductAttribute constructor.
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param EavSetupFactory          $eavSetupFactory
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        EavSetupFactory $eavSetupFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->eavSetupFactory = $eavSetupFactory;

     * {@inheritdoc}
    public function apply(): void
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
                'group' => 'General',
                'label' => 'Label',
                'type' => 'int',
                'input' => 'boolean',
                'backend' => '',
                'frontend' => '',
                'class' => '',
                'source' => Boolean::class,
                'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => true,
                'default' => '0',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => true,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => 'simple,configurable,grouped,virtual,bundle,downloadable'

     * {@inheritdoc}
    public static function getDependencies()
        return [];

     * {@inheritdoc}
    public function getAliases()
        return [];

For this 1. You will have to create new product attribute

Admin >> Stores >> Attributes >> Product >> Add Attributes

  1. Select "Yes/No" in Catalog Input Type for Store Owner
  2. Fill all your information and save that attribute.
  3. Got to Admin >> Stores >> Attributes >> Attribute Set and assign that attribute to your attribute set.
  4. Clear cache .

After all these steps this attribute will show on your product page

  • thanks to all .... can you guys please have a complete set of module
    – irshad
    Commented Nov 20, 2019 at 13:28
  • No need to write any code. Please use above steps and it will create it automatically
    – DJ Dev
    Commented Nov 20, 2019 at 13:38
  • DJ Dev@: hello I created a toggle button for the new shipment method but the issue is button yes/no function not able to enable/disable module function. please refer magento.stackexchange.com/questions/296371/…
    – irshad
    Commented Nov 23, 2019 at 9:21

First Create Attribute for Category and then add below code in category_form.xml

    <field name="your-attribute-code" sortOrder="335" formElement="checkbox">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="source" xsi:type="string">category</item>
                <item name="default" xsi:type="number">0</item>
                <rule name="required-entry" xsi:type="boolean">false</rule>
            <label translate="true">your-attribute-label</label>
                        <map name="false" xsi:type="string">0</map>
                        <map name="true" xsi:type="string">1</map>

here I created a new payment method and want to change the current dropdown option into toggle yes/no button. please check the below code and images. Packt/Shipme/etc/adminhtml/system.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        <section id="carriers"><group id="shipme" translate="label" type="text" sortOrder="50"
                                      showInDefault="1" showInWebsite="1" showInStore="1">
            <field id="active" translate="label" type="select"
                   sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0">
            <field id="name" translate="label" type="text" sortOrder="20"
                   showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Method Name</label>
            <field id="title" translate="label" type="text" sortOrder="20"
                   showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Method Title</label>
            <field id="express_enabled" translate="label" type="select"
                   sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="0"><label>Enable express</label>
            <field id="express_title" translate="label" type="text"
                   sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Title express</label>
            <field id="express_price" translate="label" type="text"
                   sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Price express</label>
            <field id="business_enabled" translate="label" type="select"
                   sortOrder="60" showInDefault="1" showInWebsite="1" showInStore="0">
                <label>Enable business</label>
            <field id="business_title" translate="label" type="text"
                   sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Title business</label>
            <field id="business_price" translate="label" type="text"
                   sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Price business</label>
            <field id="specificerrmsg" translate="label" type="textarea"
                   sortOrder="90" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Displayed Error Message</label>
            <field id="sallowspecific" translate="label" type="select"
                sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="0">
                <label>Ship to Applicable Countries</label>
            <field id="specificcountry" translate="label" type="multiselect"
                sortOrder="110" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Ship to Specific Countries</label>

enter image description here

that above images picture i want to change into toggle yes/no button. like below images. enter image description here

  • have you created? or do you find an answer Commented Jun 2, 2022 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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