Skip to content

Instantly share code, notes, and snippets.

@zorem
Last active January 31, 2023 10:56
Show Gist options
  • Save zorem/3209b1304d6c63bbb813421c6c632193 to your computer and use it in GitHub Desktop.
Save zorem/3209b1304d6c63bbb813421c6c632193 to your computer and use it in GitHub Desktop.
Create Pending Payment customer SMS notification
<?php
// Send sms when pending payment after 1 hour of create order.
add_action('woocommerce_new_order', function ($order_id) {
// You can change trigger time of Pending payment notifications SMS
// if you want send notifications after 2 hour you should use 2 hour multiply by 3600
as_schedule_single_action( time() + 7200, 'send_pending_payment_sms', array( $order_id ) ); // You need to change only time instead of 7200 if you want to change instead of 2 hour.
}, 10, 1);
add_action( 'init', 'send_pending_notifications', 1, 1 );
function send_pending_notifications() {
add_action( 'send_pending_payment_sms', 'send_pending_payment_sms' );
}
function send_pending_payment_sms ( $order_id ) {
$order = wc_get_order( $order_id );
if ( class_exists( 'SMSWOO_Sms_Notification' ) && 'pending' == $order->get_status() ) {
$sms_notification = SMSWOO_Sms_Notification::get_instance();
$phone = $order->get_billing_phone( 'edit' );
// add pending payment SMS content here
$message = 'Hi , your order #' . $order_id . ' is pending payment'; // Replace SMS content here
$sms_provider = $sms_notification->get_sms_provider();
if ( ! $sms_provider ) {
return;
}
$sms_gateway = new $sms_provider();
try {
$customer_country = ! empty( $order ) ? $order->get_billing_country() : '';
$shop_country = substr( get_option( 'woocommerce_default_country' ), 0, 2 );
$country_code = ! empty( $customer_country ) ? $customer_country : $shop_country;
$calling_code = $sms_notification->get_calling_code( $country_code );
if ( '+' != substr( $phone, 0, 1 ) ) {
$phone = preg_replace( '/^0/', '', $phone );
$phone = $sms_notification->country_special_cases( $phone );
if ( substr( $phone, 0, strlen( $calling_code ) ) != $calling_code ) {
$phone = $calling_code . $phone;
}
}
$phone = preg_replace( '[\D]', '', $phone );
if ( substr( $phone, 0, strlen( $calling_code ) ) == $calling_code ) {
$phone = preg_replace( "/^{$calling_code}(\s*)?0/", $calling_code, $phone );
}
$status_message = __( 'Sent', 'sms-for-woocommerce' );
$sms_gateway->send( $phone, $message, $country_code );
$success = true;
} catch ( Exception $e ) {
$status_message = $e->getMessage();
$success = false;
}
$data = array(
'order_id' => $order_id,
'order_number' => $order_id ? wc_get_order( $order_id )->get_order_number() : '',
'user_id' => $order_id ? wc_get_order( $order_id )->get_user_id() : '',
'date' => current_time( 'Y-m-d H:i:s' ),
'to' => $phone,
'shipment_status' => 'Pending Payment',
'status' => $status_message,
'type' => 'SMS',
'sms_type' => 'order_sms',
);
SMSWOO()->admin->update_notification_table($data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment