Sending an SMS for specific email notifications and order statuses - php

With WooCommerce I use a special theme that handle bookings for motorbikes and scooters rental service. I want to get the order related data. I am trying to send an SMS when an email notification is sent to customer for completed, on hold, pending and **processing** order status.
I have use the code below for instance that output the data I need in SMS:
$order = new WC_Order($order_id);
$status = $order->get_status(); // order status
if( 'completed' == $status || 'processing' == $status || 'pending' == $status || 'on-hold' == $status ){
$user_phone = get_post_meta($order_id, '_billing_phone', true);
foreach ($order->get_items() as $item_id => $item) {
$product_id = $order->get_item_meta($item_id, '_product_id', true); // product ID
$product_name = get_post($product_id)->post_title; // Product description
// Related Booking data to insert in SMS
$book_check_in = $order->get_item_meta( $item_id, '_st_check_in', true );
$book_check_out = $order->get_item_meta( $item_id, '_st_check_out', true );
$book_pick_up = $order->get_item_meta( $item_id, '_st_pick_up', true );
$book_drop_off = $order->get_item_meta( $item_id, '_st_drop_off', true );
}
// Send SMS in SMS API
file_get_contents("http://144.76.39.175/api.php?username=xxxxxxxxxxx&password=xxxxxxxxxxx&route=1&message%5B%5D=The+message&sender=NBWREN&mobile%5B%5D=xxxxxxxxxxx");
}
This is not working. Where should I hook this code? I tried different templates and all I got were some 500 errors or simply nothing happened.
Please give me some help.
Thanks

You can use a custom function hooked in woocommerce_email_order_details hook, using included $order and $email objects.
You will be able to rearrange the message as you like, as this is just an example.
I have commented this code to make you understand how it works:
add_action('woocommerce_email_order_details', 'send_sms_on_email_notifications', 10, 4);
function send_sms_on_email_notifications($order, $sent_to_admin, $plain_text, $email){
$order_id = $order->id; // get the order ID for Order object
$email_id = $email->id; // get the email ID for Email object
$order_status = $order->get_status(); // Get order Status
// Array of Email IDs to avoid Admin email notifications (SMS sent twice on some notifications)
$emails_ids_exceptions = array('new_order', 'failed_order', 'customer_invoice', 'customer_note');
// Your targeted order status
$order_statuses = array('completed', 'processing', 'on-hold', 'pending');
$send_the_sms = false;
// Just for your targeted order statuses
if( in_array( $order_status, $order_statuses ) ):
// iterating in the order items
foreach($order->get_items() as $item_id => $item):
$prod_id = $order->get_item_meta( $item_id, '_product_id', true ); // product ID
$prod_name = get_post($prod_id)->post_title; // Product Name
$mobile = get_post_meta($order_id, '_billing_phone', true); // mobile phone
// Related Booking data to insert in SMS
$check_in = $order->get_item_meta( $item_id, '_st_check_in', true );
$check_out = $order->get_item_meta( $item_id, '_st_check_out', true );
$pick_up = $order->get_item_meta( $item_id, '_st_pick_up', true );
$drop_off = $order->get_item_meta( $item_id, '_st_drop_off', true );
// stoping the loop (just for one item)
break;
endforeach;
// Limiting to customer email notifications
if( !in_array( $email_id, $emails_ids_exceptions ) )
{
// inserting the order data (variables) in the message
$text = "Your order $order_id with $status status, for $prod_name. Your booking details: Check in time: $check_in, Check out Time: $check_out, Pick up $pick_up and drop of Time is $drop_off";
$send_the_sms = true;
}
// TRIGGERING THE SMS
if($send_the_sms)
{
// Replacing spaces by '+' in the message
$message = str_replace(' ', '+', $text);
// Inserting the message and the user number phone in the URL
$url = "http://144.76.39.175/api.php?username=xxxxxxxxxxx&password=xxxxxxxxxxx&route=1&message%5B%5D=$message&sender=NBWREN&mobile%5B%5D=$mobile";
// Triggering the SMS
file_get_contents($url);
}
endif;
}
This code will work for the first item of the order, assuming that people rent one bike or one scooter at the time.
The code is mainly tested, but I can't guaranty the triggered SMS as I can't test it on your SMS API. I hope this will work… Let me know.
Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.

