Automatically Update Order Status to Complete When Shipment Tracking Number Input - WooCommerce - woocommerce

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 );

Related

Run a custom code after a product is sold in woocommerce

I need to know how to assign custom actions (or run custom code) when someone buys something in the website and the payment has been completed. I need this so our system can send order's data to an API.
i am using wordpress and woocommerce for the website.
how can i find the order dynamics variable to put it in my API's code ?
if there,s anyone who use wordpress and woocommerce, please tell me where can i find order's dynamic variables such as:
product id which the customer has been ordered
product count and etc.
You could write a function that connects to the woocommerce_order_status_processing hook. At this point, the payment has been accepted and WooCommerce is waiting for the store to fulfill the order.
add_action( 'woocommerce_order_status_processing', 'my_order_complete_function', 10, 1 );
function my_order_complete_function( $order_id ) {
$order = wc_get_order( $order_id );
foreach($order->get_items() as $item) {
$product_id = $item->get_product_id();
$product = wc_get_product( $product_id );
// Add your API call here.
}
}

Change Verbiage on WooCommerce Subscriptions Customer Facing

I'm looking to change the verbiage for the word "Suspend" to the word "Pause" on the customer-facing side of WooCommerce Subscriptions. I have the filter that pulls up the action buttons but I'm unsure of how to pull the specific action of "Suspend" and change it to the word "Pause".
Here is the filter.
apply_filters( 'wcs_view_subscription_actions', $actions, $subscription );
Any help would be much appreciated. If you need more information I'm happy to provide what I can.
You can use the wcs_view_subscription_actions filter hook. this way. check below code. code will go in your active theme functions.php file.
add_filter( 'wcs_view_subscription_actions', 'change_action_buttons_label', 10, 2 );
function change_action_buttons_label( $actions, $subscription ){
if( isset( $actions['suspend'] ) ){
$actions['suspend']['name'] = __( 'Pause', 'woocommerce-subscriptions' );
}
return $actions;
}

Update address fields relating to order via rest api in WooCommerce

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.

WooCommerce Product Vendors: Send notification to vendor for new order received

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.

WooCommerce pass order data into woocommerce_checkout_update_user_meta

I'm trying to add user_meta upon checkout in WooCommerce. Currently I'm using woocommerce_checkout_update_user_meta action to update user meta. I'd like to pass some of the order data itself in as well specifically I'd like to make the order id the value of the meta like so...
function woocommerce_add_my_user_meta( $user_id ) {
global $woocommerce;
update_user_meta( $user_id, 'purchased', ''.$order->ID.'' );
}
add_action('woocommerce_checkout_update_user_meta', 'woocommerce_add_my_user_meta');
This however is not working. It's just adding a blank purchased user meta.
Any help greatly appreciated.
You could use the woocommerce_checkout_update_order_meta hook
// Use hook after checkout
add_action( 'woocommerce_checkout_update_order_meta', 'do-additional-stuff-on-checkout', 10, 2 );
// Things you want to be done when hook is called
function do-additional-stuff-on-checkout( $order_id, $post_values ) {
// get user
$current_user = wp_get_current_user();
// update
update_user_meta( $current_user->ID, 'purchased', $order_id );
}

Resources