2017-05-15 1 views

答えて

0

をプラグインを使用する方法の後にプラグインクラスは、あなたのモジュールのなど/ 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 

「観測された方法は、」我々はプラグインクラスを介してこの例では

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. 

を変更したいコアクラス内の関数の存在を意味し、我々はコアのaddProductという名前の関数を観察しますカートクラス。この例では

あなたが(カートに製品を追加する)addProductメソッドの前にいくつかのコードを実行したい場合は、あなたのプラグインクラスでbeforeAddProductという名前のメソッドを作成する必要があります。

同様に、商品をカートに追加した後に何かしたい場合は、プラグインクラスにafterAddProductという名前の新しい関数を作成する必要があります。

また、 'around'という別の方法があります。これにより、観測されたメソッドが呼び出される前後にいくつかのコードを実行することができます。このためには、プラグインクラスに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;  
    } 
} 
?> 
関連する問題