Last active
February 26, 2020 18:00
-
-
Save Basilakis/5f4784d40f62cbd64008788bdf554c1d to your computer and use it in GitHub Desktop.
Change Status from Processing to Complete over WooCommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Auto Complete all WooCommerce orders. | |
*/ | |
add_filter( 'woocommerce_payment_complete_order_status', 'cg_update_order_status', 10, 2 ); | |
function cg_update_order_status( $order_status, $order_id ) { | |
$order = new WC_Order( $order_id ); | |
if ( 'processing' == $order_status && ( 'on-hold' == $order->status || 'pending' == $order->status || 'failed' == $order->status ) ) { | |
return 'completed'; | |
} | |
return $order_status; | |
} | |
/** | |
* Auto Complete all WooCommerce orders who are Virtual | |
*/ | |
add_action('woocommerce_order_status_changed', 'ts_auto_complete_virtual'); | |
function ts_auto_complete_virtual($order_id) | |
{ | |
if ( ! $order_id ) { | |
return; | |
} | |
global $product; | |
$order = wc_get_order( $order_id ); | |
if ($order->data['status'] == 'processing') { | |
$virtual_order = null; | |
if ( count( $order->get_items() ) > 0 ) { | |
foreach( $order->get_items() as $item ) { | |
if ( 'line_item' == $item['type'] ) { | |
$_product = $order->get_product_from_item( $item ); | |
if ( ! $_product->is_virtual() ) { | |
// once we find one non-virtual product, break out of the loop | |
$virtual_order = false; | |
break; | |
} | |
else { | |
$virtual_order = true; | |
} | |
} | |
} | |
} | |
// if all are virtual products, mark as completed | |
if ( $virtual_order ) { | |
$order->update_status( 'completed' ); | |
} | |
} | |
} | |
/** | |
* Auto Complete all WooCommerce orders with a loop into orders page | |
*/ | |
function auto_update_orders_status_from_processing_to_completed(){ | |
// Get all current "processing" customer orders | |
$processing_orders = wc_get_orders( $args = array( | |
'limit' => -1, | |
'post_status' => 'wc-processing', | |
) ); | |
if(!empty($processing_orders)) | |
foreach($processing_orders as $order) | |
$order->update_status( 'completed' ); | |
} | |
add_action( 'init', 'auto_update_orders_status_from_processing_to_completed' ); | |
/** | |
* Auto Complete all WooCommerce orders based on Payment Method | |
*/ | |
add_action('woocommerce_order_status_changed', 'ts_auto_complete_by_payment_method'); | |
function ts_auto_complete_by_payment_method($order_id) | |
{ | |
if ( ! $order_id ) { | |
return; | |
} | |
global $product; | |
$order = wc_get_order( $order_id ); | |
if ($order->data['status'] == 'processing') { | |
$payment_method=$order->get_payment_method(); | |
if ($payment_method!="cod") | |
{ | |
$order->update_status( 'completed' ); | |
} | |
} | |
} | |
/** | |
* Auto Complete all WooCommerce orders where total spend is more than 50 | |
*/ | |
add_action( 'woocommerce_order_status_processing', 'auto_complete_processing_orders_based_on_total', 20, 4 ); | |
function auto_complete_processing_orders_based_on_total( $order_id, $order ){ | |
// HERE define the max total order amount | |
$max_total_limit = 50; | |
if ( $order->get_total() < $max_total_limit ) { | |
$order->update_status( 'completed' ); | |
} | |
} | |
/** | |
* Auto Complete all WooCommerce orders where total spend is more than 50 if status is always process ( no autocomplete ) | |
*/ | |
add_action( 'woocommerce_thankyou', 'thankyou_auto_complete_processing_orders_based_on_total', 90, 1 ); | |
function thankyou_auto_complete_processing_orders_based_on_total( $order_id ){ | |
if( ! $order = wc_get_order( $order_id ) ){ | |
return; | |
} | |
// HERE define the max total order amount | |
$max_total_limit = 50; | |
if( $order->has_status('processing') && $order->get_total() < $max_total_limit ){ | |
$order->update_status( 'completed' ); | |
} | |
} | |
/** | |
* Auto Complete all WooCommerce orders. | |
*/ | |
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order'); | |
function custom_woocommerce_auto_complete_order( $order_id ) { | |
if ( ! $order_id ) { | |
return; | |
} | |
$order = wc_get_order( $order_id ); | |
$order->update_status( 'completed' ); | |
?> | |
<script type="text/javascript"> | |
window.onload = function() { | |
if(!window.location.hash) { | |
window.location = window.location + '#loaded'; | |
window.location.reload(); | |
} | |
} | |
</script> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment