Magentoめも

Magento2 コアファイルの継承方法

Magento2にすでにある機能を継承して動きを変えたい場合があります。

その場合は、モジュールを作成し、etc/di.xml ファイルにpreferenceを指定すればできるようです。

Modelの継承

vendor\magento\module-catalog\Model\Product.php を継承して商品一覧の商品名に Local を追加する

app/code/Yassujp/HelloWorld/etc/di.xml

<?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="Yassujp\HelloWorld\Model\Product" />
</config>

継承したいメソッドをコピーして書き換えます。

app\code\Yassujp\HelloWorld\Model\Product.php

<?php

namespace Yassujp\HelloWorld\Model;

class Product extends \Magento\Catalog\Model\Product
{
    public function getName()
    {
        return $this->_getData(self::NAME).' Local';
    }
}
model.png

商品名に Local が付加されていることが確認できます。

Blockの継承

vendor\magento\module-catalog\Block\Product\View.php を継承する場合

<?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\Block\Product\View" type="Yassujp\HelloWorld\Block\Product\View" />
</config>

継承したいメソッドをコピーして書き換えます

app\code\Yassujp\HelloWorld\Block\Product\View.php

<?php

namespace Yassujp\HelloWorld\Block\Product;

class View extends \Magento\Catalog\Block\Product\View
{
    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');
    }
}

商品詳細ページの左上に Local Block と表示されていることが確認できます。

Controllerの継承

vendor\magento\module-catalog\Controller\Product\View.php を継承する場合

<?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\Controller\Product\View" type="Yassujp\HelloWorld\Controller\Product\View" />
</config>

app\code\Yassujp\HelloWorld\Controller\Product\View.php

<?php

namespace Yassujp\HelloWorld\Controller\Product;

class View extends \Magento\Catalog\Controller\Product\View
{
    public function execute()
    {
        echo 'I am in Local Controller';
        $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('Magento\Framework\Json\Helper\Data')->jsonEncode([
                        'backUrl' => $this->_redirect->getRedirectUrl()
                    ])
                );
                return;
            }
            $resultRedirect = $this->resultRedirectFactory->create();
            $resultRedirect->setRefererOrBaseUrl();
            return $resultRedirect;
        }

        $params = new \Magento\Framework\DataObject();
        $params->setCategoryId($categoryId);
        $params->setSpecifyOptions($specifyOptions);

        try {
            $page = $this->resultPageFactory->create();
            $this->viewHelper->prepareAndRender($page, $productId, $this, $params);
            return $page;
        } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
            return $this->noProductRedirect();
        } catch (\Exception $e) {
            $this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e);
            $resultForward = $this->resultForwardFactory->create();
            $resultForward->forward('noroute');
            return $resultForward;
        }
    }
}

商品詳細ページ左上に I am in Local Controller と表示されていることが確認できます。

参考


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS