Skip to content

Instantly share code, notes, and snippets.

@ventouris
Created February 27, 2021 20:55
Show Gist options
  • Save ventouris/cbbf63c0dc7a43e060a385765bbcf194 to your computer and use it in GitHub Desktop.
Save ventouris/cbbf63c0dc7a43e060a385765bbcf194 to your computer and use it in GitHub Desktop.
function tracking_company_select_options() {
return array(
'elta' => 'Ελτά',
'tnt' => 'TNT',
'geniki_taxydromiki' => 'Γενική Ταχυδρομική'
);
}
add_action( 'cmb2_admin_init', 'dcwd_order_metabox' );
function dcwd_order_metabox() {
$cmb = new_cmb2_box( array(
'id' => 'order_tracking_info',
'title' => 'Tracking Number',
'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' => 'Courier',
'id' => 'tracking_company',
'type' => 'select',
'options_cb' => 'tracking_company_select_options',
) );
$cmb->add_field( array(
'name' => 'Tracking number',
'id' => 'tracking_number',
'type' => 'text',
) );
}
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 || 'customer_invoice' == $email->id ) {
$order_id = $order->get_id();
$tracking_number = get_post_meta( $order_id, 'tracking_number', true );
$tracking_company = get_post_meta( $order_id, 'tracking_company', true );
if ( $tracking_company == 'geniki_taxydromiki' ) {
$tracking_url = '<a href="https://www.taxydromiki.com/TRACK/' . $tracking_number . '">' . $tracking_number . '</a>';
} elseif ( $tracking_company == 'tnt' ) {
$tracking_url = '<a href="https://www.tnt.com/express/en_gr/site/shipping-tools/tracking.html?searchType=con&cons=' . $tracking_number . '">' . $tracking_number . '</a>';
} else {
$tracking_url = $tracking_number;
}
// Quit if either tracking field is empty.
if ( empty( $tracking_number ) || empty( $tracking_url ) ) {
// Debugging code.
//error_log( sprintf( 'Order %d does not have both tracking number (%s) and url (%s)', $order_id, $tracking_number, $tracking_url ) );
//echo '<p>Sorry, tracking information is not available at this time.</p>';
return;
}
if ( $plain_text ) {
if ( ! empty( $tracking_number ) ) {
printf( "\nYour tracking number is %s.\n", $tracking_number );
}
}
else {
if ( ! empty( $tracking_number ) ) {
printf( '<p>Your tracking number is %s.</p>', $tracking_url );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment