:::: MENU ::::
Posts tagged with: rewrite

Magento 2 overriding or Rewriting controller

In our daily coding routine we need to customization with core files. For that we need to overriding or rewrite the core classes because modify the core classes is not good practice. If you modify the code classes when you update the new version your code will be loss. So today we learn how to override core blocks. In start we are overriding or rewriting controller. We are just checking our rewrite is working or not. You can modify the blocks as you want by following this tutorial. But first you need to know How to create module in Magneto 2.For any suggestions & question, please feel free to drop a comment.

Overriding or Rewrite Magento 2 controller

A Controller has one or more files in Controller folder of module, it includes actions of class which contain execute() method. There are 2 different controllers, they are frontend controller and backend controller.Now we start to overriding or rewriting core Product controller. The controller we are overriding is Product View controller. Magento 2 use controller for category product listing.

Step 1.

Create a di.xml file in a following directory QaisarSatti/HelloWorld/etc/

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Controller\Product\View" type="QaisarSatti\HelloWorld\Controller\Rewrite \Product\View" />
</config>

Step 2
The step2 to overriding or rewriting Magento 2 controller is to create a ListProduct.php controller file in the following directory QaisarSatti\HelloWorld\Controller\Rewrite \Product\

<?php
/**
* Catalog Product Rewrite controller
*
* @category QaisarSatti
* @package QaisarSatti_HelloWorld
* @author Muhammad Qaisar Satti
*
*/

namespace QaisarSatti\HelloWorld\Controller\Rewrite\Product;
class View extends \Magento\Catalog\Controller\Product\View
{
public function execute() {


echo "Controller Rewrite Working"; die();
  }
}

As following this code you can rewrite or override any Magento 2 controller using the same method.

Author: Qaisar Satti
Category: Magento 2
Last Modified: December 20, 2017


Magento 2 overriding or Rewriting block

In our daily coding routine we need to customization with core files. For that we need to overriding or rewrite the core classes because modify the core classes is not good practice. If you modify the code classes when you update the new version your code will be loss. So today we learn how to override core blocks. In start we are overriding core Product blocks. We are just checking our rewrite is working or not. You can modify the blocks as you want by following this tutorial. But first you need to know How to create module in Magneto 2.For any suggestions & question, please feel free to drop a comment.

Overriding or Rewrite Magento 2 Block

A Block is one of the first class elements in the structure of Magento layouts. Every page in Magento is decorated by the Layouts file and the content is filled up by the Blocks of the different modules.Now we start to overriding or rewriting core Product block. The model we are overriding is Product Listing block. Magento 2 use block for category product listing.

Step 1.

Create a di.xml file in a following directory QaisarSatti/HelloWorld/etc/

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Block\Product\ListProduct" type="QaisarSatti\HelloWorld\Block\Rewrite\Product\ListProduct" />
</config>

Step 2
The step2 to overriding or rewriting Magento 2 block is to create a ListProduct.php block file in the following directory QaisarSatti/HelloWorld/Block/Rewrite/Product

<?php
/**
* Catalog Product Rewrite Block
*
* @category QaisarSatti
* @package QaisarSatti_HelloWorld
* @author Muhammad Qaisar Satti
*
*/

namespace QaisarSatti\HelloWorld\Block\Rewrite\Product;
class ListProduct extends \Magento\Catalog\Block\Product\ListProduct
{
public function __construct() {


echo "Block Rewrite Working"; die();
}
}

As following this code you can rewrite or override any Magento 2 block using the same method.

Author: Qaisar Satti
Category: Magento 2
Last Modified: December 20, 2017


Magento 2 overriding or rewriting model

In our daily coding routine we need to customization with core files. For that we need to overriding or rewrite the core classes because modify the core classes is not good practice. If you modify the code classes when you update the new version your code will be loss. So today we learn Magento 2 overriding rewriting model. In start we are overriding core Product model. We are jut checking our rewriting model is working or not. You can modify the model as you want by following this tutorial. But first you need to know How to create module in Magneto 2.For any suggestions & question, please feel free to drop a comment.

Overriding or Rewrite Magento 2 Model

Magento 2 Models play an even bigger role, as they typically contain the “Business Logic” that’s often relegated to the Controller or Helper methods in other PHP MVC frameworks. Now we start to overriding or rewriting core Product model. The model we are overriding is Product Model.
Step 1.

Create a di.xml file in a following directory QaisarSatti/HelloWorld/etc/

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Model\Product" type="QaisarSatti\HelloWorld\Model\Rewrite\Catalog\Product" />
</config>

Step 2
The step2 to overriding or rewriting Magento2 model is to create a Product.php helper file in the following directory QaisarSatti/HelloWorld/Model/Rewrite/Catalog

<?php
/**
* Catalog Product Rewrite Model
*
* @category QaisarSatti
* @package QaisarSatti_HelloWorld
* @author Muhammad Qaisar Satti
*
*/

