Magento 2:之前和之后在使用插件的方法观察者之后

时间:2017-05-15 05:42:42

标签: magento magento2

我想在magento 2中使用插件编写一个前后方法的观察者。

1 个答案:

答案 0 :(得分:0)

必须在模块的etc / di.xml中声明插件类。这是代码:

app/code/YourCompany/YourModule/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">    
    <type name="Magento\Checkout\Model\Cart">
        <plugin name="pluginAddProductToCart" type="YourCompany\YourModule\Plugin\CartPlugin" sortOrder="10" disabled="false"/>
    </type>
</config>

下面,

type’s name = class whose methods are to be observed
plugin’s name = random plugin name
plugin’s type = name of plugin’s class (YourCompany\YourModule\Plugin\Plugin)
plugin’s sortOrder = the order of the plugin to be called
plugin’s disabled = enable/disable plugin, default value is false.

我们可以添加修改核心类函数的方法之前,之后和周围。例如,如果核心类中有“保存”函数,那么在插件类中我们可以使用beforeSave,afterSave和aroundSave方法。

beforeMethod = contains code to run before the observed Method
afterMethod = contains code to run after the observed Method
aroundMethod = contains code to run both before and after the observed Method

“观察方法”是指我们想要通过插件类

修改的Core类中的函数
In the above di.xml file, we have defined that we are going to observe methods of class Magento\Checkout\Model\Cart in our plugin class YourCompany\YourModule\Plugin\CartPlugin.

在这个例子中,我们将观察核心Cart类的名为addProduct的函数。

对于此示例,

如果你想在addProduct方法之前运行一些代码(将产品添加到购物车),那么你必须在Plugin类中创建一个名为beforeAddProduct的方法。

同样,如果您想在将产品添加到购物车后执行某些操作,则必须在Plugin类中创建名为afterAddProduct的新函数。

而且,还有另一种名为'around'的方法。这允许您在调用observe方法之前和之后执行一些代码。为此,您必须在Plugin类中添加一个名为aroundAddProduct的新函数。

这是CartPlugin类。

app/code/YourCompany/YourModule/Plugin/CartPlugin.php


<?php

namespace YourCompany\YourModule\Plugin\CartPlugin;

use Magento\Framework\Exception\LocalizedException;

class CartPlugin
{
    /**
     * @var \Magento\Quote\Model\Quote
     */
    protected $quote;

    protected $request;

    /**
     * Plugin constructor.
     *
     * @param \Magento\Checkout\Model\Session $checkoutSession
     */
    public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Framework\App\Request\Http $request        
    ) {
        $this->quote = $checkoutSession->getQuote();
        $this->request = $request;        
    }

    /**
     * beforeAddProduct
     *
     * @param      $subject
     * @param      $productInfo
     * @param null $requestInfo
     *
     * @return array
     * @throws LocalizedException
     */
    public function beforeAddProduct($subject, $productInfo, $requestInfo = null)
    {       
        $productId = (int)$this->request->getParam('product', 0);       
        $qty = (int)$this->request->getParam('qty', 1);             

        // do something
        // your code goes here

        if ( something wrong ) {
            throw new LocalizedException(__('Your error message'));
        }

        return [$productInfo, $requestInfo];
    }

    /**
     * afterAddProduct
     *
     * @param      $subject
     * @param      $result  Returned value from core observed method 'addProduct'     
     */
    public function afterAddProduct($subject, $result)
    {
        $quote = $result->getQuote();

        // do something
        // your code goes here      
    }

    public function aroundAddProduct($subject, $proceed)
    {
        // do something 
        // before adding product to cart
        // your code goes here
        $productId = (int)$this->request->getParam('product', 0);       
        $qty = (int)$this->request->getParam('qty', 1); 

        // this will run the core addProduct function
        $returnValue = $proceed();      

        // below code is executed after product is added to cart
        if ($returnValue) {
            // do something
            // after adding product to cart
            // your code goes here

        }

        return $returnValue;    
    }
}
?>