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
Related
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 need to create a function that looks for all the new orders entered in woocommerce and then just update this order without changing anything. (like if you clicked on the order and then clicked update with everything remaining the same)
The reason for this is that I'm bringing orders from another source into woocommerce but the plugin to export them to another platform isn't listening for these external orders until they are updated even if nothing changes.
I know I have to use the woocommerce_new_order hook to listen for the new orders coming in but how can I just update it without changing anything?
function custom_woocommerce_order($order_id) {
if (!$order_id) {
return;
}
//Will this work for what I need?
$order_id = $order->save();
//Or should I do this instead?
$order = wc_get_order($order_id);
do_action( 'woocommerce_update_order', $order );
}
add_action('woocommerce_new_order', 'custom_woocommerce_order');
I want to add my own custom payment error text (we use Account Funds and want to add an extra prompt)
How can I change, the following which shows when there is not enough funds, from:
Sorry, it seems that there are no available payment methods for your
state. Please contact us if you require assistance or wish to make
alternate arrangements.
to an error message with a link like below:
You do not have sufficent funds to process this order, please top up or upgrade. Thank you.
The text seems to be stored in templates/checkout/payment.php
https://github.com/woocommerce/woocommerce/blob/ef05bfccfc01bb2c621ef1293e61f7c57950670f/templates/checkout/payment.php
How can I change this without it getting wiped out by a Woocommerce version upgrade?
In WordPress, filters are functions that can be hooked to an event (called hooks). During the execution when the event is triggered the filter is applied to the data output generated by the event hook. It is important to remember that filters perform their actions on data they receive and then return that data before it is displayed in the browser.
In the file you attached (payment.php) you have
apply_filters( 'woocommerce_no_available_payment_methods_message' ....
So you can use the filter "woocommerce_no_available_payment_methods_message" to change the text
Create a custom function and add it to functions file or a small plugin.
First we hook our own function with woocommerce event
add_filter( 'woocommerce_no_available_payment_methods_message', 'your_custom_function_name_here' );
Now we define what our function would do.
function your_custom_function_name_here( $content ) {
//your changes here
$content = "bla bla";
// Returns the content.
return $content;
}
You just have to add a filter and apply your change in your child-theme's functions.php file:
add_filter( 'woocommerce_no_available_payment_methods_message', function( $no_gateways_message ) {
return 'You do not have sufficent funds to process this order, please top up or upgrade. Thank you.';
});
Let me know if that worked.
add_filter( 'woocommerce_no_available_payment_methods_message', 'change_payment_message', 10, 2);
function change_payment_message( $value, $arg2 ) {
$message = WC()->customer->get_billing_country()?'You do not have sufficent funds to process this order, pleasetop up or upgrade. Thank you':'Please fill in your details above to see available payment methods.';
return $message;
}
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 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' );