Welcome to WordPress. This is your first post. Edit or delete it, then start writing!
/* 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. Also add custom REST API endpoint to push Author: Damien Carbery Author URI: https://www.damiencarbery.com Version: 0.6 */ // 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' ), ) ); } // Move the saving or order meta (which triggers emails) to be *after* CMB2 data saved. // NOTE: This could have unintended consequences. add_action( 'wp_loaded', 'dcwd_move_wc_order_meta_save'); function dcwd_move_wc_order_meta_save() { remove_action( 'woocommerce_process_shop_order_meta', 'WC_Meta_Box_Order_Data::save', 40 ); add_action( 'save_post', 'WC_Meta_Box_Order_Data::save', 50 ); } // If using 'Email Template Customizer for WooCommerce' plugin then use a different hook // to add the tracking information to the email. add_action( 'plugins_loaded', 'dcwd_check_for_email_template_customizer' ); function dcwd_check_for_email_template_customizer() { if ( class_exists( 'Woo_Email_Template_Customizer' ) ) { // Email Template Customizer for WooCommerce plugin does not use the 'woocommerce_email_order_details' // hook so use 'woocommerce_email_after_order_table' instead (it is one of the 3 available ones in the // plugin's 'WC Hook' field. add_action( 'woocommerce_email_after_order_table', 'dcwd_add_tracking_info_to_order_completed_email', 5, 4 ); } } // 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. if ( strpos( $url, 'www.singpost.com' ) !== false ) { return 'Singapore Post'; } // 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 ) ) { // Debugging code. //error_log( sprintf( 'Order %d does not have both tracking number (%s) and url (%s)', $order_id, $tracking_number, $tracking_url ) ); //echo '
Sorry, tracking information is not available at this time.
'; 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( 'Your order has been shipped with %s. The tracking number is %s.
', $tracking_provider, esc_url( $tracking_url, array( 'http', 'https' ) ), esc_html( $tracking_number ) ); } else { printf( 'Your order has been shipped. The tracking number is %s.
', esc_url( $tracking_url, array( 'http', 'https' ) ), esc_html( $tracking_number ) ); } } } } // Display tracking information in My Account area. add_action( 'woocommerce_view_order', 'dcwd_add_tracking_info_to_view_order_page', 5 ); function dcwd_add_tracking_info_to_view_order_page( $order_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 ) ) { // Debugging code. error_log( sprintf( 'Order %d does not have both tracking number (%s) and url (%s)', $order_id, $tracking_number, $tracking_url ) ); echo 'Sorry, tracking information is not available at this time.
'; return; } $tracking_provider = dcwd_get_tracking_provider_from_url( $tracking_url ); if ( ! empty( $tracking_provider ) ) { printf( 'Your order has been shipped with %s. The tracking number is %s.
', $tracking_provider, esc_url( $tracking_url, array( 'http', 'https' ) ), esc_html( $tracking_number ) ); } else { printf( 'Your order has been shipped. The tracking number is %s.
', esc_url( $tracking_url, array( 'http', 'https' ) ), esc_html( $tracking_number ) ); } }Welcome to WordPress. This is your first post. Edit or delete it, then start writing!