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.
Related
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 );
This question already has answers here:
Disable specific payment methods depending on Woocommerce order status
(2 answers)
Closed 2 years ago.
help i get this error
I am doing the same function Disable specific payment methods depending on Woocommerce order status I get
Copy and paste the same code and it didn't work
syntax error, unexpected 'elseif' (T_ELSEIF)
add_filter( 'woocommerce_available_payment_gateways', 'conditionally_hide_payment_gateways', 100, 1 );
function conditionally_hide_payment_gateways( $available_gateways ) {
// 1. On Order Pay page
if( is_wc_endpoint_url( 'order-pay' ) ) {
// Get an instance of the WC_Order Object
$order = wc_get_order( get_query_var('order-pay') );
// Loop through payment gateways 'pending', 'on-hold', 'processing'
foreach( $available_gateways as $gateways_id => $gateways ){
// Keep paypal only for "pending" order status
elseif ($gateways_id !== 'paypal' && $order->has_status('pending') ) {
unset($available_gateways[$gateways_id]);
}
}
}
// 2. On Checkout page
elseif( is_checkout() && ! is_wc_endpoint_url() ) {
// Disable paypal
if( isset($available_gateways['paypal']) ) {
unset($available_gateways['paypal']);
}
}
return $available_gateways;
}
enter image description here
The elseif inside the foreach doesn't have a preceding if, so that's your error.
add_filter( 'woocommerce_available_payment_gateways', 'conditionally_hide_payment_gateways', 100, 1 );
function conditionally_hide_payment_gateways( $available_gateways ) {
// 1. On Order Pay page
if( is_wc_endpoint_url( 'order-pay' ) ) {
// Get an instance of the WC_Order Object
$order = wc_get_order( get_query_var('order-pay') );
// Loop through payment gateways 'pending', 'on-hold', 'processing'
foreach( $available_gateways as $gateways_id => $gateways ){
// Keep paypal only for "pending" order status
if ($gateways_id !== 'paypal' && $order->has_status('pending') ) {
unset($available_gateways[$gateways_id]);
}
}
}
// 2. On Checkout page
elseif( is_checkout() && ! is_wc_endpoint_url() ) {
// Disable paypal
if( isset($available_gateways['paypal']) ) {
unset($available_gateways['paypal']);
}
}
return $available_gateways;
}
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
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!!!!!!
I'm using woocommerce bookings.
I'm trying to trigger woocommerce order status to refund if the woocommerce_booking status is cancelled. I tried this code but it's not working.
global $woocommerce;
$order = new WC_Order( $order_id );
if ( 'cancelled' == $order->status ) {
$order->update_status('refund', 'order_note');
}
To update order status on cancel status
add_action('woocommerce_cancelled_order','change_status_to_refund', 10, 1);
function change_status_to_refund($order_id) {
$order = new WC_Order( $order_id );
$order->update_status('refund', 'order_note');
exit;
}
I hope it will help you. Thanks :)
add_action( 'woocommerce_order_status_changed', 'wc_order_status_changed', 99, 3 );
function wc_order_status_changed( $order_id, $old_status, $new_status ){
if( $new_status == "cancelled" || $new_status == "refunded" ) {
//code here.
}
}
If you want use in some class action must be like this:
add_action( 'woocommerce_order_status_changed', array($this, 'wc_order_status_changed'), 99, 3 );
You need to fetch the status of your order and then check your required condition and update it accordingly.
$order_status = $order->get_status();
I know this is an old post, but i have just make it on my latest wordpress/woocommerce install
add_action('woocommerce_booking_cancelled', 'my_booking_cancelled_handler', 10, 1);
function my_booking_cancelled_handler ( $booking_id ) {
$booking = new WC_Booking( $booking_id );
$order_id = $booking->get_order_id();
// check order for your business logic
// refund or not ;-) it's up to you
}
I hope this helps someone.
//Try this
add_action( 'woocommerce_order_status_changed', 'auto_destroy_failed_orders', 10, 4 );
function auto_destroy_failed_orders( $order_id, $old_status, $new_status, $order )
{
if ( ( $new_status == 'completed' )) {
// Order status complete
$order->update_status( 'custom-status' );
}else if ( ( $new_status == 'failed' )) {
$order->update_status( 'custom-status' );
}else if ( ( $new_status == 'b2c-shipment' )) {
// custome status
$order->update_status( 'custom-status' );
}else if ( ( $new_status == 'on-hold' )) {
// Order status to on-hold
$order->update_status( 'custom-status' );
}else if ( ( $new_status == 'refunded' )) {
// Order status to refunded
$order->update_status( 'custom-status' );
}else if ( ( $new_status == 'failed' )) {
// Order status to failed
$order->update_status( 'custom-status' );
}else if ( ( $new_status == 'processing' ) ) {
// Order status to processing
$order->update_status( 'custom-status' );
}
}
Hey you can try this hook!!
https://therichpost.com/change-product-order-status-woocommerce-hook
Hope this will help you