How To

How to add Radio button in Admin System Configuration of Magento 2

In Magento, System Configurations have always stood apart from an extension that controls & contains tons of options. It’s like the heart of the extension that allows the store owner to handle all features & functionality seamlessly. Till today, we have created 100+ extensions and we are always focused to create admin configurations simple but effective the way love store owners willing to have. Because at the end of the day, it made for a store owner to reduce their efforts to maintain the store and deliver great user experience towards their store customer.

In Magento 2, it allows the developer to add tons of elements to admin interface like text files, radio buttons, dropdowns, and multiple selects, simple fields, images and more. We used to play with these system controls to fulfill each requirement we get. Even, we have published tons of article on adding various elements to system configuration and here is another for adding Radio button in the Admin System Configuration.

To add a radio button control to system configuration, you simply need to add an element by modifying ‘System.xml‘ by adding below code.
app\code\Vendor\Extension\etc\Adminhtml\system.xml

<pre class="lang:default decode:true">
              <label>Horizontal Alignment</label>
              Vendor\Extension\Model\Config\Source\Radiobtn

</pre>

Now create another file and named it ‘Radiobtn.php‘ and paste your own logic.
app\code\Vendor\Extension\Model\Config\Source\Radiobtn.php

<strong>app\code\Vendor\Extension\Model\Config\Source\Radiobtn.php</strong>
<pre class="lang:default decode:true"><!--?php namespace Vendor\Extension\Model\Config\Source; class Radiobtn implements \Magento\Framework\Option\ArrayInterface { public function toOptionArray() { return [['value' =&gt; 'left', 'label' =&gt; __('Left')], ['value' =&gt; 'right', 'label' =&gt; __('Right')],];
 }
}
</pre>

Also, you can play &amp; manipulate with these codes according to your need of adding one or more radio button choices inside the Magento System Configuration.
Lastly, smash that stars if you love reading our blogs and comment down below if you are facing an issue while using this code.
Happy Coding!

Click to rate this post!
[Total: 13 Average: 3.5]
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

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

15 hours ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

16 hours ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

1 day ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

3 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We're thrilled to announce the release of Hyvä Themes 1.3.6 and 1.3.7! These latest updates…

4 days ago

How Modern E-Commerce Platforms Leverage Docker & Kubernetes for Scalability

Your e-commerce platform is surging - orders are rolling in, traffic spikes are becoming the…

5 days ago