1

I developed a custom module so that customers can choose the logistics methods provided by the plugin, and these logistics methods all have a unified CarrierCode. But how do I know if the order comes from my custom logistics method when the user tracks shipment so that I can provide them with logistics information.

May be I need to change details.phtml in module-shipping, \vendor\magento\module-shipping\view\frontend\templates\tracking\details.phtml .But in this page I don't know which shipping method it use,juse know $track['number'],or $shipmentId in parent page.

So,how can i get shipping method they used?

1 Answer 1

0

If I understand you correctly, you are trying to add information based on the carrier code.

When creating a shipment, if you add tracking details, then you have carrier_code in there. Check this table:

Check \vendor\magento\module-sales\view\frontend\templates\email\shipment\track.phtml. This is the file responsible for sending tracking detail in shipment email.

And you can get the carrier code inside the tracking loop:

$_item->getCarrierCode()

To customize this file, just copy the file to your theme:

/app/design/frontend/YourTheme/ThemeName/tempaltes/email/shipment/track.phtml

Hope this helps.

UPDATE

In order to add tracking info to your custom shipping method, you should add getTrackingInfo($trackingNumber) function in your carrier (Namespace/Module/Model/Carrier/YourCarrier.php).

/**
 * Get tracking information. Original return value annotation is misleading.
 *
 * @see \Magento\Shipping\Model\Carrier\AbstractCarrier::isTrackingAvailable()
 * @see \Magento\Shipping\Model\Carrier\AbstractCarrierOnline::getTrackingInfo()
 * @see \Magento\Dhl\Model\Carrier::getTracking()
 * @param string $trackingNumber
 * @return \Magento\Shipping\Model\Tracking\Result\AbstractResult
 */
public function getTrackingInfo($trackingNumber)
{
    /** @var \Magento\Shipping\Model\Tracking\Result\Status $tracking */
    $tracking = $this->trackStatusFactory->create();

    $title = $this->getConfigData('title');

    $tracking->setCarrier($this->_code); //your carrier code
    $tracking->setCarrierTitle($title);
    $tracking->setTracking($trackingNumber);

    //you may want to add the events coming from your api
    $trackEventsData [] =
        [
            'deliverydate' => 'date',
            'deliverytime' => 'time',
            'deliverylocation' => 'location',
            'activity' => 'activity'
        ];
    $tracking->setStatus(isset($trackEventsData[0]) ? $trackEventsData[0]['activity'] : '');
    $tracking->setProgressdetail($trackEventsData);

    return $tracking;
}
6
  • Think you for your answer.
    – keke996
    Commented Apr 15, 2020 at 7:13
  • Think you for your answer. I think your code can help me tu send email with TrackingUrl.But I dont't know how to add this url.And I'm trying to add tracking message in web when customer open tracking information page.I can useing track id to visit my shipping carrier Api to get shipping log.So ,may be you can tell me how to get carrierCode in \vendor\magento\module-shipping\view\frontend\templates\tracking\details.phtml or \vendor\magento\module-shipping\view\frontend\templates\tracking\popup.phtml.Think you for your help.
    – keke996
    Commented Apr 15, 2020 at 7:21
  • Plez check my update. Commented Apr 15, 2020 at 23:45
  • think you.And a new question.After I writed \Magento\Shipping\Model\Tracking\Result\StatusFactory $trackStatusFactory $this->trackStatusFactory in my __construct(){} my website is die.Said Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: Forkel\CustomShipping\Model\Carrier\Custom What's worng with my code.I'm a new coder for magento2 ,many thing is difficult. Thank you again.
    – keke996
    Commented Apr 16, 2020 at 8:01
  • 1
    Thank you so much. My code is running;
    – keke996
    Commented Apr 17, 2020 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.