Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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.9
- */
- // Verify that CMB2 plugin is active.
- add_action( 'admin_notices', 'titwo_verify_cmb2_active' );
- function titwo_verify_cmb2_active() {
- if ( ! defined( 'CMB2_LOADED' ) ) {
- $current_screen = get_current_screen();
- if ( $current_screen->id == 'shop_order' ) {
- $plugin_data = get_plugin_data( __FILE__ );
- $plugin_name = $plugin_data['Name'];
- ?>
- <div class="notice notice-warning is-dismissible"><p>The <strong><?php echo $plugin_name; ?></strong> plugin requires <a href="https://wordpress.org/plugins/cmb2/">CMB2 plugin</a> to be active to enable adding tracking information to orders.</p></div>
- <?php
- }
- }
- }
- // Declare that this plugin supports WooCommerce HPOS.
- add_action( 'before_woocommerce_init', function() {
- if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
- \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
- }
- } );
- // 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', 'woocommerce_page_wc-orders' ), // 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' ),
- 'desc' => 'Be sure to add tracking data and click \'Update\' before setting the order status to \'Completed\', and clicking \'Update\' again. If not done in this order the email sent to the customer will not contain the tracking data.',
- ) );
- }
- // 2022-10-16: Comment this out as it is breaking in Woo >6.9.3. Instead, I simply enable the deferring of emails.
- /*
- // Move the saving of 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 );
- // Call WC_Meta_Box_Order_Data::save later, after CMB2 data is saved.
- add_action( 'save_post', 'dcwd_save_post_order_data', 50, 3 );
- }
- // Call WC_Meta_Box_Order_Data::save later, after CMB2 data is saved.
- function dcwd_save_post_order_data( $post_id, $post, $update ) {
- // Call the WooCommerce Meta Box Order Data save function for WC_Order posts only.
- // TODO: Look at wc_get_order_types() as do_action('woocommerce_process_shop_order_meta') runs when in_array( $post->post_type, wc_get_order_types( 'order-meta-boxes' ), true )
- if ( 'shop_order' == $post->post_type) {
- WC_Meta_Box_Order_Data::save( $post_id );
- }
- }*/
- // Defer emails for 10 seconds to allow time for CMB2 to save the tracking data.
- add_filter( 'woocommerce_defer_transactional_emails', '__return_true' );
- // 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 'FedEx';
- }
- 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 = wc_get_order_item_meta( $order_id, 'tracking_number');
- $tracking_url = wc_get_order_item_meta( $order_id, 'tracking_url' );
- // 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 = wc_get_order_item_meta( $order_id, 'tracking_number' );
- $tracking_url = wc_get_order_item_meta( $order_id, 'tracking_url' );
- // 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 ) );
- }
- }
- // DEBUGGING FUNCTIONS
- //add_filter( 'cmb2_override_meta_value', 'dcwd_cmb2_override_meta_value', 10, 4 );
- add_filter( 'cmb2_override_tracking_number_meta_value', 'dcwd_cmb2_override_meta_value', 10, 4 );
- add_filter( 'cmb2_override_tracking_url_meta_value', 'dcwd_cmb2_override_meta_value', 10, 4 );
- function dcwd_cmb2_override_meta_value( $type, $object_id, $a, $this_obj ) {
- error_log( 'Type: '. $type );
- error_log( 'Post/object ID: '. $object_id );
- error_log( '$a: '. var_export( $a, true ) );
- //error_log( '$this: '. var_export( $this_obj, true ) );
- if ( 'tracking_number' == $a[ 'field_id' ] ) {
- return '12345';
- }
- if ( 'tracking_url' == $a[ 'field_id' ] ) {
- return 'https://www.dhl.ie/track/12345';
- }
- return 'cmb2_field_no_override_val';
- }
- add_filter( 'cmb2_override_meta_save', 'dcwd_cmb2_override_meta_save', 10, 4 );
- add_filter( 'cmb2_override_tracking_number_meta_save', 'dcwd_cmb2_override_meta_save', 10, 4 );
- add_filter( 'cmb2_override_tracking_url_meta_save', 'dcwd_cmb2_override_meta_save', 10, 4 );
- function dcwd_cmb2_override_meta_save( $override, $a, $args, $this_obj ) {
- error_log( 'Type: cmb2_override_*' . $a['field_id'] . '*_meta_save' );
- error_log( '$a: '. var_export( $a, true ) );
- error_log( '$_POST: '. var_export( $_POST, true ) );
- //error_log( '$args: '. var_export( $args, true ) );
- //error_log( '$this: '. var_export( $this_obj, true ) );
- return $override;
- }
- //$override = apply_filters( 'cmb2_override_meta_save', null, $a, $this->args(), $this );
- add_filter( 'cmb2_can_save', 'dcwd_cmb2_can_save', 10, 2 );
- function dcwd_cmb2_can_save( $can_save, $cmb_obj ) {
- $current_screen = get_current_screen();
- if ( 'woocommerce_page_wc-orders' == $current_screen->id ) {
- if ( array_key_exists( 'tracking_number', $_POST ) && ( !empty( $_POST[ 'tracking_number' ] ) ) ) {
- if ( array_key_exists( 'tracking_url', $_POST ) && ( !empty( $_POST[ 'tracking_url' ] ) ) )
- error_log( 'dcwd_cmb2_can_save: Return true as tracking_number and tracking_url are in $_POST.' );
- return true;
- }
- }
- return $can_save;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement