{"id":265427,"date":"2020-08-29T14:12:45","date_gmt":"2020-08-29T14:12:45","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=265427"},"modified":"2021-12-30T09:36:01","modified_gmt":"2021-12-30T09:36:01","slug":"write-your-own-javascript-in-the-storefront-at-the-shopware","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/write-your-own-javascript-in-the-storefront-at-the-shopware\/","title":{"rendered":"Write your own javascript in the storefront at the Shopware"},"content":{"rendered":"\n<p>In this blog, you are going to learn \u201cHow to write your own storefront javascript plugin at Shopware.\u201d<br>I hope you know the directory structure of&nbsp;<a href=\"https:\/\/webkul.com\/blog\/create-product-and-product-variant-in-shopware-6\/\">Shopware<\/a>&nbsp;6 plugin, if you don\u2019t know, see here-&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/webkul.com\/blog\/create-product-and-product-variant-in-shopware-6\/\" target=\"_blank\">https:\/\/docs.shopware.com\/en\/shopware-platform-dev-en\/internals\/directory-structure<\/a>.<\/p>\n\n\n\n<p>Storefront JavaScript plugins are vanilla JavaScript ES6 classes that extend from our Plugin base class.<br>Firstly, create a Resources\/app\/storefront\/src\/test-plugin folder and put a test-plugin.plugin.js file in there. For more background information on JavaScript classes, take a look here.<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">test-plugin.plugin.js<\/h6>\n\n\n\n<pre class=\"EnlighterJSRAW\">import Plugin from &#039;src\/plugin-system\/plugin.class&#039;;\n\nexport default class TestPlugin extends Plugin {\n    init() {\n        window.onscroll = function() {\n            if ((window.innerHeight + window.pageYOffset) &gt;= document.body.offsetHeight) {\n                alert(&#039;seems like there\\&#039;s nothing more to see here.&#039;);\n            }\n        };\n    }\n}<\/pre>\n\n\n\n<p>In the above file create and export a TestPlugin class that extends the base Plugin class. Each Plugin has to implement the <code>init()<\/code>method. This method will call when your plugin gets initialize and is the entry point to your custom logic. In my case, I add a callback to the onScroll event from the window and check if the user is scrolling to the bottom of the page. If so we display an alert.<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">main.js<\/h6>\n\n\n\n<pre class=\"EnlighterJSRAW\">import TestPlugin from &#039;.\/test-plugin\/test-plugin.plugin&#039;;\n\nconst PluginManager = window.PluginManager;\nPluginManager.register(&#039;TestPlugin&#039;, TestPlugin);<\/pre>\n\n\n\n<p>Now you have to register your plugin in the PluginManager. Create a main.js file inside your Resources\/app\/storefront\/src folder and then get the PluginManager from the global window object.<\/p>\n\n\n\n<p>You also can bind your plugin to an DOM element by providing an css selector.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">import ExamplePlugin from &#039;.\/test-plugin\/test-plugin.plugin&#039;;\n\n const PluginManager = window.PluginManager;\n PluginManager.register(&#039;TestPlugin&#039;, TestPlugin, &#039;&#091;data-test-plugin]&#039;);<\/pre>\n\n\n\n<p>In this case, the plugin just gets execute if the HTML document contains at least one element with the data-scroll-detector attribute. You could use this.el inside your plugin to access the DOM element your plugin is bound to. Lastly, you have to add a small code snippet for the HotModuleReload server to work with your custom plugins.<\/p>\n\n\n\n<p>Create an Resources\/views\/storefront\/page\/content\/ folder and create an index.html.twig template. Inside this template extend from the @Storefront\/storefront\/page\/content\/index.html.twig and overwrite the base_main_inner block. After the parent container of the blog add a template tag that has the data-example-plugin attribute.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">{% sw_extends &#039;@Storefront\/storefront\/page\/content\/index.html.twig&#039; %}\n\n{% block base_main_inner %}\n    {{ parent() }}\n\n    &lt;template data-example-plugin&gt;&lt;\/template&gt;\n{% endblock %}<\/pre>\n\n\n\n<p>This template extension your plugin is active on every content page, like the homepage or category listing pages.<\/p>\n\n\n\n<p>I hope it will help you. Thanks for reading. Happy Coding \ud83d\ude42<br>Thank You.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, you are going to learn \u201cHow to write your own storefront javascript plugin at Shopware.\u201dI hope you know the directory structure of&nbsp;Shopware&nbsp;6 plugin, if you don\u2019t know, see here-&nbsp;https:\/\/docs.shopware.com\/en\/shopware-platform-dev-en\/internals\/directory-structure. Storefront JavaScript plugins are vanilla JavaScript ES6 classes that extend from our Plugin base class.Firstly, create a Resources\/app\/storefront\/src\/test-plugin folder and put a test-plugin.plugin.js <a href=\"https:\/\/webkul.com\/blog\/write-your-own-javascript-in-the-storefront-at-the-shopware\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":284,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9568],"tags":[],"class_list":["post-265427","post","type-post","status-publish","format-standard","hentry","category-shopware"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Write your own javascript in the storefront at the Shopware - Webkul Blog<\/title>\n<meta name=\"description\" content=\"Storefront JavaScript plugins are vanilla JavaScript ECMAScript6 classes that extend from our Plugin base class in the Shopware.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/write-your-own-javascript-in-the-storefront-at-the-shopware\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Write your own javascript in the storefront at the Shopware - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"Storefront JavaScript plugins are vanilla JavaScript ECMAScript6 classes that extend from our Plugin base class in the Shopware.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/write-your-own-javascript-in-the-storefront-at-the-shopware\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-08-29T14:12:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-30T09:36:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Diwakar Rana\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Diwakar Rana\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/write-your-own-javascript-in-the-storefront-at-the-shopware\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/write-your-own-javascript-in-the-storefront-at-the-shopware\/\"},\"author\":{\"name\":\"Diwakar Rana\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/4b025fe4ecbc5c0378cd13bb70da654f\"},\"headline\":\"Write your own javascript in the storefront at the Shopware\",\"datePublished\":\"2020-08-29T14:12:45+00:00\",\"dateModified\":\"2021-12-30T09:36:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/write-your-own-javascript-in-the-storefront-at-the-shopware\/\"},\"wordCount\":366,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"articleSection\":[\"Shopware\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/write-your-own-javascript-in-the-storefront-at-the-shopware\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/write-your-own-javascript-in-the-storefront-at-the-shopware\/\",\"url\":\"https:\/\/webkul.com\/blog\/write-your-own-javascript-in-the-storefront-at-the-shopware\/\",\"name\":\"Write your own javascript in the storefront at the Shopware - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2020-08-29T14:12:45+00:00\",\"dateModified\":\"2021-12-30T09:36:01+00:00\",\"description\":\"Storefront JavaScript plugins are vanilla JavaScript ECMAScript6 classes that extend from our Plugin base class in the Shopware.\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/write-your-own-javascript-in-the-storefront-at-the-shopware\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/write-your-own-javascript-in-the-storefront-at-the-shopware\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/write-your-own-javascript-in-the-storefront-at-the-shopware\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Write your own javascript in the storefront at the Shopware\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/4b025fe4ecbc5c0378cd13bb70da654f\",\"name\":\"Diwakar Rana\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/46482d0264c191ccd0337892016340a80ca4e4987a37f42514a0506aaee7e8dc?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/46482d0264c191ccd0337892016340a80ca4e4987a37f42514a0506aaee7e8dc?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Diwakar Rana\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/diwakar-rana829\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Write your own javascript in the storefront at the Shopware - Webkul Blog","description":"Storefront JavaScript plugins are vanilla JavaScript ECMAScript6 classes that extend from our Plugin base class in the Shopware.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/write-your-own-javascript-in-the-storefront-at-the-shopware\/","og_locale":"en_US","og_type":"article","og_title":"Write your own javascript in the storefront at the Shopware - Webkul Blog","og_description":"Storefront JavaScript plugins are vanilla JavaScript ECMAScript6 classes that extend from our Plugin base class in the Shopware.","og_url":"https:\/\/webkul.com\/blog\/write-your-own-javascript-in-the-storefront-at-the-shopware\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2020-08-29T14:12:45+00:00","article_modified_time":"2021-12-30T09:36:01+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Diwakar Rana","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Diwakar Rana","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/write-your-own-javascript-in-the-storefront-at-the-shopware\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/write-your-own-javascript-in-the-storefront-at-the-shopware\/"},"author":{"name":"Diwakar Rana","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/4b025fe4ecbc5c0378cd13bb70da654f"},"headline":"Write your own javascript in the storefront at the Shopware","datePublished":"2020-08-29T14:12:45+00:00","dateModified":"2021-12-30T09:36:01+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/write-your-own-javascript-in-the-storefront-at-the-shopware\/"},"wordCount":366,"commentCount":2,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"articleSection":["Shopware"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/write-your-own-javascript-in-the-storefront-at-the-shopware\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/write-your-own-javascript-in-the-storefront-at-the-shopware\/","url":"https:\/\/webkul.com\/blog\/write-your-own-javascript-in-the-storefront-at-the-shopware\/","name":"Write your own javascript in the storefront at the Shopware - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2020-08-29T14:12:45+00:00","dateModified":"2021-12-30T09:36:01+00:00","description":"Storefront JavaScript plugins are vanilla JavaScript ECMAScript6 classes that extend from our Plugin base class in the Shopware.","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/write-your-own-javascript-in-the-storefront-at-the-shopware\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/write-your-own-javascript-in-the-storefront-at-the-shopware\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/write-your-own-javascript-in-the-storefront-at-the-shopware\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Write your own javascript in the storefront at the Shopware"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/4b025fe4ecbc5c0378cd13bb70da654f","name":"Diwakar Rana","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/46482d0264c191ccd0337892016340a80ca4e4987a37f42514a0506aaee7e8dc?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/46482d0264c191ccd0337892016340a80ca4e4987a37f42514a0506aaee7e8dc?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Diwakar Rana"},"url":"https:\/\/webkul.com\/blog\/author\/diwakar-rana829\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/265427","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/284"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=265427"}],"version-history":[{"count":6,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/265427\/revisions"}],"predecessor-version":[{"id":265617,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/265427\/revisions\/265617"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=265427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=265427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=265427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}