How to auto cancel on-hold orders instead pending Woocommerce? [duplicate] - php

I have managed to put this together after searching the web and yet, it does not work. My aim is to automatically cancel ALL orders with the status on-hold no matter the payment gateway if the order has not been paid after three days.
The code is obviously incomplete and I'm asking for help making it complete. I was testing it with -1 minute to see if anything happened. It did not.
function get_unpaid_orders() {
global $wpdb;
$unpaid_orders = $wpdb->get_col( $wpdb->prepare( "
SELECT posts.ID
FROM {$wpdb->posts} AS posts
WHERE posts.post_status = 'wc-on-hold'
AND posts.post_date < %s
", date( 'Y-m-d H:i:s', strtotime('-1 minute') ) ) );
return $unpaid_orders;
}
add_action( 'woocommerce_cancel_unpaid_submitted', 'cancel_unpaid_orders' );
function cancel_unpaid_orders() {
$unpaid_orders = get_unpaid_orders();
if ( $unpaid_orders ) {
foreach ( $unpaid_orders as $unpaid_order ) {
$order = wc_get_order( $unpaid_order );
$cancel_order = true;
foreach ( $order->get_items() as $item_key => $item_values) {
$manage_stock = get_post_meta( $item_values, '_manage_stock', true );
if ( $manage_stock == "yes" ) {
$payment_method = $order->get_payment_method();
if ( $payment_method == "bacs" ) {
$cancel_order = false;
}
}
}
if ( $cancel_order == true ) {
$order -> update_status( 'cancelled', __( 'The order was cancelled due to no payment from customer.', 'woocommerce') );
}
}
}
}

Update 4
Note: in WooCommerce, there is already a function hooked in woocommerce_cancel_unpaid_orders action hook that cancel unpaid orders after 7 days.
I didn't find woocommerce_cancel_unpaid_submitted action hook, so I don't know if it exist and if it is triggered.
Now there is some mistakes in your code and you can better use wc_get_orders() which give you directly the correct array of WC_Order Objects instead…
Here are some different ways to make it (last ones are untested):
1) This last solution is tested and works When shop manager or administrator user roles browse the Admin orders list (only executed once a day):
add_action( 'restrict_manage_posts', 'cancel_unpaid_orders' );
function cancel_unpaid_orders() {
global $pagenow, $post_type;
// Enable the process to be executed daily when browsing Admin order list
if( 'shop_order' === $post_type && 'edit.php' === $pagenow
&& get_option( 'unpaid_orders_daily_process' ) < time() ) :
$days_delay = 5; // <=== SET the delay (number of days to wait before cancelation)
$one_day = 24 * 60 * 60;
$today = strtotime( date('Y-m-d') );
// Get unpaid orders (5 days old)
$unpaid_orders = (array) wc_get_orders( array(
'limit' => -1,
'status' => 'on-hold',
'date_created' => '<' . ( $today - ($days_delay * $one_day) ),
) );
if ( sizeof($unpaid_orders) > 0 ) {
$cancelled_text = __("The order was cancelled due to no payment from customer.", "woocommerce");
// Loop through orders
foreach ( $unpaid_orders as $order ) {
$order->update_status( 'cancelled', $cancelled_text );
}
}
// Schedule the process to the next day (executed once restriction)
update_option( 'unpaid_orders_daily_process', $today + $one_day );
endif;
}
Code goes in function.php file of your active child theme (or active theme).
2) This third solution is tested and works: The function is triggered when any order change to "processing" or "completed" status (only executed once a day):
// Triggered on orders status change to "processing" or "completed"
add_action( 'woocommerce_order_status_changed', 'daily_cancel_unpaid_orders', 10, 4 );
function daily_cancel_unpaid_orders( $order_id, $old_status, $new_status, $order ) {
// Enable the process to be executed daily
if( in_array( $new_status, array('processing', 'completed') )
&& get_option( 'unpaid_orders_daily_process' ) < time() ) :
$days_delay = 5; // <=== SET the delay (number of days to wait before cancelation)
$one_day = 24 * 60 * 60;
$today = strtotime( date('Y-m-d') );
// Get unpaid orders (5 days old)
$unpaid_orders = (array) wc_get_orders( array(
'limit' => -1,
'status' => 'on-hold',
'date_created' => '<' . ( $today - ($days_delay * $one_day) ),
) );
if ( sizeof($unpaid_orders) > 0 ) {
$cancelled_text = __("The order was cancelled due to no payment from customer.", "woocommerce");
// Loop through WC_Order Objects
foreach ( $unpaid_orders as $unpaid_order ) {
$order->update_status( 'cancelled', $cancelled_text );
}
}
// Schedule the process to the next day (executed once restriction)
update_option( 'unpaid_orders_daily_process', $today + $one_day );
endif;
}
Code goes in function.php file of your active child theme (or active theme).
3) So you can try with woocommerce_cancel_unpaid_submitted action hook:
add_action( 'woocommerce_cancel_unpaid_submitted', 'cancel_unpaid_orders' );
function cancel_unpaid_orders() {
$days_delay = 5; // <=== SET the delay (number of days to wait before cancelation)
$one_day = 24 * 60 * 60;
$today = strtotime( date('Y-m-d') );
// Get unpaid orders (5 days old here)
$unpaid_orders = (array) wc_get_orders( array(
'limit' => -1,
'status' => 'on-hold',
'date_created' => '<' . ( $today - ($days_delay * $one_day) ),
) );
if ( sizeof($unpaid_orders) > 0 ) {
$cancelled_text = __("The order was cancelled due to no payment from customer.", "woocommerce");
// Loop through orders
foreach ( $unpaid_orders as $order ) {
$order->update_status( 'cancelled', $cancelled_text );
}
}
}
Code goes in function.php file of your active child theme (or active theme).
The function code should better works. For the hook I really don't know.
4) You can try also with woocommerce_cancel_unpaid_orders action hook instead.

