{"id":675,"date":"2019-11-13T20:59:46","date_gmt":"2019-11-13T20:59:46","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/11\/13\/override-catalog-widget-block-in-magento-2\/"},"modified":"2025-05-22T15:24:00","modified_gmt":"2025-05-22T09:54:00","slug":"override-catalog-widget-block-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/override-catalog-widget-block-in-magento-2\/","title":{"rendered":"How To Override Catalog Widget Block in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In Magento 2, blocks are PHP classes used to connect or create a link between layout and templates.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We, developers, sometimes require to place the related template file into the override block with the same directory path as the original when we override the widget block class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hence, to <strong>override catalog widget block in Magento 2<\/strong>, you need to follow the below steps:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method To Override Catalog Widget Block in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create&nbsp;<em><strong>di.xml<\/strong><\/em>&nbsp;file at&nbsp;<strong>app\/code\/Vendor\/Module\/etc<\/strong>&nbsp;into your custom module<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\">\n&lt;preference for=\"Magento\\CatalogWidget\\Block\\Product\\ProductsList\" type=\"Vendor\\Module\\Block\\Product\\ProductsList\" \/>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create a block file &nbsp;<strong>ProductsList.php<\/strong>&nbsp;at&nbsp;<strong>app\/code\/Vendor\/Module\/Block\/Product<\/strong>&nbsp;to extend the widget class<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Vendor\\Module\\Block\\Product;\nclass ProductsList extends \\Magento\\CatalogWidget\\Block\\Product\\ProductsList\n{\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Copy the related template file and put into your custom module with related Path<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>vendor\/magento\/module-catalog-widget\/view\/frontend\/templates\/product\/widget\/content\/grid.phtml<\/strong><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">to<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>app\/code\/Vendor\/Module\/view\/frontend\/templates\/product\/widget\/content\/grid.phtml<\/strong><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Make sure another module has not used the same preference in <strong>di.xml<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As a store owner <a href=\"https:\/\/meetanshi.com\/blog\/override-product-view-page-social-meta-tags-in-magento-2\/\">Magento 2 override product view page social meta tags<\/a>,it will help you to hide social meta tag or dynamically change content of social meta tags.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the post with fellow developers via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can also refer to the relevant solution to <a href=\"https:\/\/meetanshi.com\/blog\/override-block-model-controller-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">override block, model, and controller in Magento 2<\/a> that I posted earlier.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Magento 2, blocks are PHP classes used to connect or create a link between layout and templates. We, developers, sometimes require to place the related template file into the override block with the same directory path as the original when we override the widget block class. Hence, to override catalog widget block in Magento [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-675","post","type-post","status-publish","format-standard","hentry","category-magento"],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/675","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=675"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/675\/revisions"}],"predecessor-version":[{"id":15186,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/675\/revisions\/15186"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}