繁体   English   中英

Magento 2事件观察器不起作用

[英]Magento 2 event observer not working

我正在尝试为Magento 2页面创建一个简单的事件观察器。

应用程序/代码/ NDAC /订单信息的/ etc / event.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="sales_order_place_after">
       <observer name="OrderInfo" instance="Ndac\Orderinfo\Observer\OrderInfo" />
   </event>

</config>

应用程序/代码/ NDAC /订单信息的/ etc / module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Orderinfo" setup_version="1.0.0"></module>
</config>

应用程序/代码/ NDAC /订单信息/和registration.php

<?php \Magento\Framework\Component\ComponentRegistrar::register(\Magento\Framework\Component\ComponentRegistrar::MODULE,"Orderinfo", __DIR__);

应用程序/代码/ NDAC /订单信息/观测/ OrderInfo.php

<?php
namespace Ndac\Orderinfo\Observer;

use Magento\Sales\Model\Order;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class OrderInfo implements ObserverInterface {
    // Tried it with constructor
    public __construct() {
        $file = fopen("/mnt/data/magento/test.txt", "w") or die ("die");
        fwrite($file, "test");
        fclose($file);
    }

    public function execute(Observer $observer)
    {
        $file = fopen("/mnt/data/magento/test.txt", "w") or die ("die")
        fwrite($file, "test");
        fclose($file);
    }
}
?>

我运行以下命令: bin/magento setup:upgrade ,该模块显示在仪表板上,并已启用,但下订单后test.txt仍然为空。

文件命名存在问题。

app/code/Ndac/Orderinfo/etc/event.xml

这必须是:

app/code/Ndac/Orderinfo/etc/events.xml

它必须是events.xml 另外,如果此事件不起作用,请尝试使用checkout_onepage_controller_success_action事件。

现在运行升级命令并清除缓存。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM