我正在尝试从Magento 1.9.0.1中的sales_order_shipment_track_save_after
事件中获取跟踪号。出于某种原因,该事件似乎不包括货件,因为$observer->getEvent()->getShipment()
返回NULL
。我使用了错误的事件吗?
这些是我的代码的相关部分:
config.xml(模块)
<events>
<sales_order_shipment_track_save_after>
<observers>
<pixelstore_sms>
<type>model</type>
<class>pixelstore_sms/observer</class>
<method>shipments</method>
</pixelstore_sms>
</observers>
</sales_order_shipment_track_save_after>
</events>
Observer.php(模型)
public function shipments($observer) {
$event = $observer->getEvent();
$shipment = $event->getShipment();
if (!$shipment) {
Mage::log('shipments event did not contain shipment', null, 'track.log');
return false;
}
// We never reach this far
$trackings = $shipment->getAllTracks();
$tracking = end($trackings);
$trackingId = $tracking->getNumber();
// Here I would prefer to have the tracking ID in $trackingId
}
正如您可能已经猜到的那样,if
语句匹配。 get_class()
表明观察者是Varien_Event_Observer
。
我是否观察到错误的事件,或者是否有其他方法我应该致电以获取跟踪ID?
答案 0 :(得分:2)
我发现该事件包含密钥Mage_Sales_Model_Order_Shipment_Track
下的track
对象。这意味着我可以通过$event->getData('track')
,$event['track']
或$event->getTrack()
从事件中获取。所以这就是最终解决了我的问题:
public function shipments(Varien_Event_Observer $observer) {
$event = $observer->getEvent();
$track = $event->getTrack();
$trackingId = $track->getNumber();
// The shipment itself can be found in the track object,
// and the order inside the shipment object:
$shipment = $track->getShipment();
$order = $shipment->getOrder();
}
对于实际事件中包含的内容,这完全是一种误解。
答案 1 :(得分:0)
它应该能够检索订单对象,即使是作为基础观察者。
public function shipments(Varien_Event_Observer $observer) {
$event = $observer->getEvent();
$shipment = $event->getShipment();
if (!$shipment) {
Mage::log('shipments event did not contain shipment', null, 'track.log');
return false;
}
// We never reach this far
$trackingId = $observer->getOrder()->getTrackingNumbers();
// Here I would prefer to have the tracking ID in $trackingId
}