我在这里尝试了很多建议,但似乎什么都不适用。我正在尝试收听事件'checkout_onepage_controller_success_action'
。我正试图在结账时将订单状态设置为"完成"(目前,我已注释掉该部分(。
正如您在下面(execute方法内部(看到的,我正在尝试打印出order对象,然后退出。但在测试时,什么也没发生。没有打印输出,没有错误消息。没有什么
测试前我运行了以下命令:bin/magento setup:upgrade
、bin/magento setup:di:compile
、bin/magento cache:clean
我还试着收听事件sales_order_place_after
。我也一无所获。。。
app/code/[company]/[module]/etc/frontend/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="checkout_onepage_controller_success_action">
<observer instance="[company][module]ObserverCheckoutSuccessObserver" name="checkout_onepage_controller_success_action_observer"/>
</event>
</config>
app/code/[company]/[module]/Observer/CheckoutSuccessObserver.php
<?php
namespace [company][module]Observer;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkEventObserver;
/**
* Class CheckoutSuccessObserver
*
* @package [company][module]Observer
*/
class CheckoutSuccessObserver implements ObserverInterface
{
/**
* Execute observer
*
* @param MagentoFrameworkEventObserver $observer
* @return void
*/
public function execute(Observer $observer)
{
$order = $observer->getEvent()->getOrder();
print_r($order); exit;
//$order = $observer->getEvent()->getOrder();
//$order_id = $order->getIncrementId();
//$order = Mage::getModel('sales/order')->loadByIncrementId($order_id);
//$order->setData('state', "complete");
//$order->setStatus("complete");
//$history = $order->addStatusHistoryComment('Order was set to complete by our automation tool.', false);
//$history->setIsCustomerNotified(null);
//$order->save();
}
}
您的示例代码看起来不错。您是否启用了自定义模块?使用bin/magento module:status [company]_[module]
检查同时尝试清除项目的目录:https://devdocs.magento.com/guides/v2.3/howdoi/php/php_clear-dirs.html.