Nik7
Nik7

Reputation: 386

How to echo Woocommerce shipment tracking URL in email?

I already found this topic here: How can I echo Woocommerce shipment tracking in an email template? This is working but in my case I want to display the tracking link instead of the tracking number. We want to display this info on a specific position. The plugin code shows me this:

<a href="<?php echo esc_url( $tracking_item['formatted_tracking_link'] ); ?>" target="_blank"><?php _e( 'Track', 'woocommerce-shipment-tracking' ); ?></a>

In the documentation of the plugin is says, that I can access the data with the key tracking_link

https://docs.woocommerce.com/document/shipment-tracking/#section-9

So then I tried to access the data with that.. But its always empty.. but the infos are there.. becasue when I use tracking_number instead of tracking_link then I get the right tracking number of the order.

Update: Based on the new infos, I have to do a workaround here because the "tracking_link" value is empty.

// Test - Output tracking code - 3
$tracking_items = $order->get_meta('_wc_shipment_tracking_items');

if( ! empty($tracking_items) ) { 
    foreach ( $tracking_items as $data ){
        echo '<a href="https://www.post.ch/swisspost-tracking?formattedParcelCodes='.<?php echo esc_url( $data['tracking_number'] ); ?> . '" target="_blank">'. sprintf( __('Track (number %s)', 'woocommerce-shipment-tracking' ), $data['tracking_number'] ) .'</a>';
        print_r($data);
            }
        }

Upvotes: 0

Views: 867

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253859

Updated: Don't use the order number… Better use the defined WC_Order object like:

$tracking_items = $order->get_meta('_wc_shipment_tracking_items');

or using the order ID (from the defined WC_Order object) like:

$tracking_items =  get_post_meta( $order->get_id(), '_wc_shipment_tracking_items', true );

So based on Shipment Tracking documentation provided link, try:

$tracking_items = $order->get_meta('_wc_shipment_tracking_items');

if( ! empty($tracking_items) ) { 
    foreach ( $tracking_items as $data ){
        echo '<a href="https://www.post.ch/swisspost-tracking?formattedParcelCodes='. esc_url( $data['tracking_number'] ) . '" target="_blank">'. sprintf( __('Track (number %s)', 'woocommerce-shipment-tracking' ), $data['tracking_number'] ) .'</a>';
        // print_r($data); // For testing
    }
}

Upvotes: 2

Related Questions