I am using the following API code to send an SMS when a new order is placed, the SMS API code is working to send SMS... Placed at the end of child theme in functions.php file... all is updated(WordPress 5.9.3 and woo-commerce 6.4 with PHP 8.0)
2 Issues:
Tried the following:
Documentation: https://www.textlocal.in/free-developer-sms-api/
Any suggestions to tweak the code so both the problems are solved?
Thanks
// Sending SMS to customers on new orders
add_action('woocommerce_new_order', 'custom_msg_customer_process_order', 10, 3);
function custom_msg_customer_process_order ($order_id) {
$order = new WC_Order( $order_id );
$order_date = $order->get_date_created();
$billing_phone = $order->get_billing_phone();
$apiKey = urlencode('apikey');
// Message details
$numbers = array($billing_phone,91xxxxxxxxxx);
$sender = urlencode('TXTCL');
$message = rawurlencode('Thank you for buying from us, a Wellness product. Your order number $order_id Dated $order_date is confirmed.');
$numbers = implode(',', $numbers);
// Prepare data for POST request
$data = array('apikey' => $apiKey, 'numbers' => $numbers, "sender" => $sender, "message" => $message);
// Send the POST request with cURL
$ch = curl_init('https://api.textlocal.in/send/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// Process your response here
echo $response;
}
While I don't know much about the texting API (and I sure hope your customers are opting in to text updates!), what I think is happening is that new WC_Order never reads your data from the database. How you should get a new order object is wc_get_order(). However, the woocommerce_new_order hook passes along the $order object as the 2nd parameter
So if we make sure our callback is expecting a second parameter there's no need to re-instantiate the order object.
As for part 2, woocommerce_new_order will fire when the order is saved to the DB and it doesn't matter what the order status is. Instead, I think we can use woocommerce_order_status_pending_to_processing which is what the new order email uses.
/**
* Sending SMS to customers on new orders.
*
* @param int $order_id The order ID. *
* @param WC_Order $order Order object.
*/
function custom_msg_customer_process_order( $order_id, $order ) {
$order_date = $order->get_date_created();
$billing_phone = $order->get_billing_phone();
$apiKey = urlencode('apikey');
// Message details
$numbers = array($billing_phone,91xxxxxxxxxx);
$sender = urlencode('TXTCL');
// Use sprintf() to replace placeholders with values.
$message = rawurlencode( sprintf( 'Thank you for buying from us, a Wellness product. Your order number %s Dated %s is confirmed.', $order_id, $order_date ) );
$numbers = implode(',', $numbers);
// Prepare data for POST request
$data = array('apikey' => $apiKey, 'numbers' => $numbers, "sender" => $sender, "message" => $message);
// Send the POST request with cURL
$ch = curl_init('https://api.textlocal.in/send/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// Process your response here
echo $response;
}
add_action('woocommerce_order_status_pending_to_processing', 'custom_msg_customer_process_order', 10, 2 );
Alternative
The above depends on your gateway setting the order to processing. If it does not (or if it wasn't initially pending) then it may not fire... since it only fires on the transition from pending to processing. A better hook might actually be woocommerce_payment_complete. But note that in source, it only passes 1 parameter to its callbacks... the $order_id (similar to the woocommerce_thankyou hook does). Therefore the code snippet would need to be adjusted to expect only a single parameter:
/**
* Sending SMS to customers on new orders.
*
* @param int $order_id The order ID. *
*/
function custom_msg_customer_process_order( $order_id ) {
$order = wc_get_order( $order_id );
// Quit early if not a valid order.
if ( ! $order ) {
return;
}
$order_date = wc_format_datetime( $order->get_date_created() );
$billing_phone = $order->get_billing_phone();
$apiKey = urlencode('apikey');
// Message details
$numbers = array($billing_phone,91xxxxxxxxxx);
$sender = urlencode('TXTCL');
// Use sprintf() to replace placeholders with values.
$message = rawurlencode( sprintf( 'Thank you for buying from us, a Wellness product. Your order number %s Dated %s is confirmed.', $order_id, $order_date ) ) );
$numbers = implode(',', $numbers);
// Prepare data for POST request
$data = array('apikey' => $apiKey, 'numbers' => $numbers, "sender" => $sender, "message" => $message);
// Send the POST request with cURL
$ch = curl_init('https://api.textlocal.in/send/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// Process your response here
echo $response;
}
add_action('woocommerce_payment_complete', 'custom_msg_customer_process_order' );