Related

Exclude one specific payment gateway from auto-restock in WooCommerce

One of my payment getaway is ipay88 which only reduce stock after payment is cleared.
During the time when customer is making the payment, the order is under "pending payment" which the stock does not reduce. However, when customer does not make the payment in time, the order will automatically be marked as "cancelled".
As I have integrated with auto-restock plugin, "pending payment" to "cancelled" will increase the stock.
I would like to ask how do prevent restock for a specific payment getaway which is ipay88.
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'WC_Auto_Stock_Restore' ) ) {
class WC_Auto_Stock_Restore {
public function __construct() {
add_action( 'woocommerce_order_status_processing_to_cancelled', array( $this, 'restore_order_stock' ), 10, 1 );
add_action( 'woocommerce_order_status_on-hold_to_cancelled', array( $this, 'restore_order_stock' ), 10, 1 );
add_action( 'woocommerce_order_status_pending_to_cancelled', array( $this, 'restore_order_stock' ), 10, 1 );
add_action( 'woocommerce_order_status_completed_to_cancelled', array( $this, 'restore_order_stock' ), 10, 1 );
add_action( 'woocommerce_order_status_processing_to_refunded', array( $this, 'restore_order_stock' ), 10, 1 );
add_action( 'woocommerce_order_status_completed_to_refunded', array( $this, 'restore_order_stock' ), 10, 1 );
add_action( 'woocommerce_order_status_on-hold_to_refunded', array( $this, 'restore_order_stock' ), 10, 1 );
add_action( 'woocommerce_order_status_cancelled_to_completed', array( $this, 'remove_order_stock' ), 10, 1 );
add_action( 'woocommerce_order_status_cancelled_to_processing', array( $this, 'remove_order_stock' ), 10, 1 );
add_action( 'woocommerce_order_status_cancelled_to_on-hold', array( $this, 'remove_order_stock' ), 10, 1 );
add_action( 'woocommerce_order_status_cancelled_to_pending', array( $this, 'remove_order_stock' ), 10, 1 );
//add_action( 'woocommerce_order_status_changed', array( $this, 'adjust_order_stock' ), 10, 3 );
} // End __construct()
//public function adjust_order_stock( $order_id, $old_status, $new_status ) {
//$valid_new_statuses = array( 'complete' );
//if ( $old_status == cancelled AND $new_status )
//}
public function remove_order_stock( $order_id ) {
$order = new WC_Order( $order_id );
if ( ! get_option('woocommerce_manage_stock') == 'yes' && ! sizeof( $order->get_items() ) > 0 ) {
return;
}
foreach ( $order->get_items() as $item ) {
if ( $item['product_id'] > 0 ) {
$_product = $order->get_product_from_item( $item );
if ( $_product && $_product->exists() && $_product->managing_stock() ) {
$old_stock = $_product->stock;
$qty = apply_filters( 'woocommerce_order_item_quantity', $item['qty'], $this, $item );
$new_quantity = $_product->reduce_stock( $qty );
do_action( 'woocommerce_auto_stock_restored', $_product, $item );
$order->add_order_note( sprintf( __( 'Item #%s stock decremented from %s to %s.', 'woocommerce' ), $item['product_id'], $old_stock, $new_quantity) );
$order->send_stock_notifications( $_product, $new_quantity, $item['qty'] );
}
}
}
}
public function restore_order_stock( $order_id ) {
$order = new WC_Order( $order_id );
if ( ! get_option('woocommerce_manage_stock') == 'yes' && ! sizeof( $order->get_items() ) > 0 ) {
return;
}
foreach ( $order->get_items() as $item ) {
if ( $item['product_id'] > 0 ) {
$_product = $order->get_product_from_item( $item );
if ( $_product && $_product->exists() && $_product->managing_stock() ) {
$old_stock = $_product->stock;
$qty = apply_filters( 'woocommerce_order_item_quantity', $item['qty'], $this, $item );
$new_quantity = $_product->increase_stock( $qty );
do_action( 'woocommerce_auto_stock_restored', $_product, $item );
$order->add_order_note( sprintf( __( 'Item #%s stock incremented from %s to %s.', 'woocommerce' ), $item['product_id'], $old_stock, $new_quantity) );
$order->send_stock_notifications( $_product, $new_quantity, $item['qty'] );
}
}
}
} // End restore_order_stock()
}
$GLOBALS['wc_auto_stock_restore'] = new WC_Auto_Stock_Restore();
}
Updated, tested and fully functional for Woocommerce versions 3+
Your code is really obsolete since WooCommerce version 3. So I have revisited all the code…
Also instead using multiple hooks for each kind of status change, you can replace all the hooks by a unique one: woocommerce_order_status_changed…
Finally with that unique hook, you will need a unique function, where it will be possible to exclude easily your 'ipay88' payment gateway ID when order status is changed from 'pending' to 'cancelled'.
For the generated order note, It's better to get a unique order note when there is multiple items in the order…
So the complete code for this plugin is:
## ---- for WooCommerce versions 3+ - Plugin code version ---- ##
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'WC_Auto_Stock_Restore' ) ) {
class WC_Auto_Stock_Restore {
public function __construct() {
add_action( 'woocommerce_order_status_changed', array( $this, 'adjust_order_stock' ), 10, 3 );
}
public function adjust_order_stock( $order_id, $old_status, $new_status ) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
if ( ! get_option('woocommerce_manage_stock') == 'yes' && ! count( $items ) > 0 )
return; // We exit
// EXCEPTION for 'ipay88' payment method id, when Order status change from 'pending' to 'cancelled'
if( $order->get_payment_method() == 'ipay88' && $old_status == 'pending' && $new_status == 'cancelled' )
return; // We exit
// We don't need from 'pending' to 'refunded' order status
if( $old_status == 'pending' && $new_status == 'refunded' )
return; // We exit
// Group Order statuses in 2 groups (array)
$statuses_group1 = array( 'processing', 'on-hold', 'pending', 'completed' );
$statuses_group2 = array( 'cancelled', 'refunded' );
$change_stock = false;
$order_note = array();
## 1. Restore Order stock (7 order status change types)
if ( in_array( $old_status, $statuses_group1 ) && in_array( $new_status, $statuses_group2 ) ) {
$change_stock = true;
$change_type = 'increase';
$change_text = __( 'incremented', 'woocommerce' );
}
## 2. Restore Order stock (4 order status change types)
elseif ( $old_status == 'cancelled' && in_array( $new_status, $statuses_group1 ) ) {
$change_stock = true;
$change_type = 'decrease';
$change_text = __( 'decremented', 'woocommerce' );
}
## 3. Other cases
else return; // We exit
if( $change_stock ){
// Iterating through each order 'line_item'
// Since WC 3, $item is a WC_Order_Item_Product object
foreach ( $items as $item ) {
$product_id = $item->get_product_id(); // The product ID
if ( $product_id > 0 ) {
// Get tan instance of the WC_Product object
$product = $item->get_product();
if ( $product && $product->exists() && $product->managing_stock() ) {
// Get the product initial stock quantity (before update)
$initial_stock = $product->get_stock_quantity();
$item_qty = apply_filters( 'woocommerce_order_item_quantity', $item->get_quantity(), $this, $item );
// Update the product stock quantity
// Replace DEPRECATED methods: increase_stock() & discrease_stock()
wc_update_product_stock( $product, $item_qty, $change_type );
// Get the product updated stock quantity
$updated_stock = ( $change_type == 'increase' ) ? $initial_stock + $item_qty : $initial_stock - $item_qty;
do_action( 'woocommerce_auto_stock_restored', $product, $item );
// A unique Order note: Store each order note in an array…
$order_note[] = sprintf( __( 'Product ID #%s stock %s from %s to %s.', 'woocommerce' ), $product_id, $change_text, $initial_stock, $updated_stock);
// DEPRECATED & NO LONGER NEEDED - can be removed
//$order->send_stock_notifications( $product, $updated_stock, $item_qty );
}
}
}
// Adding a unique composite order note (for multiple items)
$order_notes = count($order_note) > 1 ? implode(' | ', $order_note) : $order_note[0];
$order->add_order_note( $order_notes );
}
}
}
$GLOBALS['wc_auto_stock_restore'] = new WC_Auto_Stock_Restore();
}
This should go in a plugin php file (see this related to WooCommerce plugins creation).
The normal version (for themes function.php file):
## ---- for WooCommerce versions 3+ - Theme code version ---- ##
add_action( 'woocommerce_order_status_changed', 'adjust_order_stock', 10, 3 );
function adjust_order_stock( $order_id, $old_status, $new_status ) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
if ( ! get_option('woocommerce_manage_stock') == 'yes' && ! count( $items ) > 0 )
return; // We exit
// EXCEPTION for 'ipay88' payment method id, when Order status change from 'pending' to 'cancelled'
if( $order->get_payment_method() == 'ipay88' && $old_status == 'pending' && $new_status == 'cancelled' )
return; // We exit
// We don't need from 'pending' to 'refunded' order status
if( $old_status == 'pending' && $new_status == 'refunded' )
return; // We exit
// Group Order statuses in 2 groups (array)
$statuses_group1 = array( 'processing', 'on-hold', 'pending', 'completed' );
$statuses_group2 = array( 'cancelled', 'refunded' );
$change_stock = false;
$order_note = array();
## 1. Restore Order stock (7 order status change types)
if ( in_array( $old_status, $statuses_group1 ) && in_array( $new_status, $statuses_group2 ) ) {
$change_stock = true;
$change_type = 'increase';
$change_text = __( 'incremented', 'woocommerce' );
}
## 2. Restore Order stock (4 order status change types)
elseif ( $old_status == 'cancelled' && in_array( $new_status, $statuses_group1 ) ) {
$change_stock = true;
$change_type = 'decrease';
$change_text = __( 'decremented', 'woocommerce' );
}
## 3. Other cases
else return; // We exit
if( $change_stock ){
// Iterating through each order 'line_item'
// Since WC 3, $item is a WC_Order_Item_Product object
foreach ( $items as $item ) {
$product_id = $item->get_product_id(); // The product ID
if ( $product_id > 0 ) {
// Get tan instance of the WC_Product object
$product = $item->get_product();
if ( $product && $product->exists() && $product->managing_stock() ) {
// Get the product initial stock quantity (before update)
$initial_stock = $product->get_stock_quantity();
$item_qty = apply_filters( 'woocommerce_order_item_quantity', $item->get_quantity(), $this, $item );
// Update the product stock quantity
// Replace DEPRECATED methods: increase_stock() & discrease_stock()
wc_update_product_stock( $product, $item_qty, $change_type );
// Get the product updated stock quantity
$updated_stock = ( $change_type == 'increase' ) ? $initial_stock + $item_qty : $initial_stock - $item_qty;
do_action( 'woocommerce_auto_stock_restored', $product, $item );
// A unique Order note: Store each order note in an array…
$order_note[] = sprintf( __( 'Product ID #%s stock %s from %s to %s.', 'woocommerce' ), $product_id, $change_text, $initial_stock, $updated_stock);
// DEPRECATED & NO LONGER NEEDED - can be removed
//$order->send_stock_notifications( $product, $updated_stock, $item_qty );
}
}
}
// Adding a unique composite order note (for multiple items)
$order_notes = count($order_note) > 1 ? implode(' | ', $order_note) : $order_note[0];
$order->add_order_note( $order_notes );
}
}
This code goes in function.php file of your active child theme (or theme).
All the code is tested and works only for WooCommerce versions 3+
Woocommerce Order related:
How to get WooCommerce order details
Order items and WC_Order_Item_Product object in Woocommerce 3

Send a custom reminder email for WooCommerce On-Hold orders after two days

My goal is to send an email to the customer containing custom text if the order status is on-hold and if the order creation time is 48 hours or more old.
order is 48 hours old or more
send email to customer
ask customer to pay
include a link to the order (to my account payment page)
I'm trying to use the code from an answer to one of my previous questions about Automatically cancel order after X days if no payment in WooCommerce.
I have lightly changes the code:
add_action( 'restrict_manage_posts', 'on_hold_payment_reminder' );
function on_hold_payment_reminder() {
global $pagenow, $post_type;
if( 'shop_order' === $post_type && 'edit.php' === $pagenow
&& get_option( 'unpaid_orders_daily_process' ) < time() ) :
$days_delay = 5;
$one_day = 24 * 60 * 60;
$today = strtotime( date('Y-m-d') );
$unpaid_orders = (array) wc_get_orders( array(
'limit' => -1,
'status' => 'on-hold',
'date_created' => '<' . ( $today - ($days_delay * $one_day) ),
) );
if ( sizeof($unpaid_orders) > 0 ) {
$reminder_text = __("Payment reminder email sent to customer $today.", "woocommerce");
foreach ( $unpaid_orders as $order ) {
// HERE I want the email to be sent instead <=== <=== <=== <=== <===
}
}
update_option( 'unpaid_orders_daily_process', $today + $one_day );
endif;
}
This is the email part that I want to sync with the above (read the code comments):
add_action ('woocommerce_email_order_details', 'on_hold_payment_reminder', 5, 4);
function on_hold_payment_reminder( $order, $sent_to_admin, $plain_text, $email ){
if ( 'customer_on_hold_order' == $email->id ){
$order_id = $order->get_id();
echo "<h2>Do not forget about your order..</h2>
<p>CUSTOM MESSAGE HERE</p>";
}
}
So how can I send an email notification reminder for "on-hold" orders with a custom text?
The following code will be triggered once daily and will send an email reminder with a custom message on unpaid orders (for "on-hold" order status):
add_action( 'restrict_manage_posts', 'on_hold_payment_reminder' );
function on_hold_payment_reminder() {
global $pagenow, $post_type;
if( 'shop_order' === $post_type && 'edit.php' === $pagenow
&& get_option( 'unpaid_orders_reminder_daily_process' ) < time() ) :
$days_delay = 2; // 48 hours
$one_day = 24 * 60 * 60;
$today = strtotime( date('Y-m-d') );
$unpaid_orders = (array) wc_get_orders( array(
'limit' => -1,
'status' => 'on-hold',
'date_created' => '<' . ( $today - ($days_delay * $one_day) ),
) );
if ( sizeof($unpaid_orders) > 0 ) {
$reminder_text = __("Payment reminder email sent to customer $today.", "woocommerce");
foreach ( $unpaid_orders as $order ) {
$order->update_meta_data( '_send_on_hold', true );
$order->update_status( 'reminder', $reminder_text );
$wc_emails = WC()->mailer()->get_emails(); // Get all WC_emails objects instances
$wc_emails['WC_Email_Customer_On_Hold_Order']->trigger( $order->get_id() ); // Send email
}
}
update_option( 'unpaid_orders_reminder_daily_process', $today + $one_day );
endif;
}
add_action ( 'woocommerce_email_order_details', 'on_hold_payment_reminder_notification', 5, 4 );
function on_hold_payment_reminder_notification( $order, $sent_to_admin, $plain_text, $email ){
if ( 'customer_on_hold_order' == $email->id && $order->get_meta('_send_on_hold') ){
$order_id = $order->get_id();
$order_link = wc_get_page_permalink('myaccount').'view-order/'.$order_id.'/';
$order_number = $order->get_order_number();
echo '<h2>'.__("Do not forget about your order.").'</h2>
<p>'.sprintf( __("CUSTOM MESSAGE HERE… %s"),
'<a href="'.$order_link.'">'.__("Your My account order #").$order_number.'<a>'
) .'</p>';
$order->delete_meta_data('_send_on_hold');
$order->save();
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Automatically cancel order after X days if no payment in WooCommerce

I have managed to put this together after searching the web and yet, it does not work. My aim is to automatically cancel ALL orders with the status on-hold no matter the payment gateway if the order has not been paid after three days.
The code is obviously incomplete and I'm asking for help making it complete. I was testing it with -1 minute to see if anything happened. It did not.
function get_unpaid_orders() {
global $wpdb;
$unpaid_orders = $wpdb->get_col( $wpdb->prepare( "
SELECT posts.ID
FROM {$wpdb->posts} AS posts
WHERE posts.post_status = 'wc-on-hold'
AND posts.post_date < %s
", date( 'Y-m-d H:i:s', strtotime('-1 minute') ) ) );
return $unpaid_orders;
}
add_action( 'woocommerce_cancel_unpaid_submitted', 'cancel_unpaid_orders' );
function cancel_unpaid_orders() {
$unpaid_orders = get_unpaid_orders();
if ( $unpaid_orders ) {
foreach ( $unpaid_orders as $unpaid_order ) {
$order = wc_get_order( $unpaid_order );
$cancel_order = true;
foreach ( $order->get_items() as $item_key => $item_values) {
$manage_stock = get_post_meta( $item_values, '_manage_stock', true );
if ( $manage_stock == "yes" ) {
$payment_method = $order->get_payment_method();
if ( $payment_method == "bacs" ) {
$cancel_order = false;
}
}
}
if ( $cancel_order == true ) {
$order -> update_status( 'cancelled', __( 'The order was cancelled due to no payment from customer.', 'woocommerce') );
}
}
}
}
Update 4
Note: in WooCommerce, there is already a function hooked in woocommerce_cancel_unpaid_orders action hook that cancel unpaid orders after 7 days.
I didn't find woocommerce_cancel_unpaid_submitted action hook, so I don't know if it exist and if it is triggered.
Now there is some mistakes in your code and you can better use wc_get_orders() which give you directly the correct array of WC_Order Objects instead…
Here are some different ways to make it (last ones are untested):
1) This last solution is tested and works When shop manager or administrator user roles browse the Admin orders list (only executed once a day):
add_action( 'restrict_manage_posts', 'cancel_unpaid_orders' );
function cancel_unpaid_orders() {
global $pagenow, $post_type;
// Enable the process to be executed daily when browsing Admin order list
if( 'shop_order' === $post_type && 'edit.php' === $pagenow
&& get_option( 'unpaid_orders_daily_process' ) < time() ) :
$days_delay = 5; // <=== SET the delay (number of days to wait before cancelation)
$one_day = 24 * 60 * 60;
$today = strtotime( date('Y-m-d') );
// Get unpaid orders (5 days old)
$unpaid_orders = (array) wc_get_orders( array(
'limit' => -1,
'status' => 'on-hold',
'date_created' => '<' . ( $today - ($days_delay * $one_day) ),
) );
if ( sizeof($unpaid_orders) > 0 ) {
$cancelled_text = __("The order was cancelled due to no payment from customer.", "woocommerce");
// Loop through orders
foreach ( $unpaid_orders as $order ) {
$order->update_status( 'cancelled', $cancelled_text );
}
}
// Schedule the process to the next day (executed once restriction)
update_option( 'unpaid_orders_daily_process', $today + $one_day );
endif;
}
Code goes in function.php file of your active child theme (or active theme).
2) This third solution is tested and works: The function is triggered when any order change to "processing" or "completed" status (only executed once a day):
// Triggered on orders status change to "processing" or "completed"
add_action( 'woocommerce_order_status_changed', 'daily_cancel_unpaid_orders', 10, 4 );
function daily_cancel_unpaid_orders( $order_id, $old_status, $new_status, $order ) {
// Enable the process to be executed daily
if( in_array( $new_status, array('processing', 'completed') )
&& get_option( 'unpaid_orders_daily_process' ) < time() ) :
$days_delay = 5; // <=== SET the delay (number of days to wait before cancelation)
$one_day = 24 * 60 * 60;
$today = strtotime( date('Y-m-d') );
// Get unpaid orders (5 days old)
$unpaid_orders = (array) wc_get_orders( array(
'limit' => -1,
'status' => 'on-hold',
'date_created' => '<' . ( $today - ($days_delay * $one_day) ),
) );
if ( sizeof($unpaid_orders) > 0 ) {
$cancelled_text = __("The order was cancelled due to no payment from customer.", "woocommerce");
// Loop through WC_Order Objects
foreach ( $unpaid_orders as $unpaid_order ) {
$order->update_status( 'cancelled', $cancelled_text );
}
}
// Schedule the process to the next day (executed once restriction)
update_option( 'unpaid_orders_daily_process', $today + $one_day );
endif;
}
Code goes in function.php file of your active child theme (or active theme).
3) So you can try with woocommerce_cancel_unpaid_submitted action hook:
add_action( 'woocommerce_cancel_unpaid_submitted', 'cancel_unpaid_orders' );
function cancel_unpaid_orders() {
$days_delay = 5; // <=== SET the delay (number of days to wait before cancelation)
$one_day = 24 * 60 * 60;
$today = strtotime( date('Y-m-d') );
// Get unpaid orders (5 days old here)
$unpaid_orders = (array) wc_get_orders( array(
'limit' => -1,
'status' => 'on-hold',
'date_created' => '<' . ( $today - ($days_delay * $one_day) ),
) );
if ( sizeof($unpaid_orders) > 0 ) {
$cancelled_text = __("The order was cancelled due to no payment from customer.", "woocommerce");
// Loop through orders
foreach ( $unpaid_orders as $order ) {
$order->update_status( 'cancelled', $cancelled_text );
}
}
}
Code goes in function.php file of your active child theme (or active theme).
The function code should better works. For the hook I really don't know.
4) You can try also with woocommerce_cancel_unpaid_orders action hook instead.

