2017-09-11 13 views
1

Magento 2ページのシンプルなイベントオブザーバを作成しようとしています。Magento 2イベントオブザーバが動作しない

アプリケーション/コード/ NDAC/ORDERINFOの/ 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/ORDERINFOの/ 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/ORDERINFO/registration.php

​​

アプリケーション/コード/ NDAC/ORDERINFO /オブザーバー/ 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は空のままです。

答えて

3

ファイルの命名に問題があります。

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

これはのようにあります:それはevents.xmlにする必要があり

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

。また、このイベントがうまくいかない場合は、checkout_onepage_controller_success_actionイベントで試してみてください。

アップグレードを実行してキャッシュをクリアします。

関連する問題