Related

Disable order received emails for virtual products

I am currently using the code below to automatically complete virtual and downloadable products.
add_action('woocommerce_thankyou', 'wpd_autocomplete_virtual_orders', 10, 1 );
function wpd_autocomplete_virtual_orders( $order_id ) {
if( ! $order_id ) return;
// Get order
$order = wc_get_order( $order_id );
// get order items = each product in the order
$items = $order->get_items();
// Set variable
$only_virtual = true;
foreach ( $items as $item ) {
// Get product id
$product = wc_get_product( $item['product_id'] );
// Is virtual
$is_virtual = $product->is_virtual();
// Is_downloadable
$is_downloadable = $product->is_downloadable();
if ( ! $is_virtual && ! $is_downloadable ) {
$only_virtual = false;
}
}
// true
if ( $only_virtual ) {
$order->update_status( 'completed' );
}
}
The issue I am facing is that woocommerce sends out two emails in this scenario to the customer. Order Received + Order Completed. I am wondering if there is anyway to stop the order received email from going through to the customer and only have the order completed email sent.
Thank You
To prevent the "Order Received" email from being sent to the customer, you can use the woocommerce_email_enabled_customer_processing_order filter hook to disable the email.
Please try to add the following code inside your if ( $only_virtual )
add_filter( 'woocommerce_email_enabled_customer_processing_order', '__return_false' );
The easiest way to achieve this would be to bypass the WooCommerce update_status function as the email sending is built-in to the function.
Instead just update the order in wp_posts:
Replace: $order->update_status( 'completed' );
With:
wp_update_post(['ID' => $order_id, 'post_status' => 'wc-completed']);
You will then only receive the Order Received email!

Add custom text 'per item' based on product category in WooCommerce email notifications

How to add a custom text 'per item' based on product category on all WooCommerce emails?
Therefore I tried to modify Add a custom text for a particular product on specific email notification in Woocommerce and Display a custom text based on product category in WooCommerce Email answer code to my needs.
// Setting the email_is as a global variable
add_action('woocommerce_email_before_order_table', 'the_email_id_as_a_global', 1, 4);
function the_email_id_as_a_global($order, $sent_to_admin, $plain_text, $email ){
$GLOBALS['email_id_str'] = $email->id;
}
// Displaying product description in new email notifications
add_action( 'woocommerce_order_item_meta_end', 'product_description_in_new_email_notification', 10, 3 );
function product_description_in_new_email_notification( $product_cat, $cat, $order = null ){
// HERE define your targetted product CAT
$targeted_id = 'x';
// HERE define the text information to be displayed for the targeted product id
$text_information = __("There is an offer in this particular item", "woocommerce");
// Getting the email ID global variable
$refNameGlobalsVar = $GLOBALS;
$email_id = $refNameGlobalsVar['email_id_str'];
// If empty email ID we exit
if(empty($email_id)) return;
// Only for "New Order email notification" for your targeted product ID
if ( 'customer_completed_order' == $email_id && 'new_order' == $email_id
in_array( $targeted_id, array( $cat->get_product_cat(), $item->get_variation_id() ) ) ) {
// Display the text
echo '<div class="product-text" style="margin-top:10px"><p>' . $text_information . '</p></div>';
}
}
Unfortunately without the desired result. I would like to display custom text under an order item name for all products of a particular product category in all email notifications (this is per item, not per email). Any advice?
Some notes on your code attempt:
$product_cat and $cat do not exist as arguments for the woocommerce_order_item_meta_end hook
$cat->get_product_cat() does not exist and is incorrect
Use has_term() WordPress function to check if the current post has any of given terms
So you get:
// Setting global variable
function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {
$GLOBALS['email_data'] = array(
'email_id' => $email->id, // The email ID (to target specific email notification)
'is_email' => true // When it concerns a WooCommerce email notification
);
}
add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 1, 4 );
// Displaying description
function action_woocommerce_order_item_meta_end( $item_id, $item, $order, $plain_text ) {
// Getting the custom 'email_data' global variable
$ref_name_globals_var = $GLOBALS;
// Isset & NOT empty
if ( isset ( $ref_name_globals_var ) && ! empty( $ref_name_globals_var ) ) {
// Isset
$email_data = isset( $ref_name_globals_var['email_data'] ) ? $ref_name_globals_var['email_data'] : '';
// NOT empty
if ( ! empty( $email_data ) ) {
// Specific categories: the term name/term_id/slug. Several could be added, separated by a comma
$categories = array( 108, 1, 'categorie-1' );
// The text information
$text_information = __( 'There is an offer in this particular item', 'woocommerce' );
// Specific email notifications: multiple statuses can be added, separated by a comma
$email_ids = array( 'new_order', 'customer_processing_order', 'customer_completed_order', 'customer_on_hold_order' );
// Targeting specific email notifications AND check if the current post has any of given terms
if ( in_array( $email_data['email_id'], $email_ids ) && has_term( $categories, 'product_cat', $item->get_product_id() ) ) {
// Display the text
echo '<p>' . $text_information . '</p>';
}
}
}
}
add_action( 'woocommerce_order_item_meta_end', 'action_woocommerce_order_item_meta_end', 10, 4 );

PHP to take data from WCFM Order meta, and push into AST plugin

While im pretty hopeless at php, i feel like im getting close on this one, i just cant quite lock it in
So we have 2 plugins in the mix here WCFM & AST
In WCFM, a vendor adds a tracking number and carrier this is store in the order meta data with a meta keys of
wcfm_tracking_code
AND
wcfm_tracking_url
i found this hook elesewhere that allows me to fire a set of things to happen, once the tracking has been submitted into the system
“wcfm_after_order_mark_shipped” (Parameters -> $order_id, $order_item_id, $tracking_code, $tracking_url)
so now i want to take the above 2 order meta datas and push them into another plugin called AST
They code they provide to push data into thier system is:
<?php if ( class_exists( 'WC_Advanced_Shipment_Tracking_Actions' ) ) {
$order_id = '123'; //Replace with your order id
$tracking_provider = 'USPS'; //Replace with your shipping provider
$tracking_number = '123123'; //Replace with your tracking number
$date_shipped = '2020-06-22'; ////Replace with your shipped date
$status_shipped = 1; // 0=no,1=shipped,2=partial shipped(if partial shipped order status is enabled)
$sku = 't-shirt,blue-jeans'; //the line item (product) SKU
$qty = '1,1'; //the line item (product) quantity
if ( function_exists( 'ast_insert_tracking_number' ) ) {
ast_insert_tracking_number( $order_id, $tracking_number, $tracking_provider, $date_shipped, $status_shipped, $sku, $qty );
}
}
SO using both codes provided, can anyone show me how i can take the metadata input from WCFM and push it into AST?
Ive hired someone on upwork, and they cant even figure it out, but it seems so straightforward.
This is the code we have,it pushes the data in but it comes in looking like this
Instead of this like it should
add_action( 'wcfm_after_order_mark_shipped', 'testwr', 99, 6 );
function testwr( $order_id, $order_item_id, $tracking_code, $tracking_url, $product_id, $wcfm_tracking_data ) {
$order = wc_get_order( $order_id );
$data['order_id'] = $order_id;
$data['tracking_provider'] = $tracking_url;
$data['wcfm_tracking_code'] = $wcfm_tracking_data['wcfm_tracking_code'];
$data['date_shipped'] = $order->order_date;
$data['status_shipped'] = 1;
extract($data);
foreach ($order->get_items() as $item) {
$product = wc_get_product($item->get_product_id());
$item_quantity[] = $item->get_quantity();
$item_sku[] = $product->get_sku();
}
if( $item_sku ) {
$data['sku'] = implode( ",",$item_sku );
}
$tracking_url = $tracking_url . "/" . $tracking_code;
$url = "<a href='".$tracking_url."'>test</a>";
$data['qty'] = implode( ",",$item_quantity );
$test = ast_insert_tracking_number( $order_id, $tracking_url, $tracking_url, $date_shipped, $status_shipped, $sku, $qty);
var_dump($test);
print_r($tracking_code);
print_r($data);
print_r($wcfm_tracking_data);
var_dump($order_item_id);
//ast_insert_tracking_number
die();
}
Kudos to anyone willing to give this a try and some time
Regards

