Auto change order status from processing to completed in Woocommerce - wordpress

I want to change every order from woocommerce if the 'PROCESSING' status will automatically be updated to 'COMPLETED'.
I have tried writing the function in functions.php file but I did not succeed.
How can I automatically change the order status from "processing" to "completed" in Woocommerce when I have received payment from the user?
I use this code but it has no effect
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' );
}
}
Thank's

To auto completed orders, you should try the following:
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' );
}
}
Code goes in function.php file of your active child theme (or theme).I have tested the code and its working for me please check screenshot https://prnt.sc/m3zrwp

Use woocommerce_order_status_processing in trigger! woocommerce_thankyou is called only in direct checkout proccess

Related

Change WooCommerce Status Based On Payment Method Interac (etransfer)

I want when a customer chooses to pay through Email Transfer (Interac) to go ahead and set the order status to Pending etransfer.
– My custom gateway name is custom_b9599316cc4fd28.
– My custom status slug is pending-etransfer.
add_action( 'woocommerce_order_status_changed', 'woocommerce_payment_complete_order_status', 10, 3 );
function woocommerce_payment_complete_order_status( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
if ( $order->data['status'] == 'processing' ) {
$payment_method = $order->get_payment_method();
if ( $payment_method != 'custom_b9599316cc4fd28' ) {
$order->update_status( 'pending-etransfer' );
}
}
}
Any help is greatly appreciated.
The code as you see it.

No result when using the "woocommerce_new_order" hook when order has status "processing"

I need Telegram notifications to be sent when the order status is "Processing", so I have this code:
add_action( 'woocommerce_new_order', 'new_order_send_tg', 1, 1 );
function new_order_send_tg( $order_id ) {
$order = wc_get_order( $order_id );
$msg .= '✅ Order № '.$order_id . ' *success*';
$userId = '-xxxxxxxxxxxxxx'; // ID Chat
$token = 'xxxxxxxxxxxxxxxxxxxxxxxx'; // Token bot
if( 'processing' == $order->get_status() ) {
file_get_contents('https://api.telegram.org/bot'. $token .'/sendMessage?chat_id='. $userId .'&text=' . urlencode($msg) . '&parse_mode=markdown'); // Send
} else {
//
}
}
Without the if( 'processing' == $order->get_status() ) condition, everything works as expected. But with the if condition added this doesn't seem to be the case. Any advice?
It looks like the woocommerce_new_order hook is executed before the order status is changed to processing
function action_woocommerce_new_order( $order_id, $order = null ) {
// Get order
$order = is_a( $order, 'WC_Order' ) ? $order : wc_get_order( $order_id );
$status = $order->get_status();
// Result: status = pending
}
add_action( 'woocommerce_new_order', 'action_woocommerce_new_order', 10, 2 );
So you can use the woocommerce_order_status_' . $status_transition['to'] composite action hook instead, where you will replace $status_transition[to] by processing
function action_woocommerce_order_status_processing( $order_id, $order ) {
// Do your thing
}
add_action( 'woocommerce_order_status_processing', 'action_woocommerce_order_status_processing', 10, 2 );

Woocommerce action `woocommerce_order_status_completed` not executing

We have a problem as I am not a woocommerce developer. The client wants to make a product out of stock once the order is completed (and payment received).
I added an action in functions.php
add_action( 'woocommerce_order_status_completed', 'set_product_out_of_stock', 10, 1 );
function set_product_out_of_stock( $order_id ) {
error_log( "This is a test: set product out of stock $order_id ^^", 0 );
//if ( 'completed' == $order_status) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$status = 'outofstock';
update_post_meta($item['item_id'], '_stock', 0);
update_post_meta( $item['item_id'], '_stock_status', wc_clean( $status ) );
wp_set_post_terms($item['item_id'], 'outofstock', 'product_visibility', true );
}
// }
}
The problem is that this action is not even triggered, The error_log doesn't show in the logs.. what may be the issue?
The website uses woocommerce v 4.7 .
Thank you !

How to send "Customer Invoice" emails with Pay Later (pending status) gateway

I have created a new payment gateway plugin that allows customers to pay later. I will be using it with a woocommerce POS system.
I cannot get the 'customer invoice' email to send automatically - I believe it has something to do with the "pending" status of the order (which it must have in order to allow the customer to pay later). I have analysed many other answers but none have worked for me.
I have gently modified the code from this link (only - changed names and set up as plugin): https://github.com/creativelittledots/woocommerce-pay-later.
It uses a series of functions to trigger the email that I don't fully understand - but it doesn't seem to work: The default status is set to "on- hold" to allow the emails to trigger then the order is set to "pending" to allow the customer to pay for the order.
Here are the relevant snippets:
add_filter( 'woocommerce_default_order_status', array($this, 'default_order_status') );
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
add_filter( 'woocommerce_email_format_string_find', array($this, 'order_status_format_string_find') );
add_filter( 'woocommerce_email_format_string_replace', array($this, 'order_status_format_string_replace'), 10, 2 );
add_action( 'woocommerce_order_status_pending', array($this, 'send_pending_order_emails') );
add_filter( 'woocommerce_valid_order_statuses_for_payment', array($this, 'valid_order_statuses_for_payment' ), 10, 2 );
add_action( 'wp', array($this, 'change_order_to_pending_on_order_received'), 8 );`
public function valid_order_statuses_for_payment($statuses, $order) {
if( $order->is_pay_later ) {
$statuses[] = 'on-hold';
}
return $statuses;
}
/**
* Change the default order status to on-hold so that pending order emails can be triggered
*/
public function default_order_status($default) {
if( ! is_admin() && WC()->session->set( 'chosen_payment_method') == $this->id ) {
$default = 'on-hold';
}
return $default;
}
/**
* Allow Order status to be accessible from emails
*/
public function order_status_format_string_find( $find ) {
$find['order-status'] = '{order_status}';
return $find;
}
/**
* Replace Order status in emails
*/
public function order_status_format_string_replace( $replace, $email ) {
if( $email->object ) {
$replace['order-status'] = wc_get_order_status_name( $email->object->get_status() );
}
return $replace;
}
/**
* Trigger pending order emails and invoice email
*/
public function send_pending_order_emails( $order_id ) {
$emails = new WC_Emails();
$order = wc_get_order( $order_id );
$emails->customer_invoice( $order_id );
$emails->emails['WC_Email_New_Order']->trigger( $order_id );
$order->set_payment_method( $this );
}
/**
* WC Shop As Customer support on Order Received, because the default status is on hold we need to change these orders to pending
*/
public function change_order_to_pending_on_order_received() {
if( class_exists('WC_Shop_As_Customer') && ! empty( $_GET['order_on_behalf'] ) && ! empty( $_GET['key'] ) && ! empty( $_GET['send_invoice'] ) ) {
global $wp;
if ( ! isset( $wp->query_vars['order-received'] ) )
return;
// Bail if we're not shopping-as - don't display the special interface.
if ( ! WC_Shop_As_Customer::get_original_user() )
return;
$order_id = $wp->query_vars['order-received'];
if ( ! empty( $order_id ) ) {
$order = new WC_Order( absint( $order_id) );
$order->update_status( 'pending' );
}
unset( $_GET['send_invoice'] );
}
}
I also read that woocommerce_order_status_pending may not trigger emails so I have tried adding this code (no successes):
add_filter( 'woocommerce_email_actions', 'filter_woocommerce_email_actions' );
function filter_woocommerce_email_actions( $actions ){
$actions[] = 'woocommerce_order_status_pending';
return $actions;
}
I suspect either:
The code to put the order 'on hold' then trigger then back to 'pending' is not set up correctly
woocommerce_order_status_pending hook does not trigger emails
I have a little work around to trigger from the "woocommerce_thankyou" hook in functions.php but I believe that this should be done in the gateway (plus I don't want to rely on woocommerce_thankyou - this email should be based on order creation).
Thanks in advance!!!!!!

Fill out custom field with used coupon code WooCommerce

Right now I'm working with a mailchimp plugin that needs a custom field for validating/segmenting.
For this segment I want to check what kind of coupon is used. So I scavenged the following code that should fill my custom field with the used coupons:
add_action( 'woocommerce_checkout_update_order_meta',
'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( empty( $_POST['my_field_name'] ) ) {
$coupons = $order->get_used_coupons();
update_post_meta( $order_id, 'my_field_name', $coupons);
}
}
Sadly this will only crash my site.
Any help would be greatly appreciated.
There are several problems with your code:
You're not validating if the field has any information, so you need
to check if $_POST has the my_field_name key
You need to load the $order variable in order to get the used coupons.
What happens when there's a my_field_value? Do you store it?
Here's the modified code:
add_action( 'woocommerce_checkout_update_order_meta',
'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id, $posted ) {
if ( isset($_POST['my_field_name']) && empty( $_POST['my_field_name'])) {
$order = new WC_Order( $order_id );
$coupons = $order->get_used_coupons();
update_post_meta( $order_id, 'my_field_name', $coupons);
}
}

Resources