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

Magento 2提供了一个称为插件拦截器的新功能,用于在任何公共类的任何函数调用之前/之后/周围运行一些代码。因此,借助插件,我们可以更改类方法的行为,而无需对类本身的代码进行任何更改。这也避免了与其他模块的冲突。

假设您的模块名为YourCompany_YourModule

插件类必须在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>

这里,

类型的名称=要观察其方法的类
插件的名称=随机的插件名称
插件的类型=插件的类的名称(YourCompany \ YourModule \ Plugin \插件)
插件的sortOrder =插件的顺序,称为
插件的禁用=启用/禁用插件,默认值为false。

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

beforeMethod =包含要在观察到的方法之前运行的代码afterMethod =包含要在观察到的方法
之后运行的代码aroundMethod =包含要在观察到的方法
之前和之后运行的代码

“观察到的方法”是指我们要通过我们的Plugin类修改的Core类中存在的函数

在上面的di.xml文件中,我们定义了将要Magento\Checkout\Model\Cart在插件class中观察class的方法YourCompany\YourModule\Plugin\CartPlugin

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

在这个例子中

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

同样,如果您想在将产品添加到购物车后做某事,则必须创建一个afterAddProduct在Plugin类中命名的新函数。

并且,还有另一种名为“ around”的方法。这使您可以在调用观察到的方法之前和之后执行一些代码。为此,您将必须添加一个aroundAddProduct在Plugin类中命名的新函数。

这是CartPlugin课程。

应用程序/代码/ YourCompany / YourModule / Plugin / CartPlugin.php

<?php
 
namespace YourCompany\YourModule\Plugin;
 
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;    
    }
}
?>

在官方文档中了解有关Magento 2插件的更多信息:Magento 2 Plugins

希望这可以帮助。谢谢。

赞(0)
微信
支付宝
微信二维码图片

微信扫描二维码打赏

支付宝二维码图片

支付宝扫描二维码打赏

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论