Skip to content

Instantly share code, notes, and snippets.

@kshojib
Last active February 1, 2023 07:13
Show Gist options
  • Save kshojib/e6f190055f23c0c9b78eab6abf767a6f to your computer and use it in GitHub Desktop.
Save kshojib/e6f190055f23c0c9b78eab6abf767a6f to your computer and use it in GitHub Desktop.
Send email to customer when order is shipped via Boxtal Connect plugin.
<?php
use Boxtal\BoxtalConnectWoocommerce\Util\Order_Util;
use Boxtal\BoxtalConnectWoocommerce\Order\Controller;
// send email to custom with tracking number on order shipped
add_action('boxtal_connect_order_shipped', 'send_tracking_number', 10, 2);
function send_tracking_number($order_id) {
$order = wc_get_order($order_id);
$email = $order->get_billing_email();
$subject = 'Votre commande a été expédiée';
$controller = new Controller(
array(
'url' => null,
'version' => null,
)
);
$tracking = $controller->get_order_tracking( Order_Util::get_id( $order ) );
$message = '';
$message .= '<p style="margin-bottom: 30px;">Bonjour,</p>';
if ( null !== $tracking && property_exists( $tracking, 'shipmentsTracking' ) && ! empty( $tracking->shipmentsTracking ) ) {
if ( 1 === count( $tracking->shipmentsTracking ) ){
$message .= '<p>';
$message .= __('Your order has been sent in 1 shipment.', 'boxtal-connect');
$message .= '</p>';
}else{
$message .= '<p>';
$message .= sprintf( __( 'Your order has been sent in %s shipments.', 'boxtal-connect' ), count( $tracking->shipmentsTracking ) ) ;
$message .= '</p>';
}
foreach ( $tracking->shipmentsTracking as $shipment ) {
$message .= '<h4>';
$message .= sprintf( __( 'Shipment reference %s', 'boxtal-connect' ), $shipment->reference );
$message .= '</h4>';
$parcel_count = count( $shipment->parcelsTracking );
if ( 1 === $parcel_count || 0 === $parcel_count ) {
$message .= '<p>';
$message .= sprintf( __( 'Your shipment has %s package.', 'boxtal-connect' ), $parcel_count );
$message .= '</p>';
}else {
$message .= '<p>';
$message .= sprintf( __( 'Your shipment has %s packages.', 'boxtal-connect' ), $parcel_count );
$message .= '</p>';
}
foreach ( $shipment->parcelsTracking as $parcel ) {
if ( null !== $parcel->trackingUrl ) {
$message .= '<p>';
$message .= sprintf( __( 'Package reference %s', 'boxtal-connect' ), '<a href="' . esc_url( $parcel->trackingUrl ) . '" target="_blank">' . $parcel->reference . '</a>' );
$message .= '</p>';
}else {
$message .= '<p>';
$message .= sprintf( __( 'Package reference %s', 'boxtal-connect' ), $parcel->reference );
$message .= '</p>';
}
}
}
}
$message .= '<p style="margin-top: 50px;"></p>';
$headers = array('Content-Type: text/html; charset=UTF-8');
$site_name = get_bloginfo('name');
$from = 'From: ' . $site_name . ' <[email protected]>';
$headers[] = $from;
wp_mail($email, $subject, $message, $headers);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment