Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save KoolPal/d8ec395a5c661eb203ba30e7c1f38c2c to your computer and use it in GitHub Desktop.
Save KoolPal/d8ec395a5c661eb203ba30e7c1f38c2c to your computer and use it in GitHub Desktop.
Tracking Info to WooCommerce order - Use CMB2 to add a custom metabox to add tracking information to WooCommerce orders. The information is then added to the "Completed Order" email. https://www.damiencarbery.com/2020/01/add-tracking-info-to-woocommerce-order/
<?php
/*
Plugin Name: Tracking Info to WooCommerce order
Plugin URI: https://www.damiencarbery.com/2020/01/add-tracking-info-to-woocommerce-order/
Description: Use CMB2 to add a custom metabox to add tracking information to WooCommerce orders. The information is then added to the "Completed Order" email.
Author: Damien Carbery
Author URI: https://www.damiencarbery.com
Version: 0.2
$Id: tracking-info-to-wc-order.php 4947 2020-01-04 14:52:28Z damien $
*/
// Add the metabox to allow for manual entering (or editing) of tracking information.
add_action( 'cmb2_admin_init', 'dcwd_order_metabox' );
function dcwd_order_metabox() {
$cmb = new_cmb2_box( array(
'id' => 'order_tracking_info',
'title' => 'Tracking Information',
'object_types' => array( 'shop_order', ), // Post type
'context' => 'side',
'priority' => 'high',
'show_names' => true, // Show field names on the left
) );
$cmb->add_field( array(
'name' => 'Tracking number',
'id' => 'tracking_number',
'type' => 'text',
) );
$cmb->add_field( array(
'name' => 'Tracking URL',
'id' => 'tracking_url',
'type' => 'text_url',
'protocols' => array( 'http', 'https' ),
) );
}
// Examine the tracking url and return a provider name.
function dcwd_get_tracking_provider_from_url( $url ) {
if ( strpos( $url, 'usps.com' ) !== false ) {
return 'USPS';
}
if ( strpos( $url, 'fedex.com' ) !== false ) {
return 'FexEd';
}
if ( strpos( $url, 'ups.com' ) !== false ) {
return 'UPS';
}
// Add more as necessary.
// Unknown provider.
return null;
}
// If available, include the tracking information in the Completed Order email.
//add_action( 'woocommerce_email_order_details', 'dcwd_add_tracking_info_to_order_completed_email', 5, 4 );
function dcwd_add_tracking_info_to_order_completed_email( $order, $sent_to_admin, $plain_text, $email ) {
/* // Only customers need to know about the tracking information.
if ( ! $sent_to_admin ) {
return;
}
*/
if ( 'customer_completed_order' == $email->id ) {
$order_id = $order->get_id();
$tracking_number = get_post_meta( $order_id, 'tracking_number', true );
$tracking_url = get_post_meta( $order_id, 'tracking_url', true );
// Quit if either tracking field is empty.
if ( empty( $tracking_number ) || empty( $tracking_url ) ) {
return;
}
$tracking_provider = dcwd_get_tracking_provider_from_url( $tracking_url );
if ( $plain_text ) {
if ( ! empty( $tracking_provider ) ) {
printf( "\nYour order has been shipped with %s. The tracking number is %s and you can track it at %s.\n", $tracking_provider, esc_html( $tracking_number ), esc_url( $tracking_url, array( 'http', 'https' ) ) );
}
else {
printf( "\nYour order has been shipped. The tracking number is %s and you can track it at %s.\n", esc_html( $tracking_number ), esc_url( $tracking_url, array( 'http', 'https' ) ) );
}
}
else {
if ( ! empty( $tracking_provider ) ) {
printf( '<p>Your order has been shipped with <strong>%s</strong>. The tracking number is <strong><a href="%s">%s</a></strong>.</p>', $tracking_provider, esc_url( $tracking_url, array( 'http', 'https' ) ), esc_html( $tracking_number ) );
}
else {
printf( '<p>Your order has been shipped. The tracking number is <strong><a href="%s">%s</a></strong>.</p>', esc_url( $tracking_url, array( 'http', 'https' ) ), esc_html( $tracking_number ) );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment