Answer the question
In order to leave comments, you need to log in
How to automatically change order status in Woocommerce?
There is a site for selling content by subscription using recurrent payments + Woocommerce plugin version 4.1.1.
Problem: After paying for a subscription, the status of the Woocommerce order goes to the status "In progress", and you have to manually change the status of the order to "Completed" to activate the subscription and provide the client with access to the content.
It is required that the status of the order (after payment) automatically change to "Completed", bypassing the status "In processing".
After researching on the Internet, I found several ready-made functions, but, unfortunately, they do not work.
Example:
function wc_mark_all_orders_as_complete($order_status, $order_id) {
$order = new WC_Order($order_id);
if ($order_status == 'processing' && ( $order->status == 'on-hold' || $order->status == 'pending' || $order->status == 'failed' )) {
return 'completed';
}
return $order_status;
}
add_filter('woocommerce_payment_complete_order_status', 'wc_mark_all_orders_as_complete', 10, 2);
Answer the question
In order to leave comments, you need to log in
UPD:
Solved this problem by adding this function to functions.php
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 );
if( $order->has_status( 'processing' ) ) {
$order->update_status( 'completed' );
}
}
If the goods are specified as virtual, then for such types of goods there is an addition to WooCommerce, Orders Autocomplete.
https://wordpress.org/plugins/autocomplete-woocomm...
I myself recently did a project and needed this function so that after payment the product was transferred to the Completed state
Working solution found in internet point. I publish for myself, so as not to lose, but maybe someone will come in handy.
add_filter( 'woocommerce_thankyou', 'update_order_status', 10, 1 );
function update_order_status( $order_id ) {
if ( !$order_id ){
return;
}
$order = new WC_Order( $order_id );
if ( 'on-hold' == $order->status) {
$order->update_status( 'pending payment' );
}
return;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question