{"id":299,"date":"2019-01-25T08:56:27","date_gmt":"2019-01-25T08:56:27","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/01\/25\/override-block-model-controller-magento-2\/"},"modified":"2025-05-22T17:07:38","modified_gmt":"2025-05-22T11:37:38","slug":"override-block-model-controller-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/override-block-model-controller-magento-2\/","title":{"rendered":"How to Override Block, Model, and Controller in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2 CMS offers flexible options to customize the default configurations. The customized features are helpful to offer enhanced customer experience, easy administration, improved SERPs and sales!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, it is advisable to&nbsp;override block, model, and controller in Magento 2&nbsp;than editing the core files. This guide walks you through the method to overriding block, model, controller, giving you the flexibility to make changes in the core functionalities.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Magento 2 Controller is responsible for handling the incoming requests.&nbsp;Blocks are PHP classes used to connect or create a link between layout and templates. The Model represents the data of the application in MVC.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Override Block, Model, and Controller in Magento 2:<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Magento 2 override&nbsp;<strong>Model<\/strong>&nbsp;class using&nbsp;<strong>di.xml<\/strong><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Use the following code to override&nbsp;<code><strong><strong>Magento\\ConfigurableProduct\\Model\\ConfigurableAttributeData<\/strong><\/strong><\/code> Create file&nbsp;<strong><strong>[Vendor]\\[Module]\\etc\\di.xml<\/strong><\/strong><\/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\"\n            xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Acl\/etc\/acl.xsd\">\n        &lt;preference for=\"Magento\\ConfigurableProduct\\Model\\ConfigurableAttributeData\" type=\"[Vendor]\\[Module]\\Model\\Rewrite\\ConfigurableAttributeData\" \/>\n    &lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create class&nbsp;<code><strong><strong>[Vendor]\\[Module]\\Model\\Rewrite\\ConfigurableAttributeData<\/strong><\/strong><\/code>&nbsp;to override Magento Model<\/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\n    namespace [Vendor]\\[Module]\\Model\\Rewrite;\n \n    use Magento\\Catalog\\Api\\ProductRepositoryInterface;\n    use Magento\\Framework\\Pricing\\Helper\\Data;\n \n    class ConfigurableAttributeData extends \\Magento\\ConfigurableProduct\\Model\\ConfigurableAttributeData\n    {\n        private $productRepository;\n        private $priceHelper;\n \n        public function __construct(\n            ProductRepositoryInterface $productRepository,\n            Data $priceHelper\n        )\n        {\n            $this->productRepository = $productRepository;\n            $this->priceHelper = $priceHelper;\n \n        }\n \n        protected function getAttributeOptionsData($attribute, $config)\n        {\n            $attributeOptionsData = [];\n            foreach ($attribute->getOptions() as $attributeOption) {\n \n                $optionId = $attributeOption['value_index'];\n                $product_details = $config[$attribute->getAttributeId()][$optionId];\n                $productId = $product_details[0];\n                $product = $this->productRepository->getById($productId);\n                $attributeOptionsData[] = [\n                    'id' => $optionId,\n                    'label' => $attributeOption['label'],\n                    'products' => isset($config[$attribute->getAttributeId()][$optionId])\n                        ? $config[$attribute->getAttributeId()][$optionId]\n                        : [],\n                    'sku' => $product->getSku(),\n                    'childProductPrice' => $this->priceHelper->currency($product->getFinalPrice(), true, false),\n                ];\n            }\n            return $attributeOptionsData;\n        }\n    }<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Magento 2 override&nbsp;<strong>block<\/strong>&nbsp;class using&nbsp;<strong><strong>di.xml<\/strong><\/strong>Create file :&nbsp;<strong><strong><strong><strong>[Vendor]\\[Module]\\etc\\di.xml<\/strong><\/strong><\/strong><\/strong><\/li>\n<\/ul>\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\"\n            xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Acl\/etc\/acl.xsd\">\n        &lt;preference for=\"Magento\\Swatches\\Block\\Product\\Renderer\\Configurable\" type=\"[Vendor]\\[Module]\\Block\\Rewrite\\Product\\Renderer\\Configurable\" \/>\n    &lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create class&nbsp;<code><strong><strong>[Vendor]\\[Module]\\Block\\Rewrite\\Product\\Renderer\\Configurable<\/strong><\/strong><\/code>&nbsp;to override Magento block<\/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\n    namespace [Vendor]\\[Module]\\Block\\Rewrite\\Product\\Renderer;\n \n    class Configurable extends \\Magento\\Swatches\\Block\\Product\\Renderer\\Configurable\n    {\n \n        protected function getRendererTemplate()\n        {\n            return $this->isProductHasSwatchAttribute() ?\n                self::SWATCH_RENDERER_TEMPLATE : '[Vendor]_[Module]::product\/view\/type\/options\/configurable.phtml';\n        }\n    }<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Magento 2 override&nbsp;<strong>controller<\/strong>&nbsp;class using&nbsp;<strong><strong>di.xml&nbsp;<\/strong><\/strong>Create file :&nbsp;<strong><strong>[Vendor]\\[Module]\\etc\\di.xml<\/strong><\/strong><\/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\"\n            xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Acl\/etc\/acl.xsd\">\n        &lt;preference for=\"Magento\\Catalog\\Controller\\Product\\Gallery\" type=\"[Vendor]\\[Module]\\Controller\\Product\\Gallery\" \/>\n    &lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create class&nbsp;<strong><strong>[Vendor]\\[Module]\\Controller\\Product\\Gallery<\/strong><\/strong>&nbsp;to override Magento block<\/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\n    namespace [Vendor]\\[Module]\\Controller\\Product;\n \n    class Gallery extends \\Magento\\Catalog\\Controller\\Product\\Gallery\n    {\n \n        public function execute()\n        {\n            $result = null;\n            if (!$this->_initProduct()) {\n                $store = $this->getRequest()->getQuery('store');\n                if (isset($store) &amp;&amp; !$this->getResponse()->isRedirect()) {\n                    $result = $this->resultRedirectFactory->create();\n                    $result->setPath('');\n                } elseif (!$this->getResponse()->isRedirect()) {\n                    \/\/ Apply custom code\n                }\n            }\n            return $result ?: $this->resultPageFactory->create();\n        }\n    }<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s all to override block, model, and controller in Magento 2!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Seems pretty much tedious, isn\u2019t?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We\u2019ve got your back.&nbsp;<a href=\"https:\/\/meetanshi.com\/contacts\" target=\"_blank\" rel=\"noreferrer noopener\">Contact us<\/a>&nbsp;to do your customization tasks! Flawlessly implement amazing features helpful in your store administration tasks and also enrich customer experience!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Happy Customization<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2 CMS offers flexible options to customize the default configurations. The customized features are helpful to offer enhanced customer experience, easy administration, improved SERPs&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-299","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/299","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=299"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/299\/revisions"}],"predecessor-version":[{"id":15523,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/299\/revisions\/15523"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}