I would like to automark just success paid orders to "Completed" status. I have searched a lot on Stack and Google, and found this answer code:
WooCommerce: Auto complete paid Orders (depending on Payment methods)
But problem is that the code mark all placed orders to "Completed" status not depending if order was success placed or not.
What do I need to change in the code to automark ONLY Paid orders to "Completed" status?
New enhanced and simplified code version replacement (March 2019):
See: WooCommerce: Auto complete paid orders
Original answer:
For Paypal and other third party gateways, the "paid" order status to target is "processing" (and "completed"), so you can lightly change the code to:
add_action( 'woocommerce_thankyou', 'wc_auto_complete_paid_order', 20, 1 );
function wc_auto_complete_paid_order( $order_id ) {
if ( ! $order_id )
return;
// Get an instance of the WC_Product object
$order = wc_get_order( $order_id );
// No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
if ( in_array( $order->get_payment_method(), array( 'bacs', 'cod', 'cheque', '' ) ) ) {
return;
// Updated status to "completed" for paid Orders with all others payment methods
} elseif ( in_array( $order->get_status(), array('on-hold', 'processing') ) ) {
$order->update_status( 'completed' );
}
}
Code goes in function.php file of the active child theme (or active theme). tested and works.
This way you will avoid "failed", "Cancelled" or "Pending" orders.
Related
On Woocommerce we have the option for BACS payments. Some orders are coming through as "Paid" and some aren't. I can't understand why as they are using the exact same payment method. The two images below will show you this:
Paid:
Not Paid:
We are using a function to automatically change these payments from "On Hold" to "Processing", in case this may have something to do with the issue. The code is below:
add_action( 'woocommerce_thankyou', 'bacs_order_payment_processing_order_status', 10, 1 );
function bacs_order_payment_processing_order_status( $order_id ) {
if ( ! $order_id ) {
return;
}
// Get an instance of the WC_Order object
$order = new WC_Order( $order_id );
if ( ( get_post_meta($order->id, '_payment_method', true) == 'bacs' || 'cod' ) && ('on-hold' == $order->status || 'pending' == $order->status) ) {
$order->update_status('processing');
}
else {
return;
}
}
// change COD payment method order status from processing to on-hold
add_action('woocommerce_thankyou_cod', 'action_woocommerce_thankyou_cod', 10, 1);
function action_woocommerce_thankyou_cod($order_id)
{
$order = wc_get_order($order_id);
$order->update_status('processing');
}
Any help would be massively appreciated!
Your code is very outdated, confused with mistakes…
Instead, to change order status for payments like:
Cash on delivery cod use (default status is "processing"):
Change COD default order status to "On Hold" instead of "Processing" in Woocommerce
Cash on Cheque cheque use *(default status is "on-hold"):
Change default WooCommerce order status to processing for cheque and bacs payments
bank wire bacs use *(default status is "on-hold"):
WooCommerce change order status BACS processing
Change default WooCommerce order status to processing for cheque and bacs payments
For other payment gateways use (default order status is related to each payment gateway and if there are shippable items or not):
WooCommerce: Auto complete paid orders
Auto process paid orders instead of auto complete in WooCommerce
In WooCommerce, I need all my orders to go immediately to "processing" status to have the order-processing email sent directly when the order is processed.
By default, this behavior exist for Paypal and COD orders, but not for BACS and Cheque where the default status is on-hold.
I tried several snippets like this one:
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_process_order' );
function custom_woocommerce_auto_process_order( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
$order->update_status( 'processing' );
}
But this doesn't work, the order still shows up in "on-hold" status and the processing email notification is not sent. Now I just found a this snippet:
add_filter( 'woocommerce_bacs_process_payment_order_status', function( $status = 'on_hold', $order = null ) {
return 'processing';
}, 10, 2 );
And it works, but only for "BACS". How can I adapt it to also work for "Cheque" orders?
The filter hook woocommerce_cheque_process_payment_order_status is not yet implemented in Woocommerce 3.5.7 … if you look to the file located in your woocommerce plugin under: includes > gateways > cheque > class-wc-gateway-cheque.php, the hook is missing (line 122):
$order->update_status( 'on-hold', _x( 'Awaiting check payment', 'Check payment method', 'woocommerce' ) );
But on Github WC version 3.5.7 for class-wc-gateway-cheque.php file, the hook exist (line 122):
$order->update_status( apply_filters( 'woocommerce_cheque_process_payment_order_status', 'on-hold', $order ), _x( 'Awaiting check payment', 'Check payment method', 'woocommerce' ) );
The hook will is planed to be available next WooCommerce 3.6 release, see the file change on Woocommerce Github. It's tagged 3.6.0-rc.2 and 3.6.0-beta.1
So it will be possible to change the default order status to "processing" for "bacs" and "cheque" payment methods, using the following:
add_filter( 'woocommerce_bacs_process_payment_order_status','filter_process_payment_order_status_callback', 10, 2 );
add_filter( 'woocommerce_cheque_process_payment_order_status','filter_process_payment_order_status_callback', 10, 2 );
function filter_process_payment_order_status_callback( $status, $order ) {
return 'processing';
}
Code goes in functions.php file of your active child theme (or active theme).
You're almost there. Right now you are adding a filter for the BACS hook. There is a similar hook for Cheque payment method.
Simply add the following code:
add_filter(
'woocommerce_cheque_process_payment_order_status',
function( $status = 'on_hold', $order = null ) {
return 'processing';
}, 10, 2
);
It does the exact same, but just for the Cheque orders.
Is it possible to set all new orders to on hold without them going to processing first?
I need to capture the funds, but the order needs to go as on-hold rather than processing so that I can make sure the payment is received before triggering the email to our supplier.
I have found some code that allows me to change all new orders to on-hold, however when payment is authorized the order is set to processing.
pending payment --> processing (triggers email) --> on-hold
Screenshot of Order Notes that show this
This is the code that I found:
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_onhold_order' );
function custom_woocommerce_auto_onhold_order( $order_id ) {
global $woocommerce;
if ( !$order_id )
return;
$order = new WC_Order( $order_id );
$order->update_status( 'on-hold' ); //All new orders go to "on-hold"
}
I've been looking for the action which triggers the order status to be set to processing on payment authorization however I cannot find it.
All help is greatly appreciated!
Edit: I've managed to stumble upon a snippet of code that appears to work.
add_filter( 'woocommerce_payment_complete_order_status', 'custom_update_order_status', 10, 2 );
function custom_update_order_status( $order_status, $order_id ) {
return 'on-hold';
}
All orders start with a "pending" status in Woocommerce. That status is set before they go through the payment gateway...
There is no email notifications for "Pending" orders staus
You can try the following, that will set the status "on-hold" on order creation (The update status action comes once the order has been created ans saved in database):
add_action( 'woocommerce_checkout_create_order', 'force_new_order_status', 20, 1 );
function force_new_order_status( $order ) {
if( ! $order->has_status('on-hold') )
$order->set_status( 'on-hold', 'Forced status by a custom script' );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
I have integrated a payment gateway to accept online payments for my store running on woocommerce. Everything works fine but I noticed that woocommerce is changing the order status to wc-processing for all the online paid orders by default.
As per my store's functionality I want all the online paid orders to be in wc-on-hold status initially.
Is there any way to stop woocommerce changing the order status to wc-processing programatically?
Here it is a code snippet based on this thread. We use here woocommerce_thankyou (that is fired just after payment has been done) to hook our function, converting 'processing' orders status to 'on-hold':
add_action( 'woocommerce_thankyou', 'custom_woocommerce_paid_order_status', 10, 1 );
function custom_woocommerce_paid_order_status( $order_id ) {
if ( ! $order_id ) {
return;
}
global $woocommerce;
$order = new WC_Order( $order_id );
// 'processing' orders status are converted to 'on-hold'.
if ( is_object($order) && $order->has_status( 'processing' ) {
$order->update_status( 'on-hold' );
}
return;
}
You can also target in your conditions the payment gateways for example here we bypass 3 payment gateways and target a specific payment gateway using "your_payment_gateway" slug:
add_action( 'woocommerce_thankyou', 'custom_woocommerce_paid_order_status', 10, 1 );
function custom_woocommerce_paid_order_status( $order_id ) {
if ( ! $order_id ) {
return;
}
global $woocommerce;
$order = new WC_Order( $order_id );
// Bypass orders with Bank wire, Cash on delivery and Cheque payment methods.
if ( ( get_post_meta($order->id, '_payment_method', true) == 'bacs' ) || ( get_post_meta($order->id, '_payment_method', true) == 'cod' ) || ( get_post_meta($order->id, '_payment_method', true) == 'cheque' ) ) {
return;
}
// Target your "your_payment_gateway_slug" with this conditional
if ( is_object($order) && get_post_meta($order->id, '_payment_method', true) == 'your_payment_gateway_slug' && $order->has_status( 'processing' ) ) {
$order->update_status( 'on-hold' );
}
return;
}
This code snippets goes on function.php file of your active child theme or theme.
You can easily do anything you want, and the correct hook for paid orders is woocommerce_thankyou
References:
WooCommerce Class WC_Abstract_Order
WooCommerce: Auto complete paid Orders (depending on Payment methods)
Renaming WooCommerce Order Status
yes there is a way, but you need to modify the payment plugin or add your own code, you can read this to understand how payments work.
Now, woocommerce use $order->payment_complete() method to handle the completed order, so you need to hook your own function to modify the status, here is the description of that method
Use this filter: woocommerce_payment_complete_order_status
Normally wooCommerce should autocomplete orders for virtual products. But it doesn't and this is a real problem, even a BUG like.
So at this point you can find somme helpful things(but not really convenient):
1) A snippet code (that you can find in wooCommerce docs):
/**
* 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' );
}
But this snippet does not work for BACS*, Pay on delivery and Cheque payment methods. It's ok for Paypal and Credit Card gateways payment methods.
*BACS is a Direct Bank transfer payment method
And …
2) A plugin: WooCommerce Autocomplete Orders
This plugin works for all payment methods, but not for other Credit Card gateways payment methods.
My question:
Using (as a base) the wooCommerce snippet in point 1:
How can I implement conditional code based on woocommerce payment methods?
I mean something like: if the payment methods aren't "BACS", "Pay on delivery" and "Cheque" then apply the snippet code (update status to "completed" for paid orders concerning virtual products).
Some help will be very nice.
The most accurate, effective and lightweight solution (For WooCommerce 3 and above) - 2019
This filter hook is located in:
WC_Order Class inside payment_complete() method which is used by all payment methods when a payment is required in checkout.
WC_Order_Data_Store_CPT Class inside update() method.
As you can see, by default the allowed paid order statuses are "processing" and "completed".
###Explanations:
Lightweight and effective:
As it is a filter hook, woocommerce_payment_complete_order_status is only triggered when an online payment is required (not for "cheque", "bacs" or "cod" payment methods). Here we just change the allowed paid order statuses.
So no need to add conditions for the payment gateways or anything else.
Accurate (avoid multiple notifications):
This is the only way to avoid sending 2 different customer notifications at the same time:
• One for "processing" orders status
• And one for "completed" orders status.
So customer is only notified once on "completed" order status.
Using the code below, will just change the paid order status (that is set by the payment gateway for paid orders) to "completed":
add_action( 'woocommerce_payment_complete_order_status', 'wc_auto_complete_paid_order', 10, 3 );
function wc_auto_complete_paid_order( $status, $order_id, $order ) {
return 'completed';
}
Code goes in function.php file of the active child theme (or active theme).
Related: WooCommerce: autocomplete paid orders based on shipping method
2018 - Improved version (For WooCommerce 3 and above)
Based on Woocommerce official hook, I found a solution to this problem *(Works with WC 3+).
In Woocommerce for all other payment gateways others than bacs (Bank Wire), cheque and cod (Cash on delivery), the paid order statuses are "processing" and "completed".
So I target "processing" order status for all payment gateways like Paypal or credit card payment, updating the order status to complete.
The code:
add_action( 'woocommerce_thankyou', 'wc_auto_complete_paid_order', 20, 1 );
function wc_auto_complete_paid_order( $order_id ) {
if ( ! $order_id )
return;
// Get an instance of the WC_Product object
$order = wc_get_order( $order_id );
// No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
if ( in_array( $order->get_payment_method(), array( 'bacs', 'cod', 'cheque', '' ) ) ) {
return;
}
// For paid Orders with all others payment methods (paid order status "processing")
elseif( $order->has_status('processing') ) {
$order->update_status( 'completed' );
}
}
Code goes in function.php file of the active child theme (or active theme).
Original answer (For all woocommerce versions):
The code:
/**
* AUTO COMPLETE PAID ORDERS IN WOOCOMMERCE
*/
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_paid_order', 10, 1 );
function custom_woocommerce_auto_complete_paid_order( $order_id ) {
if ( ! $order_id )
return;
$order = wc_get_order( $order_id );
// No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
if ( ( 'bacs' == get_post_meta($order_id, '_payment_method', true) ) || ( 'cod' == get_post_meta($order_id, '_payment_method', true) ) || ( 'cheque' == get_post_meta($order_id, '_payment_method', true) ) ) {
return;
}
// For paid Orders with all others payment methods (with paid status "processing")
elseif( $order->get_status() === 'processing' ) {
$order->update_status( 'completed' );
}
}
Code goes in function.php file of the active child theme (or active theme).
With the help of this post: How to check payment method on a WooCommerce order by id?
with this : get_post_meta( $order_id, '_payment_method', true ); from helgatheviking
"Bank wire" (bacs), "Cash on delivery" (cod) and "Cheque" (cheque) payment methods are ignored and keep their original order status.
Updated the code for compatibility with WC 3.0+ (2017-06-10)
For me this hook was called even if the payment did not go through or failed, and this resulted to completed failed payments. After some research I changed it to use 'woocommerce_payment_complete' because it's called only when payment is complete and it covers the issue that #LoicTheAztec mentions above –
add_action( 'woocommerce_payment_complete', 'wc_auto_complete_paid_order', 20, 1 );
function wc_auto_complete_paid_order( $order_id ) {
if ( ! $order_id )
return;
// Get an instance of the WC_Product object
$order = wc_get_order( $order_id );
// No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
if ( in_array( $order->get_payment_method(), array( 'bacs', 'cod', 'cheque', '' ) ) ) {
return;
// Updated status to "completed" for paid Orders with all others payment methods
} else {
$order->update_status( 'completed' );
}
}
For me, the simplest hook to modify order status when payment is completed is 'woocommerce_order_item_needs_processing' as this filter hook is meant to modify the target order status when payment completes.
This is what the final snippet will look alike:
add_filter('woocommerce_order_item_needs_processing', '__return_false',999);
It also compatible with the other plugins on sites.
For me, on a testing system with PayPal Sandbox (WooCommerce PayPal Payments plugin) the LoicTheAztec solution (2019 update) worked only when I added the $order->update_status( 'completed' ); code line. The return 'completed'; has not an effect in my case, I left it just because it's a filter.
add_filter( 'woocommerce_payment_complete_order_status', function( $status, $order_id, $order ) {
$order->update_status( 'completed' );
return 'completed';
}, 10, 3 );
If you are looking for autocompletion of virtual orders (like courses, ebooks, downloadables etc), this might be useful.
* Auto Complete all WooCommerce virtual orders.
*
* #param int $order_id The order ID to check
* #return void
*/
function custom_woocommerce_auto_complete_virtual_orders( $order_id ) {
// if there is no order id, exit
if ( ! $order_id ) {
return;
}
// No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
if ( in_array( $order->get_payment_method(), array( 'bacs', 'cod', 'cheque', '' ) ) ) {
return;
}
// get the order and its exit
$order = wc_get_order( $order_id );
$items = $order->get_items();
// if there are no items, exit
if ( 0 >= count( $items ) ) {
return;
}
// go through each item
foreach ( $items as $item ) {
// if it is a variation
if ( '0' != $item['variation_id'] ) {
// make a product based upon variation
$product = new WC_Product( $item['variation_id'] );
} else {
// else make a product off of the product id
$product = new WC_Product( $item['product_id'] );
}
// if the product isn't virtual, exit
if ( ! $product->is_virtual() ) {
return;
}
}
/*
* If we made it this far, then all of our items are virual
* We set the order to completed.
*/
$order->update_status( 'completed' );
}
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_virtual_orders' );
Adapted from https://gist.github.com/jessepearson/33f383bb3ea33069822817cfb1da4258