I implemented this code by #LoicTheAztec to set the default status for all COD orders to pending:
add_action( 'woocommerce_thankyou', 'woocommerce_thankyou_change_order_status', 10, 1 );
function woocommerce_thankyou_change_order_status( $order_id ){
if( ! $order_id ) return;
$order = wc_get_order( $order_id );
if( $order->get_status() == 'processing' ) $order->update_status( 'pending' );
}
After adding the code (as a plugin), Woocommerce started sending duplicate email notifications of new orders. Essentially, one notification is sent when the order is created (rightly so), but the same exact notification is again then sent when the order is marked as completed.
How can I stop this?
You are generating another notification since you are updating the order status after it has already been set by the default functionality in woocommerce. Can you try the below code:
add_filter( 'woocommerce_cod_process_payment_order_status', 'default_cod_payment_order_status', 10 );
function default_cod_payment_order_status( $order_status ) {
return 'pending';
}
Related
I'm trying to figure out the best way to automatically update the order status in WooCommerce when the tracking information is inputted via the official WooCommerce Shipment Tracking plugin. I found the doc from Woo on how to automatically complete orders, but I only want this to execute when the tracking number is added. Any help would be greatly appreciated!
Below is the code from Woo:
/**
* Auto Complete all WooCommerce orders.
*/
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 );
$order->update_status( 'completed' );
}
And here are the meta references from the plugin:
The Shipping Tracking plugin stores the tracking information in the order meta with the meta key _wc_shipment_tracking_items. It’s an array with the following structure:
tracking_provider — String of predefined provider
custom_tracking_provider — String of custom provider
custom_tracking_link — String of custom tracking URL tracking_number
String of tracking number date_shipped — Timestamp of shipment date
As you said you are using the "WooCommerce Shipment Tracking plugin." but in that plugin, I didn't find any filters or hooks that will help to update status when the tracking number is added. but I found that they use update_post_meta() to update the tracking code so you can use the update_postmeta action hook to update order status.
Try the below code. code will go in your active theme functions.php file.
function update_order_status_when_shipment_tracking_input( $meta_id, $object_id, $meta_key, $meta_value ){
if( $meta_key == '_wc_shipment_tracking_items' ){
error_log('update_order_status_when_shipment_tracking_input');
$order = wc_get_order( $object_id );
if( $order ){
$order->update_status( 'completed' );
}
}
}
add_action( 'update_postmeta', 'update_order_status_when_shipment_tracking_input', 10, 4 );
This code change order first and last name after order via rest api. I want to echo the billing first name where it says "asd". olso i need to other meta related to billing information.
add_action( 'woocommerce_payment_complete', 'digger_checkout_save_user_meta');
function digger_checkout_save_user_meta( $order_id ) {
//$order = wc_get_order( $order_id );
// $user_id = $order->get_user_id();
update_post_meta($order_id, '_billing_first_name', 'asd' );
update_post_meta($order_id, '_billing_last_name','bsd' );
}
i try this update_post_meta($order_id, '_billing_first_name', 'billing_first_name' ); but it doesnt work :) thx a lot for your replay.
Please help figure it out. Some strange behavior in the payment process. If the buyer makes a purchase without using the coupon code, if the payment is successful, it is immediately transferred to the status: completed(it's important). But as soon as the buyer makes a purchase using the coupon. Even with successful payment, the order is transferred to the status: Processing.
I tried to use this code:
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_paid_order', 20 );
function custom_woocommerce_auto_complete_paid_order( $order_id ) {
if ( ! $order_id )
return;
$order = new WC_Order( $order_id );
if ( $order->has_status( 'processing' ) ) {
$order->update_status( 'completed' );
}else {
return;
}
}
But it doesn't work. Anyway, when paying for an item using a coupon, it first gets into the status Processing. Please, help me figure it out.
add_action('woocommerce_order_status_completed', 'my_order_status_completed_sms', 10, 1);
function my_order_status_completed_sms($order_id){
$order = wc_get_order($order_id);
$user_id = $order->get_customer_id();
if($user_id){
$phone_number = get_user_meta($user_id, 'billing_phone', true);
if($phone_number){
cosmosfarm_members_sms_send($phone_number, '#'.$order_id.' is complete. thank you!');
}
}
}
When someone purchases from a woocommerce, woocommerce send sms by plug-in.
but there's some issues.
If the buyer is a member, sms will be sent out normally, no problem.
However, if the buyer is not a guest, no sms will be sent.
What's the problem?
Your function is dependend upon having user_id and getting the user_meta for that user.
function my_order_status_completed_sms( $order_id ) {
// Get the order object
$order = wc_get_order( $order_id );
// Get the billing phone number from the order
$phone_number = $order->get_billing_phone();
if ( $phone_number ) {
cosmosfarm_members_sms_send( $phone_number, '#' . $order_id . ' is complete. thank you!' );
}
}
Using the order object, the biling phone is there if entered for either customer or guest.
I am using WooCommerce Product Vendors plugin for vendor management.
Currently, when a new order is received the notification is sent to customer and admin. Once admin change order status to Processing or Completed then the email is sent to Vendor.
But I need to send that notification email when an order is received.
Can this be achieved by creating a filter in functions.php or maybe by triggering product status change notification on order received?
Updated: Added "New booking" email notification Id…
There is many ways to achieve that. Here I have 2 of them:
1). The first one, based on email ID "new Order". It is tested and works:
add_action ('woocommerce_email_customer_details', 'new_order_email_to_vendor', 20, 4 );
function new_order_email_to_vendor( $order, $sent_to_admin, $plain_text, $email ){
if( in_array( $email->id, ['new_order', 'new_booking'] ) ){
$mailer['WC_Product_Vendors_Order_Email_To_Vendor']->trigger( $order );
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
2). This is untested as it's based on order status change conditions. You can set in the if statement the "from" order statuses or some "to" order statuses or both…
Here I use the "from" 'pending' order status only, as all orders are always set in pending status during the payment process in Woocommerce:
add_action( 'woocommerce_order_status_changed', 'new_order_email_to_vendor', 10, 4 );
function new_order_email_to_vendor( $order_id, $old_status, $new_status, $order ){
if ( in_array( $new_status, array( 'processing','completed') ) { // <== Updated
$emails = WC()->mailer()->get_emails();
$emails['WC_Product_Vendors_Order_Email_To_Vendor']->trigger( $order );
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.