这是我的密码。当我尝试保存产品newfile123456789.txt
时,文件未更新。
events.xml的代码
<?xml version="1.0"?>
<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="Company_Module_Product_Save_After" instance="Company\Module\Observer\productSaveAfter" />
</event>
</config>
ProductSaveAfter.php的代码
namespace Company\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
class productSaveAfter implements ObserverInterface
{
/**
* @var ObjectManagerInterface
*/
protected $_objectManager;
/**
* @param \Magento\Framework\ObjectManagerInterface $objectManager
*/
public function __construct(
\Magento\Framework\ObjectManagerInterface $objectManager
) {
$this->_objectManager = $objectManager;
}
/**
* customer register event handler
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
//$_product = $observer->getProduct(); // you will get product object
//$_sku = $_product->getSku(); // for sku
$myfile = fopen("newfile123456789.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
die('Observer Is called!');
}
}
答案 0 :(得分:0)
请如下定义产品保存事件:-
<event name="catalog_product_save_after"> <observer name="Company_Module_Product_Save_After" instance="YCompany\Module\Observer\productSaveAfter" /> </event>
还请确保您的观察者正在工作,并且在其中创建文件的文件夹具有写权限。