在发生产品保存事件之后,我尝试在magento 2中访问该产品。
我在以下位置配置了侦听器:
controller_action_catalog_product_save_entity_after
xml如下所示:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_catalog_product_save_entity_after">
<observer name="wire_products_update" instance="TBS\WireProductsHook\Observer\WireProductsUpdate" />
</event>
</config>
保存产品后,将调用观察者类,但是我找不到有关如何访问保存的产品的任何信息。观察者类如下:
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace TBS\WireProductsHook\Observer;
use Magento\Framework\Event\ObserverInterface;
class WireProductsUpdate implements ObserverInterface {
public function execute( \Magento\Framework\Event\Observer $observer ) {
// echos name of event: controller_action_catalog_product_save_entity_after
echo 'name of event: ' . $observer->getEvent()->getName();
// tried using the same method as the save class: <magento>/module-catalog/Controller/Adminhtml/Product/Save.php on line 76
$controller = $observer->getData('controller');
// Fails: Fatal error: Cannot access protected property ...
$product = $controller->initializationHelper->initialize($controller->productBuilder->build($controller->getRequest()));
}
}
在/module-catalog/Controller/Adminhtml/Product/Save.php中,事件在第114行触发:
$this->_eventManager->dispatch(
'controller_action_catalog_product_save_entity_after',
['controller' => $this]
);
这将控制器作为参数传递。但是我不确定如何使用它来访问产品。
我想获得产品SKU