I need to do an action when an order is completed.
I Have tried this:
function mysite_woocommerce_payment_complete( $order_id ) {
error_log("callback fired");
}
add_action( 'woocommerce_payment_complete', 'mysite_woocommerce_payment_complete' );
But when I use the checkmark to mark the order as completed here in admin orders screen,
...the hook doesn't fire.
I tried woocommerce_order_status_changed too, it does the action when I place the order, but does nothing when i mark the order completed.
But when I mark the order completed, I get the email about completing it.
Thanks!
Edit:
I tried woocommerce_order_status_changed too, this way:
function mysite_woocommerce_payment_complete($order_id, $old_status, $new_status ) {
error_log("$old_status / $new_status \n");
}
add_action( 'woocommerce_order_status_changed', 'mysite_woocommerce_payment_complete', 99, 3 );
but it fires on purchasing (I selected bank transfer) and shows: "pending / on-hold", but not true - see edi2 doesn't fire on manual backend change from "on hold" to "completed". Neither by checkmark nor in single order interface.
Edit2
woocommerce_order_status_changed and woocommerce_order_status_completed works, it only outputed my testing "error" into debug.log, not to error_log for some reason.
The woocommerce_payment_complete i used previously doesn't apply to methods like bank transfer, that's why that didn't work. Thanks to #helgatheviking for the quick and right answer
Well the completed order email is triggered by the following:
// Triggers for this email
add_action( 'woocommerce_order_status_completed_notification', array( $this, 'trigger' ) );
as seen here in source.
All "transactional email actions" (ie: actions that trigger the sending of an email) get a _notification hook in addition to the normal hook, seen here.
Thus woocommerce_order_status_completed_notification is an additional hook triggered on the woocommerce_order_status_completed hook if woocommerce_order_status_completed is in the woocommerce_email_actions array, which it is by default. To avoid any surprised from the emails, I recommend using the woocommerce_order_status_completed hook which is fired any time there is an order status change, including from within the admin, see this example:
function mysite_woocommerce_payment_complete( $order_id ) {
error_log("callback fired");
}
add_action( 'woocommerce_order_status_completed', 'mysite_woocommerce_payment_complete' );
Related
I have a plugin I developed that connects a WooCommerce order to HubSpot. The issue i'm running into though is that while it works, the hook im using right now is sending order info to HubSpot before its technically complete. So this means that stuff like Failed orders get sent in as Pending, and coupon codes are ommitted.
So i'm wondering what the right hook to use is.
My goal: Everytime a WooCommerce order is created & completed and a WooCommerce order is updated, send the data to HubSpot.
What I have so far:
add_action('save_post_shop_order', 'printout', 10, 3);
function printout($post_ID, $post, $update)
{
if (!is_admin()){
return;
}
if($update){
$msg = $post_ID;
$order = get_woocommerce_order($msg);
mainplugin($msg, $order);
}
}
add_action('woocommerce_new_order', 'neworder_delegator', 10, 2);
function neworder_delegator($order_id, $order){
mainplugin($order_id, $order);
}
So I guess i'm just looking for the right hook to get me what I want.
Thanks!
Here is your answer:
Each WooCommerce Order transition has one or more dynamic hooks that trigger when the status transition happens.
They start with 'woocommerce_order_status_' and the remaining part of the action is either the new status that the order has transitioned to, or the to and from statuses involved in the format '<status_transition_from>to<status_transition_to>'
Examples
You can hook your function to
add_action( 'woocommerce_order_status_completed', 'your_order_completed_function');
To trigger your function only when the order transitioned to completed, and not when refunded, cancelled, on-hold, etc. as those would run on other actions such as
woocommerce_order_status_refunded
woocommerce_order_status_cancelled
woocommerce_order_status_on-hold
woocommerce_order_status_failed
woocommerce_order_status_processing
Edited to add link to official WooCommerce docs:
https://woocommerce.github.io/code-reference/hooks/hooks.html
I need help with a problem-related to plugin "WooCommerce Pay for Payment" which counting some extra fee in shipping. Problem is, that this plugin sets automatically "processing" status in order which causes thanking email for payment (in case of local payment) and don't send email notification about a new order, so customer is confused (I didn't send any money and I received email "thanks for your payment").
I tried this solution: Set WooCommerce order status when order is created from processing to pending
But it only changes order status back to "on-hold" but sends email thank for payment anyway.
Only one thing that I need is to send to the customer in every new order email about a new order, nothing more (I would like to change status to "processing" manually).
Thank you for help, I have no idea how to resolve because I couldn't find PHP file causing a change of status in the plugin.
EDIT: Sorry to all. This was problem of COD in woocommerce plugin. Not Pay for payment as I mentioned. Woocommerce COD automatically set "processing" status.
I found solution for this on github:here
Its the first code.
Based on the answer to this question, this code worked fine for me:
function sv_wc_cod_order_status( $status ) {
return 'on-hold';
}
add_filter( 'woocommerce_cod_process_payment_order_status', 'sv_wc_cod_order_status', 15 );
Updated: The code that you found in Github is outdated, clumsy and complicated, since there is a dedicated filter hook now. You should better try this lightweight and effective code, that will set the default order status for "Cash on delivery" payment gateway (COD) to "On Hold":
add_filter( 'woocommerce_cod_process_payment_order_status', 'change_cod_payment_order_status', 10, 2 );
function change_cod_payment_order_status( $order_status, $order ) {
return 'on-hold';
}
Code goes in functions.php file of your active child theme (active theme). Tested and works.
So the default order status set by the payment gateway is now "On Hold" instead of "Processing"
In my case,
add_filter( 'woocommerce_cod_process_payment_order_status','change_cod_payment_order_status', 10, 2 );
function change_cod_payment_order_status( $order_status, $order ) {
return 'on-hold';
}
Worked great in WC 4.42 + WP 5.4.1
Thx!
two solution above are same except:
the solution by #LoicTheAztek has 2 arguments in the core function and have a '10' hook priority
the solution by #Jiří-Prek has an arguments in the core function and have a '15' hook priority
but for my WP5.1.1 and WC3.5.7
function change_cod_payment_order_status( $order_status, $order ) {
return 'on-hold';
}
generating an error
PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to
function change_cod_payment_order_status()
so I prefer use the code with only one argument in a main function
I use WooCommerce to manually take orders via the telephone / email etc. I use the backend to record the orders manually adding the orders.
At present, when generating an order as 'pending payment' as the status the stock is automatically reduced / deducted. I do not want this to happen. Ideally I only want stock to be reduced when the order is marked as in 'processing' as then payment would of then been taken.
I understand this is how WooCommerce works back is there a way to avoid stock being reduced until a certain status has been selected?
I have tried the below code within functions.php and used the 'on-hold' status to test but the stock is still reduced.
add_filter( 'woocommerce_can_reduce_order_stock', 'wcs_do_not_reduce_onhold_stock', 10, 2 );
function wcs_do_not_reduce_onhold_stock( $reduce_stock, $order ) {
if ( $order->has_status( 'on-hold' )) {
$reduce_stock = false;
}
return $reduce_stock;
}
I wonder if it because I am adding the order as an admin? I know this is how WooCommerce works but ideally I need a method of overriding the 'pending payment' status when creating an order both within the draft and creation stage until marked as 'processing'.
Any help would be fantastic.
Try this adding to functions.php
Note: not tested.
function reduce_stock_processing($order_id) {
wc_reduce_stock_levels($order_id);
}
add_action('woocommerce_order_status_processing', 'reduce_stock_processing');
I need help with a problem-related to plugin "WooCommerce Pay for Payment" which counting some extra fee in shipping. Problem is, that this plugin sets automatically "processing" status in order which causes thanking email for payment (in case of local payment) and don't send email notification about a new order, so customer is confused (I didn't send any money and I received email "thanks for your payment").
I tried this solution: Set WooCommerce order status when order is created from processing to pending
But it only changes order status back to "on-hold" but sends email thank for payment anyway.
Only one thing that I need is to send to the customer in every new order email about a new order, nothing more (I would like to change status to "processing" manually).
Thank you for help, I have no idea how to resolve because I couldn't find PHP file causing a change of status in the plugin.
EDIT: Sorry to all. This was problem of COD in woocommerce plugin. Not Pay for payment as I mentioned. Woocommerce COD automatically set "processing" status.
I found solution for this on github:here
Its the first code.
Based on the answer to this question, this code worked fine for me:
function sv_wc_cod_order_status( $status ) {
return 'on-hold';
}
add_filter( 'woocommerce_cod_process_payment_order_status', 'sv_wc_cod_order_status', 15 );
Updated: The code that you found in Github is outdated, clumsy and complicated, since there is a dedicated filter hook now. You should better try this lightweight and effective code, that will set the default order status for "Cash on delivery" payment gateway (COD) to "On Hold":
add_filter( 'woocommerce_cod_process_payment_order_status', 'change_cod_payment_order_status', 10, 2 );
function change_cod_payment_order_status( $order_status, $order ) {
return 'on-hold';
}
Code goes in functions.php file of your active child theme (active theme). Tested and works.
So the default order status set by the payment gateway is now "On Hold" instead of "Processing"
In my case,
add_filter( 'woocommerce_cod_process_payment_order_status','change_cod_payment_order_status', 10, 2 );
function change_cod_payment_order_status( $order_status, $order ) {
return 'on-hold';
}
Worked great in WC 4.42 + WP 5.4.1
Thx!
two solution above are same except:
the solution by #LoicTheAztek has 2 arguments in the core function and have a '10' hook priority
the solution by #Jiří-Prek has an arguments in the core function and have a '15' hook priority
but for my WP5.1.1 and WC3.5.7
function change_cod_payment_order_status( $order_status, $order ) {
return 'on-hold';
}
generating an error
PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to
function change_cod_payment_order_status()
so I prefer use the code with only one argument in a main function
In previous versions of Woocommerce, an email notification was sent automatically when an order was changed from pending status to cancelled status (In my case, this happens after an allotted time set in the inventory section of the admin).
In WooCommerce 3.0.8 they have removed this automation and labelled as a fix:
https://github.com/woocommerce/woocommerce/blob/master/CHANGELOG.txt
And the pull request is here:
https://github.com/woocommerce/woocommerce/pull/15170/files
I'm looking to restore this functionality, but obviously copy/pasting this line back in to the Woocommerce core files isn't a good idea as it will get overwritten when the platform updates.
I know the best method would to be to create a function and hook into the cancelled order action via functions.php but after having a look I'm a bit lost about how to do this. Here is the line which was replaced:
add_action( 'woocommerce_order_status_pending_to_cancelled_notification', array( $this, 'trigger' ), 10, 2 );
How can I restore this old automated functionality?
The good new: Using woocommerce_order_status_pending_to_cancelled action hook with a custom function hook in it, solve your problem definitively:
add_action('woocommerce_order_status_pending_to_cancelled', 'cancelled_send_an_email_notification', 10, 2 );
function cancelled_send_an_email_notification( $order_id, $order ){
// Getting all WC_emails objects
$email_notifications = WC()->mailer()->get_emails();
// Sending the email
$email_notifications['WC_Email_Cancelled_Order']->trigger( $order_id );
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and perfectly works in WooCommerce 3+ (still work on version 4.8+)