TrapBarn Posted January 31, 2022 Share Posted January 31, 2022 (edited) I have been trying to modify a PHP plugin used in Woocommerce. Originally, it had two fields, with the first being where a tracking number (tracking_number) was manually inserted. The second field (tracking_url) being for the URL of the carriers page also manually entered. My modification has been to replace the latter with a "Select" dropdown giving a choice between Royal Mail, UPS and Parcel Force. It works OK for Royal Mail and Parcel Force because the respective selections of these two are saved in the tracking_url field. It send the email with the tracking number and the name of the carrier. But not so with UPS! If I select the UPS URL it disappears as soon as I click "update". Any help would be much appreciated Herewith the code <<?php /* Plugin Name: 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 $Id: tracking-info-to-wc-order.php 5510 2021-05-10 11:02:59Z 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', 'desc' => 'Select Carrier', 'id' => 'tracking_url', 'type' => 'select', 'show_option_none' => true, 'default' => 'https://www.royalmail.com/track-your-item#/tracking-results/', 'options' => array( 'https://www.royalmail.com/track-your-item#/tracking-results/' => __( 'https://www.royalmail.com/track-your-item#/tracking-results/', 'cmb2' ), 'https://www.ups.com/WebTracking/track?loc=en_GB&requester=ST/' => __( 'https://www.ups.com/WebTracking/track?loc=en_GB&requester=ST/', 'cmb2' ), 'https://www.parcelforce.com/TRACK-TRACE/' => __( 'https://www.parcelforce.com/TRACK-TRACE/', 'cmb2' ), 'https://www.ups.com/WebTracking/track?loc=en_GB&requester=ST/' => __( 'https://www.ups.com/WebTracking/track?loc=en_GB&requester=ST/', 'cmb2' ), ), ) ); } // 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, 'www.royalmail.com' ) !== false ) { return 'Royal Mail'; } if ( strpos( $url, 'www.UPS.com' ) !== false ) { return 'UPS'; } if ( strpos( $url, 'www.parcelforce.com' ) !== false ) { return 'Parcel Force'; } // 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 '<p>Sorry, tracking information is not available at this time.</p>'; 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 ) ); } } } } // 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 '<p>Sorry, tracking information is not available at this time.</p>'; return; } $tracking_provider = dcwd_get_tracking_provider_from_url( $tracking_url ); 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 ) ); } } Thank you. Edited January 31, 2022 by TrapBarn Quote Link to comment Share on other sites More sharing options...
TrapBarn Posted February 1, 2022 Author Share Posted February 1, 2022 Now sorted. Quote Link to comment Share on other sites More sharing options...
TrapBarn Posted February 7, 2022 Author Share Posted February 7, 2022 It may be of interest to others. It turned out to be a clash with the API and Woo. The tracking number is pushed out to Woocommerce by the Royal Mail REST API. This writes the tracking to the "Notes" field and sends an email to the customer with the "Notes". It also reclassifies the order as marked. We worked out how to fix the problem after we realised there could be a potential conflict between the email that the API was sending and the email that Woo sends when an order is completed. So we decided to write a plug in that would defer the sending of the Woo email to allow the API one to go out first. We put in a delay of 2 minutes and that worked. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.