허용되는 답변에 더 이상 사용되지 않는 저장 방법 대신 리포지토리를 사용하는 것이 좋습니다. 또한 추적 생성 후 고객 알림을 추가했습니다.
/** @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는 발송물입니다. Notify는 사용자에게 알리고 (이메일 보내기) 주문 상태 내역 모음에 내역 항목을 추가합니다.