Magento 2

How to Override Core Block, Model and controller in Magento2

I follow below steps to override Block, Controller and Model I took Exmaple of Product Model and Product View Block and View Controller/Action
 
Create a file called di.xml in your /etc/di.xml
 
<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <preference for="\Magento\Catalog\Model\Product" type="Goweb\Test\Model\Product" />
    <preference for="\Magento\Catalog\Block\Product\View" type="Goweb\Test\Block\Product\View" />
    <preference for="\Magento\Catalog\Controller\Product\View" type="Goweb\Test\Controller\Product\View" />
</config>
Then I created Model File in /Model/Product.php
 
<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Goweb\Test\Model;
 
class Product extends \Magento\Catalog\Model\Product
{
    /**
     * Get product name
     *
     * @return string
     * @codeCoverageIgnoreStart
     */
    public function getName()
    {
        return $this->_getData(self::NAME).'Local';
    }    
}
Then I created Block file in /Block/Product/View.php
 
<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Goweb\Test\Block\Product;
/**
 * Product View block
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class View extends \Magento\Catalog\Block\Product\View
{
 
 
    /**
     * Retrieve current product model
     *
     * @return MagentoCatalogModelProduct
     */
    public function getProduct()
    {
       echo 'Local Block';
       if (!$this->_coreRegistry->registry('product') && $this->getProductId()) {
            $product = $this->productRepository->getById($this->getProductId());
            $this->_coreRegistry->register('product', $product);
        }
        return $this->_coreRegistry->registry('product');
    }
 
 
}
Now Create Product View Controller /Controller/Product/View.php
 
<?php
/**
 *
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Goweb\Test\Controller\Product;
 
class View extends \Magento\Catalog\Controller\Product\View
{
 
    /**
     * Product view action
     *
     * @return MagentoFrameworkControllerResultForward|MagentoFrameworkControllerResultRedirect
     */
    public function execute()
{ // Get initial data from request echo 'I Am in Local Controller'; // Get initial data from request $categoryId = (int) $this->getRequest()->getParam('category', false); $productId = (int) $this->getRequest()->getParam('id'); $specifyOptions = $this->getRequest()->getParam('options'); if ($this->getRequest()->isPost() && $this->getRequest()->getParam(self::PARAM_NAME_URL_ENCODED)) { $product = $this->_initProduct(); if (!$product) { return $this->noProductRedirect(); } if ($specifyOptions) { $notice = $product->getTypeInstance()->getSpecifyOptionMessage(); $this->messageManager->addNotice($notice); } if ($this->getRequest()->isAjax()) { $this->getResponse()->representJson( $this->_objectManager->get('MagentoFrameworkJsonHelperData')->jsonEncode([ 'backUrl' => $this->_redirect->getRedirectUrl() ]) ); return; } $resultRedirect = $this->resultRedirectFactory->create(); $resultRedirect->setRefererOrBaseUrl(); return $resultRedirect; } // Prepare helper and params $params = new MagentoFrameworkDataObject(); $params->setCategoryId($categoryId); $params->setSpecifyOptions($specifyOptions); // Render page try { $page = $this->resultPageFactory->create(false, ['isIsolated' => true]); $this->viewHelper->prepareAndRender($page, $productId, $this, $params); return $page; } catch (MagentoFrameworkExceptionNoSuchEntityException $e) { return $this->noProductRedirect(); } catch (Exception $e) { $this->_objectManager->get('PsrLogLoggerInterface')->critical($e); $resultForward = $this->resultForwardFactory->create(); $resultForward->forward('noroute'); return $resultForward; } }
}
Its Working Fine for me :-)

1 Bình luận

  1. cialis nói:

    Kids Perfect for quick usa overnight pharmacy generic cialis overnight shipping one of?

Trở về
  • Magento plug-ins
  • Magento plug-ins

    Magento2 gave very good concept called Plugin   we can do what ever after and before core function and also we have...

  • HOW TO CREATE MENU IN MAGENTO 2 ADMIN
  • HOW TO CREATE MENU IN MAGENTO 2 ADMIN

    magento-2-admin- menu My previous tutorial show you how to create a simple module in Magento 2, learn more here: http://www.venustheme.com/how-to-create-magento-2-module/....