Limit to one applied coupon, removing other previous applied coupons in Woocommerce

I am making coupon dynamically to use user email as coupon but how can I restrict user to use only one coupon per cart. If use multiple auto removed the previous one from cart.
add_filter ( 'woocommerce_get_shop_coupon_data', 'generate_coupons', 10, 2 );
function generate_coupons( $data, $code) {
global $wpdb, $woocommerce;
$vpm_options = get_option( 'vpm_email_coupon_option_name' ); // Array of All Options
$amount = $vpm_options['coupon_value_3']; // coupon_discount
$offer_type = $vpm_options['offer_type_2']; // Type: fixed_cart, percent, fixed_product, percent_product
//getting the coupon input value
if (filter_var($code, FILTER_VALIDATE_EMAIL) && email_exists( $code )) {
$code = $code ;
}else{
$code = '';
}
// Check if the coupon has already been created in the database
$sql = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = 'shop_coupon' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1;", $code );
$coupon_id = $wpdb->get_var( $sql );
if ( empty( $coupon_id ) ) {
// Create a coupon with the properties you need
$data = array(
'discount_type' => $offer_type,
'coupon_amount' => $amount, // value
'product_ids' => array(),
'exclude_product_ids' => array(),
'usage_limit' => '',
'usage_limit_per_user' => '',//Limit
'limit_usage_to_x_items' => '',
'usage_count' => '',
'expiry_date' => '2020-12-31', // YYYY-MM-DD
);
// Save the coupon in the database
$coupon = array(
'post_title' => $code,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'shop_coupon'
);
$new_coupon_id = wp_insert_post( $coupon );
// Write the $data values into postmeta table
foreach ($data as $key => $value) {
update_post_meta( $new_coupon_id, $key, $value );
}
//apply the coupon
$woocommerce->cart->add_discount( $code );
return $data;
}
}
The following code will remove the first applied coupon, if customer applies another coupon code, so there will be only one applied coupon in cart (the last applied coupon):
add_action( 'woocommerce_before_calculate_totals', 'one_applied_coupon_only', 10, 1 );
function one_applied_coupon_only( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// For more than 1 applied coupons only
if ( sizeof($cart->get_applied_coupons()) > 1 && $coupons = $cart->get_applied_coupons() ){
// Remove the first applied coupon keeping only the last appield coupon
$cart->remove_coupon( reset($coupons) );
}
}
Code goes in function.php file of your active child theme (active theme). Tested and works.

