I'd like to pass the payment method to the order notes section of a Woocommerce Order (when viewing the order in the backend)
This field gets imported into our ShipStation software. Right now, I have no way of knowing what payment method a customer used when viewing an order from ShipStation. (we give a free gift when a certain payment method is used.)
At first, viewing the payment method in Wordpress Admin is available. You just have to enable it in Screen Options.
If you want to add the Shipment method into the order note, you need to just add this to your functions.php:
add_action( ‘woocommerce_new_order’, ‘add_engraving_notes’, 1, 1 );
function add_engraving_notes( $order_id ) {
$order = new WC_Order( $order_id );
$note = $order->getShippingMethod();;
$order->add_order_note( $note );
$order->save();
}
Related
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.
}
}
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 );
I have an e-commerce site running on WordPress, I have added functionality in which whenever a user checkout, the API of my courier is hit and a tracking code is saved in the database.
Now I want to create a custom order from the backend (Add new order in Woocommerce) and do the same thing, like calling the API and saving the tracking returned from the API call to the database, but don't know which hook to use.
you should use 'woocommerce_new_order' hook:
add_action( 'woocommerce_new_order', 'api_call', 1, 1 );
function api_call( $order_id ) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
// etc...
}
fires after new order created
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.
I'm working in WordPress 4.3.1., with WooCommerce 2.4.7. For my employer's current store project, we need to have unique IDs generated for each item purchased, which can never be the same as any other purchase.
For example, if someone buys 3 shirts of the same design and size, each of these shirts, once in the cart, should each have a unique item ID displayed. Regardless of the number of orders we get, the unique item IDs cannot be duplicated.
As for the ID, I was considering mixing the product SKU with the date, and possibly the order ID to generate the date. How exactly would I go about this? Is there a plugin that can handle this, or should I deal in straight PHP?
I assume you want to use the unique id for work order tracking internally
use this hook (I am using processing rather then completed)
add_action( 'woocommerce_order_status_processing', 'add_unique_id' );
Then do this
function add_unique_id($order_id) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ($items as $item_id => $product ) {
$gen_id = "generate id goes here";
wc_add_order_item_meta($item_id, 'unique_id', $gen_id);
}
You could use a WooCommerce hook like woocommerce_order_status_completed in order to run something after every purchase.
add_action( 'woocommerce_order_status_completed', 'custom_function' );
function custom_function($order_id) {
// completed order object (can be useful)
$order = new WC_Order( $order_id );
// run what you want here
}
I can suggest you to use a post meta or an user meta (based on your needs).