php – 从Magento的装运跟踪事件中获取跟踪ID

我想从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?

最佳答案 我发现事件包含键轨道下的Mage_Sales_Model_Order_Shipment_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();
}

关于实际事件中包含的内容,这完全是一种误解.

点赞