Get daily orders count in Woocommerce

I want to get daily order count of daily whenever an order is posted. I wrote the below code to give a notification on slack whenever there is an order. As a result of displaying $result it showing the current order amount rather than the total order count of the day.
function wp_slack_woocommerce_order_status_completed2( $events ) {
$events['woocommerce_order_status_processing'] = array(
// Action in WooCommerce to hook in to get the message.
'action' => 'woocommerce_order_status_processing',
// Description appears in integration setting.
'description' => __( 'Whenever we receive an order', 'slack-woocommerce' ),
// Message to deliver to channel. Returns false will prevent
// notification delivery.
'message' => function( $order_id ) {
$order = wc_get_order( $order_id );
$date = is_callable( array( $order, 'get_date_completed' ) )
? $order->get_date_completed()
: $order->completed_date;
$url = add_query_arg(
array(
'post' => $order_id,
'action' => 'edit',
),
admin_url( 'post.php' )
);
$user_id = is_callable( array( $order, 'get_user_id' ) )
? $order->get_user_id()
: $order->user_id;
if ( $user_id ) {
$user_info = get_userdata( $user_id );
}
if ( ! empty( $user_info ) ) {
if ( $user_info->first_name || $user_info->last_name ) {
$username = esc_html( ucfirst( $user_info->first_name ) . ' ' . ucfirst( $user_info->last_name ) );
} else {
$username = esc_html( ucfirst( $user_info->display_name ) );
}
} else {
$billing_first_name = is_callable( array( $order, 'get_billing_first_name' ) )
? $order->get_billing_first_name()
: $order->billing_first_name;
$billing_last_name = is_callable( array( $order, 'get_billing_last_name' ) )
? $order->get_billing_last_name()
: $order->billing_last_name;
if ( $billing_first_name || $billing_last_name ) {
$username = trim( $billing_first_name . ' ' . $billing_last_name );
} else {
$username = __( 'Guest', 'slack-woocommerce' );
}
}
global $wpdb;
$date_from = '2018-02-27';
$date_to = '2018-02-28';
$post_status = implode("','", array('wc-processing', 'wc-completed') );
$result = $wpdb->get_results( "SELECT count(*) as total FROM $wpdb->posts
WHERE post_type = 'shop_order'
AND post_status IN ('{$post_status}')
AND post_date BETWEEN '{$date_from} 00:00:00' AND '{$date_to} 23:59:59'
");
// Remove HTML tags generated by WooCommerce.
add_filter( 'woocommerce_get_formatted_order_total', 'wp_strip_all_tags', 10, 1 );
$total = html_entity_decode( $order->get_formatted_order_total() );
remove_filter( 'woocommerce_get_formatted_order_total', 'wp_strip_all_tags', 10 );
//$data2=WC_API_Reports::get_sales_report( );
// Returns the message to be delivered to Slack.
return apply_filters( 'slack_woocommerce_order_status_completed_message',
sprintf(
__( 'Reveived new order with amount *%1$s*. Made by *%2$s* on *%3$s*. <%4$s|See detail> %s', 'slack-woocommerce' ),
$total,
$username,
$date,
$url,
$resullt
),
$order
);
},
);
return $events;
}
add_filter( 'slack_get_events', 'wp_slack_woocommerce_order_status_completed2' );
To get the daily order count you can use this custom function that will return the order count for the current day or the order count for a specific defined date:
function get_daily_orders_count( $date = 'now' ){
if( $date == 'now' ){
$date = date("Y-m-d");
$date_string = "> '$date'";
} else {
$date = date("Y-m-d", strtotime( $date ));
$date2 = date("Y-m-d", strtotime( $date ) + 86400 );
$date_string = "BETWEEN '$date' AND '$date2'";
}
global $wpdb;
$result = $wpdb->get_var( "
SELECT DISTINCT count(p.ID) FROM {$wpdb->prefix}posts as p
WHERE p.post_type = 'shop_order' AND p.post_date $date_string
AND p.post_status IN ('wc-on-hold','wc-processing','wc-completed')
" );
return $result;
}
Code goes in function.php file of your active child theme (or theme). Tested and works.
USAGE:
1) To get the orders count for the current date:
$orders_count = get_daily_orders_count();
2) To get the orders count for a specific date (with a format like 2018-02-28):
// Get orders count for february 25th 2018 (for example)
$orders_count = get_daily_orders_count('2018-02-25');

Resources