Jump to content

TrapBarn

New Members
  • Posts

    5
  • Joined

  • Last visited

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

TrapBarn's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. As an septuagenarian, I needed something to keep me active in retirement so I set up TrapBarn to be fast and simple whilst providing support to customer. My customers are mostly gamekeepers, farmers etc - many of who grew up before the internet age. Hence the need for a straight forward site that is intuitive. Is there anything more that I need to do on that front? On GTMatrix it is returning 95% on performance and 100% on structure. Time to interactive is 464ms
  2. 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.
  3. 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.
  4. I'm a Newbie of (seriously) advanced years and would appreciate a bit of help. I have a metabox with 2 fields. First field is called tracking_number and the tracking number is entered manually. The second field is called tracking_url which is also entered manually. The current code is as follows:- // Examine the tracking url and return a provider name. function dcwd_get_tracking_provider_from_url( $url ) { if ( strpos( $url, 'parcel.force.com' ) !== false ) { return 'Parcel Force'; } if ( strpos( $url, 'royal.mail.com' ) !== false ) { return 'Royal Mail'; } if ( strpos( $url, 'ups.com' ) !== false ) { return 'UPS'; } // Unknown provider. return null; } I want to have the second field populated automatically based on examining the format of the tracking number in the tracking_number field. The format for the carriers I use are :- Royal Mail = 21 alpha-numeric starting with 320.......... Parcel Force = 2 alpha followed by 7 digits UPS = 18 alpha- numeric starting with 1Z Can someone oblige with the necessary adjustments, please?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.