WooCommerce PHP: Shipment Tracking Code Snippet
Original Prompt: WooCommerce shipping tracking. Add as a OOP class, and initialize it. Add metabox to order page where admin can add tracking number and carrier name (select2 dropdown of 3 popular ones, ability to add a custom one, add js to admin footer).If one of the 3, add link to the respective carrier tracking page on the number (make a function to do this). If not, just show the number and carrier.Tracking information will then display in emails, and the view order page in customer account section. No Code comments.
class CWPAI_Shipping_Tracking {
public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) );
add_action( 'admin_footer', array( $this, 'add_js' ) );
add_action( 'woocommerce_email_after_order_table', array( $this, 'add_tracking_info_to_email' ) );
add_action( 'woocommerce_view_order', array( $this, 'add_tracking_info_to_order_page' ) );
}
public function add_metabox() {
add_meta_box(
'cwpai_shipping_tracking',
__( 'Shipping Tracking', 'cwpai' ),
array( $this, 'metabox_content' ),
'shop_order',
'side',
'default'
);
}
public function metabox_content() {
global $post;
$tracking_number = get_post_meta( $post->ID, '_cwpai_tracking_number', true );
$carrier_name = get_post_meta( $post->ID, '_cwpai_carrier_name', true );
?>
<p>
<label for="cwpai_tracking_number"><?php _e( 'Tracking Number', 'cwpai' ); ?></label>
<input type="text" name="cwpai_tracking_number" id="cwpai_tracking_number" value="<?php echo esc_attr( $tracking_number ); ?>" />
</p>
<p>
<label for="cwpai_carrier_name"><?php _e( 'Carrier Name', 'cwpai' ); ?></label>
<select name="cwpai_carrier_name" id="cwpai_carrier_name">
<option value=""><?php _e( 'Select a carrier', 'cwpai' ); ?></option>
<option value="fedex" <?php selected( $carrier_name, 'fedex' ); ?>><?php _e( 'FedEx', 'cwpai' ); ?></option>
<option value="ups" <?php selected( $carrier_name, 'ups' ); ?>><?php _e( 'UPS', 'cwpai' ); ?></option>
<option value="usps" <?php selected( $carrier_name, 'usps' ); ?>><?php _e( 'USPS', 'cwpai' ); ?></option>
<option value="custom" <?php selected( $carrier_name, 'custom' ); ?>><?php _e( 'Custom', 'cwpai' ); ?></option>
</select>
</p>
<?php
}
public function add_js() {
global $post_type;
if ( 'shop_order' !== $post_type ) {
return;
}
?>
<script type="text/javascript">
jQuery( function( $ ) {
$( '#cwpai_carrier_name' ).select2();
} );
</script>
<?php
}
public function add_tracking_info_to_email( $order ) {
$tracking_number = get_post_meta( $order->get_id(), '_cwpai_tracking_number', true );
$carrier_name = get_post_meta( $order->get_id(), '_cwpai_carrier_name', true );
if ( ! $tracking_number || ! $carrier_name ) {
return;
}
?>
<h2><?php _e( 'Tracking Information', 'cwpai' ); ?></h2>
<p>
<strong><?php _e( 'Tracking Number:', 'cwpai' ); ?></strong>
<?php echo esc_html( $tracking_number ); ?>
</p>
<p>
<strong><?php _e( 'Carrier:', 'cwpai' ); ?></strong>
<?php echo esc_html( $carrier_name ); ?>
</p>
<?php
if ( in_array( $carrier_name, array( 'fedex', 'ups', 'usps' ) ) ) {
echo $this->get_tracking_url( $carrier_name, $tracking_number );
}
}
public function add_tracking_info_to_order_page( $order_id ) {
$tracking_number = get_post_meta( $order_id, '_cwpai_tracking_number', true );
$carrier_name = get_post_meta( $order_id, '_cwpai_carrier_name', true );
if ( ! $tracking_number || ! $carrier_name ) {
return;
}
?>
<h2><?php _e( 'Tracking Information', 'cwpai' ); ?></h2>
<p>
<strong><?php _e( 'Tracking Number:', 'cwpai' ); ?></strong>
<?php echo esc_html( $tracking_number ); ?>
</p>
<p>
<strong><?php _e( 'Carrier:', 'cwpai' ); ?></strong>
<?php echo esc_html( $carrier_name ); ?>
</p>
<?php
if ( in_array( $carrier_name, array( 'fedex', 'ups', 'usps' ) ) ) {
echo $this->get_tracking_url( $carrier_name, $tracking_number );
}
}
private function get_tracking_url( $carrier_name, $tracking_number ) {
switch ( $carrier_name ) {
case 'fedex':
$url = 'https://www.fedex.com/apps/fedextrack/?tracknumbers=' . $tracking_number;
break;
case 'ups':
$url = 'https://www.ups.com/track?loc=en_US&tracknum=' . $tracking_number;
break;
case 'usps':
$url = 'https://tools.usps.com/go/TrackConfirmAction?tLabels=' . $tracking_number;
break;
default:
return '';
}
return '<p><a href="' . esc_url( $url ) . '" target="_blank">' . __( 'Click here to track your shipment', 'cwpai' ) . '</a></p>';
}
}
new CWPAI_Shipping_Tracking();
Snippet Explanation
This is a PHP class called CWPAI_Shipping_Tracking, which adds a shipping tracking metabox to the order page in WooCommerce. The class has four methods:
- The constructor, which sets up various actions to add the metabox, add JavaScript, and display the tracking information on the order page and in the email notification.
- The add_metabox() method, which adds the metabox to the order page.
- The metabox_content() method, which displays the contents of the metabox.
- The add_js() method, which adds JavaScript to the footer of the admin pages.
- The add_tracking_info_to_email() method, which adds the tracking information to the email notification.
- The add_tracking_info_to_order_page() method, which adds the tracking information to the order page.
- The get_tracking_url() method, which generates the URL for the tracking information based on the carrier name and tracking number
The class makes use of various WordPress and WooCommerce functions, such as add_meta_box(), get_post_meta(), and selected(), as well as the Select2 JavaScript library. The class also includes some conditional logic to check whether the tracking information is available for a particular order and whether the carrier is supported for tracking.
FAQs About This Snippet
What is the purpose of this code snippet?
This code snippet is a part of a WordPress plugin that adds shipping tracking information to WooCommerce orders. It adds a metabox to the order page where the user can input the tracking number and carrier name. The plugin then uses this information to display the tracking information on the order page and in the email sent to the customer.
What does the add_metabox() function do?
The add_metabox() function is used to add a metabox to the order page where the user can input the tracking number and carrier name. It uses the WordPress function add_meta_box() to add the metabox and defines the content of the metabox in the metabox_content() function.
What is the purpose of the add_tracking_info_to_email() function?
The add_tracking_info_to_email() function is used to display the shipping tracking information in the email sent to the customer. It retrieves the tracking number and carrier name from the order metadata and displays them along with a tracking URL if the carrier is one of the supported carriers (FedEx, UPS, USPS).
What is the purpose of the add_js() function?
The add_js() function is used to add JavaScript to the order page that initializes the select2 library on the carrier name dropdown. This enhances the user experience by improving the appearance and functionality of the dropdown.
What is the purpose of the get_tracking_url() function?
The get_tracking_url() function is used to generate the tracking URL for the supported carriers (FedEx, UPS, USPS). It takes the carrier name and tracking number as arguments and returns the URL for the carrier's tracking page. This URL is then displayed to the user if the carrier is one of the supported carriers.