{"id":2849,"date":"2016-03-02T10:31:23","date_gmt":"2016-03-02T05:01:23","guid":{"rendered":"http:\/\/www.magewallet.com\/?p=213"},"modified":"2020-03-13T16:08:17","modified_gmt":"2020-03-13T10:38:17","slug":"create-magento-2-widget","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/create-magento-2-widget\/","title":{"rendered":"How to create Magento 2 widget"},"content":{"rendered":"<p>Magento has been the benchmark set amongst the e\u00ad-commerce frameworks available in the market. Magento Widgets are small Magento extensions with a predefined set of configuration options. Using them the store administrators can enrich the front-end blocks functionality. They provide great control and flexibility in creating informational and marketing content. Personally I see the widgets plus WYSIWYG implementation as a great boost to site owner friendly backend section. In this post we\u2019ll learn how to create and use widgets for Magento 2.<\/p>\n<p>Some of the possible implementations of the Magento widgets are:<\/p>\n<ul>\n<li>Get dynamic product data<\/li>\n<li>Set lists with the recently viewed items<\/li>\n<li>Marketing images placed on different Magento front-end locations<\/li>\n<li>Embedded in content pages<\/li>\n<\/ul>\n<p>and many more!!<\/p>\n<p>I assume that you\u2019ll familiar with the file structure of the Magento 2 extensions. If you a new Magento2 developer and you have no experience with Magento2 extension, you need to read the following tutorial<\/p>\n<p><a href=\"http:\/\/devdocs.magento.com\/guides\/v2.0\/extension-dev-guide\/build\/module-file-structure.html\" target=\"_blank\" rel=\"noopener noreferrer\">Magento Guide<\/a><\/p>\n<p>So, here is the list of files we\u2019ll create step by step:<\/p>\n<p>&#8211; app\/code\/Hiddentechies\/ContactWidget\/etc\/module.xml<br \/>\n&#8211; app\/code\/Hiddentechies\/ContactWidget\/etc\/widget.xml<br \/>\n&#8211; app\/code\/Hiddentechies\/ContactWidget\/registration.php<br \/>\n&#8211; app\/code\/Hiddentechies\/ContactWidget\/Block\/Widget\/Mwcontact.php<br \/>\n&#8211; app\/code\/Hiddentechies\/ContactWidget\/view\/frontend\/templates\/widget\/contact_widget.phtml<\/p>\n<p>I have used [Hiddentechies] as module Vendor Name and [ContactWidget] as extension name.<\/p>\n<p>Lets start step by step process of creating widget for Magento 2.<\/p>\n<p>1) etc\/module.xml<\/p>\n<pre class=\"lang:default decode:true \">&lt;?xml version=\"1.0\"?&gt;\r\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\"&gt;\r\n    &lt;module name=\"Hiddentechies_ContactWidget\" setup_version=\"1.0.0\" \/&gt;\r\n&lt;\/config&gt;<\/pre>\n<p>2) etc\/widget.xml<\/p>\n<pre class=\"lang:default decode:true \">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n\r\n&lt;widgets xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Widget:etc\/widget.xsd\"&gt;\r\n    &lt;widget id=\"custom_widget\" class=\"Hiddentechies\\ContactWidget\\Block\\Widget\\Mwcontact\"&gt;\r\n        &lt;label translate=\"true\"&gt;Hiddentechies - Contact Widget&lt;\/label&gt;\r\n        &lt;description&gt;Hiddentechies - Sample Contact Widget&lt;\/description&gt;\r\n        &lt;parameters&gt;\r\n            &lt;parameter name=\"name\" xsi:type=\"text\" visible=\"true\" required=\"true\" sort_order=\"0\" &gt;\r\n                &lt;label translate=\"true\"&gt;Name&lt;\/label&gt;\r\n            &lt;\/parameter&gt;\r\n            &lt;parameter name=\"address\" xsi:type=\"textarea\" visible=\"true\" sort_order=\"10\"&gt;\r\n                &lt;label translate=\"true\"&gt;Address&lt;\/label&gt;\r\n            &lt;\/parameter&gt;\r\n            &lt;parameter name=\"mobile\" xsi:type=\"text\" visible=\"true\" source_model=\"Magento\\Config\\Model\\Config\\Source\\Yesno\" sort_order=\"20\"&gt;\r\n                &lt;label translate=\"true\"&gt;Mobile No&lt;\/label&gt;\r\n            &lt;\/parameter&gt;\r\n            &lt;parameter name=\"website\" xsi:type=\"text\" visible=\"true\" sort_order=\"30\"&gt;\r\n                &lt;label translate=\"true\"&gt;Website URL&lt;\/label&gt;\r\n            &lt;\/parameter&gt;\r\n        &lt;\/parameters&gt;\r\n    &lt;\/widget&gt;\r\n&lt;\/widgets&gt;<\/pre>\n<p>3) registration.php<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\r\nuse \\Magento\\Framework\\Component\\ComponentRegistrar;\r\n\r\nComponentRegistrar::register(\r\n        ComponentRegistrar::MODULE, \r\n        'Hiddentechies_ContactWidget',\r\n        __DIR__\r\n);\r\n<\/pre>\n<p>4) Block\/Widget\/Mwcontact.php<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\nnamespace Hiddentechies\\ContactWidget\\Block\\Widget;\r\n\r\nclass Mwcontact extends \\Magento\\Framework\\View\\Element\\Template implements \\Magento\\Widget\\Block\\BlockInterface\r\n{\r\n    protected function _construct()\r\n    {\r\n        parent::_construct();\r\n        $this-&gt;setTemplate('widget\/contact_widget.phtml');\r\n    }\r\n}<\/pre>\n<p>5) view\/frontend\/templates\/widget\/contact_widget.phtml<\/p>\n<pre class=\"lang:default decode:true \">&lt;div class=\"contact-widget\"&gt;\r\n    &lt;table&gt;\r\n        &lt;tr&gt;\r\n            &lt;td&gt;Name&lt;\/td&gt;\r\n            &lt;td&gt;&lt;?php echo $this-&gt;getData('name'); ?&gt;&lt;\/td&gt;\r\n        &lt;\/tr&gt;\r\n        &lt;tr&gt;\r\n            &lt;td&gt;Address&lt;\/td&gt;\r\n            &lt;td&gt;&lt;?php echo $this-&gt;getData('address'); ?&gt;&lt;\/td&gt;\r\n        &lt;\/tr&gt;\r\n        &lt;tr&gt;\r\n            &lt;td&gt;Mobile&lt;\/td&gt;\r\n            &lt;td&gt;&lt;?php echo $this-&gt;getData('mobile'); ?&gt;&lt;\/td&gt;\r\n        &lt;\/tr&gt;\r\n        &lt;tr&gt;\r\n            &lt;td&gt;Website URL&lt;\/td&gt;\r\n            &lt;td&gt;&lt;?php echo $this-&gt;getData('website'); ?&gt;&lt;\/td&gt;\r\n        &lt;\/tr&gt;\r\n    &lt;\/table&gt;\r\n&lt;\/div&gt;<\/pre>\n<p>Thats it. Now add this widget to app\/code folder and run upgrade command. Clear your cache and try to add this widget to any CMS page. You can see result on CMS page on frontend.<\/p>\n<p>Enjoy cool stuff!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento has been the benchmark set amongst the e\u00ad-commerce frameworks available in the market. Magento Widgets are small Magento extensions with a predefined set of configuration options. Using them the store administrators can enrich the front-end blocks functionality. They provide great control and flexibility in creating informational and marketing content. Personally I see the widgets&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/create-magento-2-widget\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":4826,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[10,38,129,196],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/2849"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=2849"}],"version-history":[{"count":3,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/2849\/revisions"}],"predecessor-version":[{"id":10507,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/2849\/revisions\/10507"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/4826"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=2849"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=2849"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=2849"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}