Hello Magento Friends,

In today’s blog, we will learn about How to Create a Widget Programmatically in Magento 2.

Widgets are components that can be added to any CMS block in Magento 2. Widgets are used to add static or dynamic content to CMS blocks or CMS pages. Widgets can improve the user interface on the storefront. Widgets can help to simplify the browsing of stores for the users.

So let’s look at How to Create Widget Programmatically in Magento 2.

Steps to Create a Widget Programmatically in Magento 2:

Step 1: To declare a custom widget create the widget.xml file at the below path

app\code\Vendor\Extention\etc\widget.xml

Now, add the below code

Parameters allow adding of different fields to the widget.

Step 2: Now, create a PHP file inside the Block. Go to below path

app\code\Vendor\Extention\Block\Customwidget.php

And add the code as follows

Step 3: Now create a phtml file, that will show the data of phtml to the frontend. Navigate to the following path

app\code\Vendor\Extention\view\frontend\templates\customwidget.phtml

Add the following code

Conclusion:

This way you can create a widget programmatically in Magento 2. You can also Insert a Widget into Sidebar in Magento 2. If you have any doubts, let me know through the comment section.

Share the article and let your friends learn about creating a widget in Magento 2.

Keep connected with us for more!

Happy Coding!

Click to rate this post!
[Total: 6 Average: 3.3]