如何在Magento 2的当前订单发货中添加跟踪号?


10

我确实找到了Magento 1.x的示例代码。但是我不知道如何在Magento 2上执行此操作。

谁能描述如何使用依赖注入(DI)来实现这一点?

谢谢。

$trackingDetail = array(
    'carrier_code' => 'ups',
    'title' => 'United Parcel Service',
    'number' => 'TORD23254WERZXd3', // Replace with your tracking number
);

$track = Mage::getModel('sales/order_shipment_track')->addData($trackingDetail);
$shipment->addTrack($track);


$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($shipment)
->addObject($shipment->getOrder())
->save();

Answers:


12

AFAIK轨道对象在M2中相同。

但是,其余代码已更改。

$data = array(
    'carrier_code' => 'ups',
    'title' => 'United Parcel Service',
    'number' => 'TORD23254WERZXd3', // Replace with your tracking number
);

$track = $this->trackFactory->create()->addData($data);
$shipment->addTrack($track)->save();

其中$this->trackFactory的一个实例Magento\Sales\Model\Order\Shipment\TrackFactory,并$shipment为您的货对象。


顺便说一句,我想知道您是否也知道如何为MG2获取订单发货注释。如果您这样做,请在此处发布。谢谢。
Roshan ruzaik

如何在装运中添加跟踪URl?
Purushotam Sharma,

@Roshanruzaik,如果您要问如何在货件上添加注释,那么这就是我的做法:$ commentText ='Test comment'; //评论接口:\ Magento \ Sales \ Api \ Data \ ShipmentCommentCreationInterface $ comment = $ this-> commentInterface-> setComment($ commentText); //在创建货件时将$ comment用作参数之一,如下例://船运订单服务:\ Magento \ Sales \ Model \ ShipOrder $ this-> shipOrderService-> execute($ orderId,$ shippedItems,$ notify,$ includeComment,$ comment,$ tracks);
Sarjan Gautam,

@Raphael,您好,在数字钢琴演奏家tracking饮着追踪编号,而且carrier_code正在保存在magento2的“ sales_shipment_track”表中,但它将在哪里节省运费?
Nagaraju K,

你可以请这个类似的问题帮助magento.stackexchange.com/questions/268844/...
克里斯温家宝

9

除了接受的答案外,最好使用存储库而不是不建议使用的保存方法。在跟踪创建之后还添加了客户通知。

/** @var Magento\Sales\Model\Order\ShipmentRepository */
protected $_shipmentRepository;

/** @var Magento\Shipping\Model\ShipmentNotifier */
protected $_shipmentNotifier;

/** @var Magento\Sales\Model\Order\Shipment\TrackFactory */
protected $_trackFactory; //missing ;

public function __construct(
  \Magento\Shipping\Model\ShipmentNotifier $shipmentNotifier, 
  \Magento\Sales\Model\Order\ShipmentRepository $shipmentRepository, 
  \Magento\Sales\Model\Order\Shipment\TrackFactory $trackFactory)
{
  $this->_shipmentNotifier = $shipmentNotifier;
  $this->_shipmentRepository = $shipmentRepository;
  $this->_trackFactory = $trackFactory;
}
public function addTrack($shipment, $carrierCode, $description, $trackingNumber) 
{
    /** Creating Tracking */
    /** @var Track $track */
    $track = $this->_trackFactory->create();
    $track->setCarrierCode($carrierCode);
    $track->setDescription($description);
    $track->setTrackNumber($trackingNumber);
    $shipment->addTrack($track);
    $this->_shipmentRepository->save($shipment);

    /* Notify the customer*/
    $this->_shipmentNotifier->notify($shipment);
 }

其中$ shipment是装运对象。通知将通知(发送电子邮件)给用户,并将历史记录项添加到订单状态历史记录集合中。


它对我创建了helper类不起作用,我能够获取getDeiveryTime(),getCancelTime(),getDispatchTime(),但我无法获取跟踪号。你能提供的功能列表,以获取装运其余细节
insoftservice

您可以帮忙解决类似问题吗?magento.stackexchange.com/questions/268844/…–
克里斯·温

@carol Castro,我们如何在此处添加订单ID?
Mujahidh
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.