cancel
Showing results for 
Search instead for 
Did you mean: 

Get shipping tracking number to email

SOLVED

Get shipping tracking number to email

Hey,

 

I'm trying to edit the shipping email template, and I need to get the tracking number, but it comes in the block 

{{block class='Magento\\Framework\\View\\Element\\Template' area='frontend' template='Magento_Sales::email/shipment/track.phtml' shipment=$shipment order=$order}}

There is any way I can access this $shipment outside the block call?

Other thing, I need to do a <a href="www.url.com/$variable"> with a base url then a variable, but it always show me the complete <a> tag in the email and not a link to the words, there is any reason for this?

 

 

Thanks Smiley Very Happy

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Get shipping tracking number to email

Hello @rui_silva1 

 

Add this in the shipping email:

{{layout handle="sales_email_order_shipment_track" shipment=$shipment order=$order}}

Override sales_email_order_shipment_track.xml in your module:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="sales_email_order_shipment_renderers"/>
    <body>
        <block class="Magento\Framework\View\Element\Template" name="sales.order.email.shipment.track" template="[VendorName_ModuleName]::email/shipment/track.phtml">
            <arguments>
                <argument name="tracking_url" xsi:type="object">Magento\Sales\Block\DataProviders\Email\Shipment\TrackingUrl</argument>
            </arguments>
        </block>
    </body>
</page>

Then, create app/code/[Vendor]/[Module]/email/shipment/track.phtml

<?php 
$_shipment = $block->getShipment() 
$_order = $block->getOrder()
if ($_shipment && $_order) :
$trackCollection = $_order->getTracksCollection($_shipment->getId())
 if ($trackCollection) :
    foreach ($trackCollection as $_item) :
      $_item->getTitle(); // print
      $_item->getNumber(); // print 
     // add a tag here with number
    endforeach;
 endif;
endif;

I hope it helps.

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

View solution in original post

2 REPLIES 2

Re: Get shipping tracking number to email

Hello @rui_silva1 

 

Add this in the shipping email:

{{layout handle="sales_email_order_shipment_track" shipment=$shipment order=$order}}

Override sales_email_order_shipment_track.xml in your module:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="sales_email_order_shipment_renderers"/>
    <body>
        <block class="Magento\Framework\View\Element\Template" name="sales.order.email.shipment.track" template="[VendorName_ModuleName]::email/shipment/track.phtml">
            <arguments>
                <argument name="tracking_url" xsi:type="object">Magento\Sales\Block\DataProviders\Email\Shipment\TrackingUrl</argument>
            </arguments>
        </block>
    </body>
</page>

Then, create app/code/[Vendor]/[Module]/email/shipment/track.phtml

<?php 
$_shipment = $block->getShipment() 
$_order = $block->getOrder()
if ($_shipment && $_order) :
$trackCollection = $_order->getTracksCollection($_shipment->getId())
 if ($trackCollection) :
    foreach ($trackCollection as $_item) :
      $_item->getTitle(); // print
      $_item->getNumber(); // print 
     // add a tag here with number
    endforeach;
 endif;
endif;

I hope it helps.

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

Re: Get shipping tracking number to email

In Magento 2.4.1 you can copy vendor/magento/module-sales/view/frontend/templates/email/shipment/track.phtml to app/design/frontend/[Theme]/default/Magento_Sales/templates/email/shipment/track.phtml, and make your changes there.

 

In my case, I just needed to rewrite the link to a 3rd-party tracking site, so I changed this:

 

<a href="<?= $block->escapeUrl($block->getTrackingUrl()->getUrl($_item)) ?>" target="_blank">
    <?= $block->escapeHtml($_item->getNumber()) ?>
</a>

 

to this:

 

<a href="<?= $block->escapeUrl('https://portal.shipwizmo.com/tracking.aspx?custtracknbr=' . $block->escapeHtml($_item->getNumber())) ?>" target="_blank">
    <?= $block->escapeHtml($_item->getNumber()) ?>
</a>

On my local, I cleared static files cache, then regular cache and sent a test email and it worked well. If you need to do more manipulation to things before getting to this final template file, you can use the above code in the accepted solution, but put the .phtml file in app/code/[Vendor]/[Module]/view/frontend/templates/email/shipment/track.phtml.