を使用して、私は前にのためにオブザーバーを書きたいとMagentoの中で2Magentoの2:ビフォー&メソッドオブザーバー後プラグイン
0
A
答えて
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;
}
}
?>
関連する問題
- 1. Magento OAuth、プラグインのインストール
- 2. プラグインのMagentoリフレッシュキャッシュ保存
- 3. Magento 2
- 4. Magentoの2:Sebwite
- 5. Magento 2のカスタムフォーム
- 6. Magentoの2:カートページ
- 7. チェックアウト後のMagento 1.9.2.4
- 8. Magento 2のMagento 2への拡張機能のインストール1.9
- 9. Magento 2:CSSのファイルパス
- 10. Magento 2:カスタムモジュールのsystem.xml
- 11. プラグインを無効にした後にMagentoの管理ページが空白になる
- 12. magento 2イメージギャラリーpro
- 13. Magento 2 Grunt problems
- 14. onchangeイベントmagento 2
- 15. Magento 2タイムゾーン
- 16. sales_order_place_after not magento 2
- 17. Magento 2 Modal Widget
- 18. Magento 2 Cron.php
- 19. Magento 2テーマカスタマイズ
- 20. Magento 2 UIコンポーネント
- 21. Magento 2オブザーバマニュアルディスパッチ/トリガオブザーバ
- 22. removeAttribute magento 2
- 23. Magento 2 REST API
- 24. Magento 2 - ajax変更数量後のリロード合計カート
- 25. Magento 2:プレースオーダーでお支払い後の確認ステップ
- 26. Magento 2 - 502 ssl設定後の不正なゲートウェイ
- 27. Magento 2拡張機能をインストールした後のフロントエンドエラー
- 28. Magento 2 bin/magentoコマンドプロンプトphp versionメッセージ
- 29. 〜60秒後のMagentoカールエラー503
- 30. Magentoの2 REINDEX mcryptのフグエラー