Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How to Create a Custom Widget in Magento 2

Vinh Jacker | 12-18-2024

Magento 2 Create Widget

Widget is a greater tool to insert or edit content into a CMS Page Block or Page, so I am so happy to bring you Create Widget in Magento 2 topic. Reading the post will help you understand: “What are the widgets in Magento 2?” and “How to use them on Magento 2 store”. With this simple instruction, it is easy to create and customize new custom widgets in Magento 2.

What is the widget in Magento 2?

Firstly, we all will get to know the definition of Widget in Magento 2.

Widgets are the powerful functionalities in Magento 2 configuration. As a store admin, you can take the advantage of the widget to improve the storefront under the lively interface. The widgets allow showing static information or dynamic content to CMS pages or blocks. You can add widgets in any CMS block of Magento 2 to display such content or elements, for example:

  • Dynamic product data
  • Dynamic lists of the recently viewed products
  • Promotional banners
  • Interactive navigation elements and action blocks
  • Dynamic flash elements that are inserted in content pages

The customization of the widgets in Magento 2 is similar to an optimized front-end extension with a simple module. It’s hassle-free to create a Magento 2 widget because it is the same as when you create a HelloWorld excepting from some additional files. Hence, although you don’t have any experience in programming, it is easy to improve your storefront with the dynamic blocks of content.

Here are available default widgets:

  • CMS Static Block
  • CMS Page Link
  • Recently Compared Products
  • Recently Viewed Products
  • Catalog Products List
  • Catalog Product Link
  • Catalog New Products List
  • Catalog Category Link

With this guide below, you can create your own widget in Magento 2 easily and quickly.

4 steps to creating a widget in Magento 2

  • Step 1: Declare widget
  • Step 2: Create a widget template file
  • Step 3: Create widget Block class
  • Step 4: Flush cache and posts

Step 1: Declare widget widget.xml

Create a file etc/widget.xml with the following content

<?xml version="1.0" ?>
<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:helloworld:Magento_Widget:etc/widget.xsd">
	<widget class="Mageplaza\HelloWorld\Block\Widget\Posts" id="mageplaza_helloworld_posts">
		<label>Blog Posts</label>
		<description>Posts</description>
		<parameters>
			<parameter name="posts" sort_order="10" visible="true" xsi:type="text">
				<label>Custom Posts Label</label>
			</parameter>
		</parameters>
	</widget>
</widgets>

Step 2: Create a widget template file in Magento 2

File: view/frontend/templates/widget/posts.phtml

<?php if($block->getData('posts')): ?>
	<h2 class='posts'><?php echo $block->getData('posts'); ?></h2>
	<p>This is sample widget. Perform your code here.</p>
<?php endif; ?>

Step 3: Create widget Block class

Create block file: Block/Widget/Posts.php

<?php 
namespace Mageplaza\HelloWorld\Block\Widget;

use Magento\Framework\View\Element\Template;
use Magento\Widget\Block\BlockInterface; 
 
class Posts extends Template implements BlockInterface {

	protected $_template = "widget/posts.phtml";

}

Step 4: Flush cache and posts

Go to admin panel > Content > Pages > Home page > Edit

In Content tab, click on Insert Widget icon

Magento 2 insert widget

You will see the Blog posts in widget list

magento 2 widgets


Magento 2 extensions

Magento 2 extensions

Allow you to achieve more with your online store

Check it out!


Conclusion

Though it requires no high level of programming for the store admins to add features and interactive content to the website by widgets, they still need to understand how it works in Magento 2 to create a widget. This guide gets you through the process of utilizing widgets from the functionality standpoint to streamline your Magento 2 store. If you have any questions, let us know in the comment section. Thanks for reading.

Related Post

x
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services