问题
I need to automatically create an invoice on an order once it receives the processing
status. Which event should be observed in my extension to achieve this?
I'm trying but this code is not working, what can it be?
.xml in etc/modules
<?xml version="1.0"?>
<config>
<modules>
<Atwix_Orderhook>
<active>true</active>
<codePool>community</codePool>
</Atwix_Orderhook>
</modules>
</config>
config.xml in app/code/...
<?xml version="1.0"?>
<config>
<modules>
<Atwix_Orderhook>
<version>1.0</version>
</Atwix_Orderhook>
</modules>
<global>
<models>
<orderhook>
<class>Atwix_Orderhook_Model</class>
</orderhook>
</models>
<events>
<sales_order_save_after>
<observers>
<auto_invoice_order>
<type>singleton</type>
<class>Atwix_Orderhook_Model_Observer</class>
<method>implementOrderStatus</method>
</auto_invoice_order>
</observers>
</sales_order_save_after>
</events>
</global>
</config>
Oberver.php in app/code/...
<?php
class Atwix_Orderhook_Model_Observer
{
public function implementOrderStatus($event)
{
$order = $event->getOrder();
if ($order == 'processing') {
if ($order->canInvoice())
$this->_processOrderStatus($order);
}
return $this;
}
private function _processOrderStatus($order)
{
$invoice = $order->prepareInvoice();
$invoice->register();
Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder())
->save();
$invoice->sendEmail(false, '');
return true;
}
}
回答1:
You can observe sales_order_save_after
and then check for the order state you are targeting with something like this:
public function exampleEventHandler(Varien_Event_Observer $observer)
{
/* @var Mage_Sales_Model_Order $order */
$order = $observer->getOrder();
$stateProcessing = $order::STATE_PROCESSING;
// Only trigger when an order enters processing state.
if ($order->getState() == $stateProcessing && $order->getOrigData('state') != $stateProcessing) {
...
}
...
}
回答2:
app/code/MyCompany/MyModule/etc/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="sales_order_save_after">
<observer name="mymodule_sales_order_save_after" instance="MyCompany\MyModule\Observer\CheckOrderStatus" />
</event>
</config>
and then
app/code/MyCompany/MyModule/Observer/CheckOrderStatus.php
<?php
namespace MyCompany\MyModule\Observer;
use Magento\Framework\Event\ObserverInterface;
class CheckOrderStatus implements ObserverInterface {
protected $orderRepository;
public function __construct(
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository) {
$this->orderRepository = $orderRepository;
}
public function execute(\Magento\Framework\Event\Observer $observer) {
$order = $observer->getEvent()->getOrder();
$customerId = $order->getCustomerId();
$OrderStatus=$order->getStatus();
if ($OrderStatus == ‘pending’) {
//some work here
}elseif ($OrderStatus == ‘processing’) {
//some work here
}else{
// do something here
}
}
}
for more information https://mage2-blog.com/magento-2-get-order-submission/
来源:https://stackoverflow.com/questions/30357622/trigger-observer-on-magento-order-status-change-events