namespace QaisarSatti\HelloWorld\Model\Rewrite\Catalog;
class Product extends \Magento\Catalog\Model\Product
{
public function __construct()
{
echo "Overrding/ Rewrite Working"; die();
}
}

As following code you can rewrite or override any Magento 2 model using the same method.

Author: Qaisar Satti
Category: Magento 2
Last Modified: May 22, 2018


Override magento controller

Every Magento project requires a certain level of customization. This may involve adding a new element or overriding an existing one. Today, we’ll talk about how to override a magento controller. For any suggestions & question, please feel free to drop a comment.

First Step:

I am overriding the customer controller. First thing will create new module file.
The file location will me  will be

app/etc/modules/QaisarSatti_Customer.xml

<?xml version="1.0"?>
    <config>
     <modules>
          <QaisarSatti_Customer>
               <active>true</active>
               <codePool>local</codePool>
          </QaisarSatti_Customer>
      </modules>
    </config>

Second Step:

We will create module configuration file. So file location will be

app/code/local/QaisarSatti/Customer/etc/config.xml

<?xml version="1.0"?>
    <config>
        <modules>
            <QaisarSatti_Customer>
                <version>0.0.1</version>
            </QaisarSatti_Customer>
        </modules>
        <frontend>
            <routers>
                <customer>
                    <args>
                        <modules>
                            <QaisarSatti_Customer before="Mage_Customer">QaisarSatti_Customer</QaisarSatti_Customer>
                        </modules>
                    </args>
                </customer>
            </routers>
        </frontend>
    </config>

Third Step:

Now we will create the Our controller file.

app/code/local/QaisarSatti/Customer/controllers/AccountController.php

<?php

/**
* Overriding Customer account controller
*/

require_once Mage::getModuleDir('controllers', 'Mage_Customer') . DS . 'AccountController.php';

class QaisarSatti_Customer_AccountController extends Mage_Customer_AccountController {

public function ajaxAction() {  //if you want to create a custom method in customer controller
   echo 'Ajax cction is working!!';
}

public function loginAction() {
   echo 'Overring the Login Action is working';
}

}
Author: Qaisar Satti
Category: Magento
Last Modified: February 6, 2018


Magento 2 create  and use helper

Using helper you can put your methods in there that you have no idea where to put them. For example if you want create the thumbnail of image you use the helper method. The method you don’t know where to put put in helper class. Now creating the helper and use of it.

Create Helper folder in module directory.



namespace QaisarSatti\HelloWorld\Helper

 

Now create the your helper class what name you want to put.In my case i will use Data file. Now the complete path will be.

namespace QaisarSatti\HelloWorld\Helper\Data.php



This file will be extend with.



 \Magento\Framework\App\Helper\AbstractHelper



Now create helper Data class.

 


<?php

/**

* Simple Hello World Module

*

* @category QaisarSatti

* @package QaisarSatti_HelloWorld

* @author Muhammad Qaisar Satti

* @Email [email protected]

*

*/


namespace QaisarSatti\HelloWorld\Helper;



use Magento\Store\Model\Store;



class Data extends \Magento\Framework\App\Helper\AbstractHelper

{



public function getTitle()

{

return  'First Hello World Module';

}

}

Now we will use helper method  in our block.

<?php

/**

* Simple Hello World Module

*

* @category QaisarSatti

* @package QaisarSatti_HelloWorld

* @author Muhammad Qaisar Satti

* @Email [email protected]

*

*/


namespace QaisarSatti\HelloWorld\Block;

class HelloWorld extends \Magento\Framework\View\Element\Template

{

protected $_helper;

public function __construct(

\QaisarSatti\HelloWorld\Helper\Data $_helper,

) {



$this->_helper=$_helper;



}

public function _prepareLayout()

{

parent::_prepareLayout();

$this->pageConfig->getTitle()->set($this->_helper->getTitle());

return $this;

}

}
Author: Qaisar Satti
Category: Magento 2
Last Modified: December 20, 2017


Magento 2 Override default theme template files

If you are doing the custom theme then you must know how in Magento 2 Override default theme template files in your custom theme.You want to override all the files then simply go to.

vendor/magento/module-theme/view/frontend/

And copy the templates folder in your theme. In my case theme name is theme. So in my theme folder create the Magento_Theme Folder. And paste the templates folder there. Now your theme structure will look like this.

app/design/frontend/QaisarSatti/theme/Magento_Theme/templates

If you want override the single file like header.phtml. Then copy file from

vendor/magento/module-theme/view/frontend/templates/html/header.phtml

Paste in your theme. And file structure will look like

app/design/frontend/QaisarSatti/theme/Magento_Theme/templates/html/header.phtml
Author: Qaisar Satti
Category: Magento 2
Last Modified: March 28, 2018