Get used coupon codes and related discount amounts from WooCommerce orders

I need to insert in a custom plugin the code to get the name of the discount codes I enter in the settings, the discount obtained with the code and the total.
Based on Get coupon data from WooCommerce orders answer code, I have inserted the following code:
$order = wc_get_order( $order_id );
// GET THE ORDER COUPON ITEMS
$order_items = $order->get_items('coupon');
// print_r($order_items); // For testing
// LOOP THROUGH ORDER COUPON ITEMS
foreach( $order_items as $item_id => $item ){
// Retrieving the coupon ID reference
$coupon_post_obj = get_page_by_title( $item->get_name(), OBJECT, 'shop_coupon' );
$coupon_id = $coupon_post_obj->ID;
// Get an instance of WC_Coupon object (necessary to use WC_Coupon methods)
$coupon = new WC_Coupon($coupon_id);
## Filtering with your coupon custom types
if( $coupon->is_type( 'fixed' ) || $coupon->is_type( 'percent' ) || $coupon->is_type( 'fixed_product' ) ){
// Get the Coupon discount amounts in the order
$order_discount_amount = wc_get_order_item_meta( $item_id, 'discount_amount', true );
$order_discount_tax_amount = wc_get_order_item_meta( $item_id, 'discount_amount_tax', true );
## Or get the coupon amount object
$coupons_amount = $coupons->get_amount();
}
}
$confirmation = str_ireplace("{order_items}", $order_items, $confirmation);
But the only information it brings back to me, when I do an echo is the word "array".
What am I doing wrong? Any help?
Try the following instead, that will add a coma separated string of applied coupon codes with their respective discount amount:
$order = wc_get_order( $order_id ); // If needed
$output = array(); // Initializing
// loop through order items "coupon"
foreach( $order->get_items('coupon') as $item_id => $item ){
// Get the coupon array data in an unprotected array
$data = $item->get_data();
// Format desired coupon data for output
$output[] = $data['code'] . ': ' . strip_tags( wc_price( $data['discount'] + $data['discount_tax'] ) );
}
$confirmation = str_ireplace("{order_items}", implode(', ', $output), $confirmation);

How to change sender email based on product id in WooCommerce

I am trying to change the sender email for all emails related to two products in WooCommerce, but not for all the others.
I have the code below to change the sender email, but I am not sure how to make it work for only those two products (by product id or category).
function change_sender_email( $original_email_address ) {
return 'admin#example.com';
}
add_filter( 'wp_mail_from', 'change_sender_email' );
Could I somehow use the filter 'woocommerce_email_recipient_customer_completed_order'?
I know how to use that to conditionally change the recipient of the email, but I couldn't get it to work to change the sender email.
You can use: woocommerce_email_from_address
// Change email sender address
function my_email_from_address( $from_email, $wc_email ) {
// Get the WC_Order object instance
$order = $wc_email->object;
// Get items
$items = $order->get_items();
// Loop through
foreach ( $items as $item ) {
// Get product ID
$product_id = $item->get_product_id();
// Compare
if ( $product_id == 30 ) {
$from_email = 'my.email1#stackoverflow.com';
} elseif ( $product_id == 32 ) {
$from_email = 'my.email2#stackoverflow.com';
}
}
return $from_email;
}
add_filter( 'woocommerce_email_from_address', 'my_email_from_address', 20, 2 );

Categories