WooCommerce Orders with Custom status change status Cancelled after failed payment - wordpress

I use a custom status: Payment Due. Once an order gets the status Payment Due it will automatically trigger a email after 14 days reminding the customer to pay for their order.
Now when this customer tries to pay but it fails for whatever reason the order will get the status Cancelled.
But I need this order to get back to status Payment Due or something similar like Payment Failed.
So for particular statuses I would really like to remove the status Cancelled and Failed from applying when a customer fails to complete the payment and apply a other custom status in stead

The following hook did the trick:
add_action( 'woocommerce_order_status_reminder_to_failed',
'change_status_to_payment_required' , 10, 10 );
function change_status_to_payment_required( $order_id ) {
$order = wc_get_order($order_id);
$order->update_status( 'payment-required' );
}

Related

Auto change order status from pending to completed in Woocommerce

I am trying to change paid order status pending to completed.
CODE:
function wc_autocomplete_paid_orders( $order_status, $order_id ) {
$order = wc_get_order( $order_id );
if ( $order_status == 'pending' ) {
return 'completed';
}
return $order_status;
} ```
THank you for the help.
You should not do this programatically as this is done from the payment providers that your WooCommerce is configured to use whet a successful order payment has been issued.
If in case you use a payment provider in sandbox mode or test mode that does not do this and you need to test the completed order status, you can do change this manually from the WordPress admin panel:
WordPress Admin / WooCommerce / Orders - edit a PENDING oder and change it manually to COMPLETE.

WooCommerce send order complete email automatically

I'm faced with a little problem.
I need to send the Customer Invoice / Order Details email automatically when an order is completed.
I haven't found any action or filter to apply in this case an the internet is not useful. Has anyone an idea how to send this email when the order status switches?
Best regards!
You can use woocommerce_order_status_completed hook and WC_Email_Customer_Invoice
add_action( 'woocommerce_order_status_completed', 'mysite_woocommerce_order_status_completed', 10, 1);
function mysite_woocommerce_order_status_completed( $order_id ) {
// Send invoice email to customer
//use order ID as trigger value
WC()->mailer()->emails['WC_Email_Customer_Invoice']->trigger($order_id);
}

WooCommerce hook on order status change fires multiple times instead for only the changed order

I've created a hook for WooCommerce when an order is cancelled like so:
add_action( 'woocommerce_order_status_cancelled', 'prefix_order_cancelled_hook' );
function prefix_order_cancelled_hook($order_id){
write_log("Order ${order_id} has been cancelled")
}
Now when the status changed to cancelled, the hook is invoked as expected but the output execution is not what I'm expecting. I'm getting the following in the log:
Order 4 has been cancelled
Order 5 has been cancelled
Order 6 has been cancelled
I've seen that this corresponds to the number of orders I currently have on the store. Can someone help with why this is happening and how to only run the hook once for the changed order.
Seems like your code is executed for all orders. So there seems to be a problem with the $order_id. I think you can't access properties directly, but need to get an instance of the WC_Order object (since Woocommerce 3.0+ something about) like:
add_action( 'woocommerce_order_status_cancelled', 'prefix_order_cancelled_hook' );
function prefix_order_cancelled_hook($order_id){
// get an instance of an WC_Order object
$order = wc_get_order( $order_id );
// get the ID of the order
$order_id = $order->get_id();
write_log("Order ${order_id} has been cancelled")
}

How do you get current renewal order id for woocommerce subscriptions?

I'm using woocommerce subscriptions with the custom function like below and can only get the parent order id from $subscription (from the order that initiated the subscription rather than getting each new order ID as renewals get triggered). Does anyone know how to get each new recurring order id?
add_action( 'woocommerce_scheduled_subscription_payment', 'record_parent_referral_on_payment' );
function record_parent_referral_on_payment( $subscription ) {
....

How to update user meta field after a action is completed

I tried to update user meta field after the checkout payment is successful. I tried keeping the following code in thankyou.php
update_user_meta($user->ID, '_uw_balance', $updated_balance);
Actually i'm using a wallet plugin called "User Wallet Credit System" and using the user wallet balance to get deducted in the total order amount. I'm able to deduct the wallet amount in order total but unable to update the wallet after the successful payment.
I'm using woocommerce for the checkout and everything. I need to how should i update the wallet amount after the transaction is processed.
Use woocommerce_thankyou being called after successful checkout.
you can play with order id,
add_action('woocommerce_thankyou', 'update_e_wallet', 11, 1);
function update_e_wallet($order_id) {
//do something...
}
also you can get user by order id
$order = new WC_Order( $order_id );
$user_id = $order->user_id;

Resources