Case:
In checkout packages are splited by shipping class of products (different brands).
Every package have own shipping method that customer can choose.
And finally when customer make an order I need to store chosen shipping method for every product in meta_data so I can see it in order details on admin panel and for the RESTAPI (I use some external services to manage orders).
I tried something like this but I know this is mess, I don't understand how to handle session data and store it in order data.
add_action( 'woocommerce_checkout_create_order_shipping_item', 'order_shipping_item', 20, 4 );
function order_shipping_item($cartItemData,$order){
foreach ( WC()->cart->get_shipping_packages() as $package_id => $package ) {
// Check if a shipping for the current package exist
if ( WC()->session->__isset( 'shipping_for_package_'.$package_id ) ) {
// Loop through shipping rates for the current package
foreach ( WC()->session->get( 'shipping_for_package_'.$package_id )['rates'] as $shipping_rate_id => $shipping_rate ) {
$method_id = $shipping_rate->get_method_id(); // The shipping method slug
$instance_id = $shipping_rate->get_instance_id(); // The instance ID
$label_name = $shipping_rate->get_label(); // The label name of the method
}
}
foreach ($package['contents'] as $item) {
$item->add_meta_data('_shipping_method',$label_name);
# code...
}
}
}
Right now I have no idea how to achieve this, any clues? help!
Here is the way to save as custom order item meta data the chosen shipping method details for each order item:
// Custom function that get the chosen shipping method details for a cart item
function get_cart_item_shipping_method( $cart_item_key ){
$chosen_shippings = WC()->session->get( 'chosen_shipping_methods' ); // The chosen shipping methods
foreach( WC()->cart->get_shipping_packages() as $id => $package ) {
$chosen = $chosen_shippings[$id]; // The chosen shipping method
if( isset($package['contents'][$cart_item_key]) && WC()->session->__isset('shipping_for_package_'.$id) ) {
return WC()->session->get('shipping_for_package_'.$id)['rates'][$chosen];
}
}
}
// Save shipping method details in order line items (product) as custom order item meta data
add_action( 'woocommerce_checkout_create_order_line_item', 'add_order_line_item_custom_meta', 10, 4 );
function add_order_line_item_custom_meta( $item, $cart_item_key, $values, $order ) {
// Load shipping rate for this item
$rate = get_cart_item_shipping_method( $cart_item_key );
// Set custom order item meta data
$item->update_meta_data( '_shipping_rate_id', $rate->id ); // the shipping rate ID
$item->update_meta_data( '_shipping_rate_method_id', $rate->method_id ); // the shipping rate method ID
$item->update_meta_data( '_shipping_rate_instance_id', (int) $rate->instance_id ); // the shipping rate instance ID
$item->update_meta_data( '_shipping_rate_label', $rate->label ); // the shipping rate label
$item->update_meta_data( '_shipping_rate_cost', wc_format_decimal( (float) $rate->cost ) ); // the shipping rate cost
$item->update_meta_data( '_shipping_rate_tax', wc_format_decimal( (float) array_sum( $rate->taxes ) ) ); // the shipping rate tax
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
I wish to disable auto-complete orders but only for those orders where amount is higher than 30 EUR for example. All products are virtual.
I found this function on net:
add_action( 'woocommerce_thankyou', 'stop_auto_complete_order' );
function stop_auto_complete_order( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
$order->update_status( 'processing' );
}
However I have no clue how to make it only if order is higher than specific amount.
You need to use some WC_Order methods like:
The total order amount: get_total()
The order status: get_status()
Based on your code, the following will:
auto-complete paid orders up to 30 (will be set to "completed" status)
auto-process paid orders bigger than 30 (will be set to "processing" status)
The code:
add_action( 'woocommerce_thankyou', 'stop_auto_complete_order' );
function stop_auto_complete_order( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
// Auto-complete paid orders up to 30 (for "on-hold" and "processing" order statuses)
if ( $order->get_total() <= 30 && in_array( $order->get_status(), [ 'on-hold', 'processing' ] ) ) {
$order->update_status( 'completed' );
}
// Other paid orders are set to "processing" status
elseif( $order->get_status() === 'on-hold' ) {
$order->update_status( 'processing' );
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Sometimes the function woocommerce_thankyou is not called, but sometimes works fine.
Our code is:
add_action(‘woocommerce_thankyou’, ‘send_order_information_for_delivery’, 999, 1);
function send_order_information_for_delivery($order_id)
{
$order = wc_get_order($order_id);
$order_items = $order->get_items();
// … …
}
Any idea why sometimes doesn’t work?
The main objective of this method is to obtain the information of the purchase order and its items and to send them to another database through an API.
The strange thing is that in some orders the method is not called.
Instead, you could try to use the following hooked function, that will work for defined order statuses only once, after the delivery data is processed.
add_action( 'woocommerce_order_status_changed', 'delivery_information_process', 20, 4 );
function delivery_information_process( $order_id, $old_status, $new_status, $order ){
// Define the order statuses to check
$statuses_to_check = array( 'on-hold', 'processing' );
// Only "On hold" order status and "Free Shipping" Shipping Method
if ( $order->get_meta( '_delivery_check', true ) && in_array( $new_status, $statuses_to_check ) )
{
// Getting all WC_emails objects
foreach($order->get_items() as $item_id => $item ){
$product = $item->get_product();
$sku = $product->get_sku();
}
## ==> Process delivery data step
// Once delivery information is send or processed ==> update '_delivery_check' to avoid repetitions
$order->update_meta_data( '_delivery_check', true );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
In Woocommerce, I would like to automatically put all Woocommerce Subscriptions "on hold" rather than "active" when the order is still "processing". Once I mark the order as "completed" that subscription should change to "active".
I've tried everything I can think of, if somebody knows how to do this please let me know.
I'm running wordpress 4.8.1 / Woocommerce 3.1.2 / Woocommerce Subscriptions 2.2.7 / and the payment gateway is Stripe 3.2.3.
This can be done in two steps:
1) With a custom function hooked in woocommerce_thankyou action hook, when the order has a 'processing' status and contains subscriptions, we update the subscriptions status to 'on-hold':
add_action( 'woocommerce_thankyou', 'custom_thankyou_subscription_action', 50, 1 );
function custom_thankyou_subscription_action( $order_id ){
if( ! $order_id ) return;
$order = wc_get_order( $order_id ); // Get an instance of the WC_Order object
// If the order has a 'processing' status and contains a subscription
if( wcs_order_contains_subscription( $order ) && $order->has_status( 'processing' ) ){
// Get an array of WC_Subscription objects
$subscriptions = wcs_get_subscriptions_for_order( $order_id );
foreach( $subscriptions as $subscription_id => $subscription ){
// Change the status of the WC_Subscription object
$subscription->update_status( 'on-hold' );
}
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
2) With a custom function hooked in woocommerce_order_status_completed action hook, when the order status is changed to "completed", it will auto change the subscription status to "active":
// When Order is "completed" auto-change the status of the WC_Subscription object to 'on-hold'
add_action('woocommerce_order_status_completed','updating_order_status_completed_with_subscription');
function updating_order_status_completed_with_subscription($order_id) {
$order = wc_get_order($order_id); // Get an instance of the WC_Order object
if( wcs_order_contains_subscription( $order ) ){
// Get an array of WC_Subscription objects
$subscriptions = wcs_get_subscriptions_for_order( $order_id );
foreach( $subscriptions as $subscription_id => $subscription ){
// Change the status of the WC_Subscription object
$subscription->update_status( 'active' );
}
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
All code is tested on Woocommerce 3+ and works.
Normally wooCommerce should autocomplete orders for virtual products. But it doesn't and this is a real problem, even a BUG like.
So at this point you can find somme helpful things(but not really convenient):
1) A snippet code (that you can find in wooCommerce docs):
/**
* 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' );
}
But this snippet does not work for BACS*, Pay on delivery and Cheque payment methods. It's ok for Paypal and Credit Card gateways payment methods.
*BACS is a Direct Bank transfer payment method
And …
2) A plugin: WooCommerce Autocomplete Orders
This plugin works for all payment methods, but not for other Credit Card gateways payment methods.
My question:
Using (as a base) the wooCommerce snippet in point 1:
How can I implement conditional code based on woocommerce payment methods?
I mean something like: if the payment methods aren't "BACS", "Pay on delivery" and "Cheque" then apply the snippet code (update status to "completed" for paid orders concerning virtual products).
Some help will be very nice.
The most accurate, effective and lightweight solution (For WooCommerce 3 and above) - 2019
This filter hook is located in:
WC_Order Class inside payment_complete() method which is used by all payment methods when a payment is required in checkout.
WC_Order_Data_Store_CPT Class inside update() method.
As you can see, by default the allowed paid order statuses are "processing" and "completed".
###Explanations:
Lightweight and effective:
As it is a filter hook, woocommerce_payment_complete_order_status is only triggered when an online payment is required (not for "cheque", "bacs" or "cod" payment methods). Here we just change the allowed paid order statuses.
So no need to add conditions for the payment gateways or anything else.
Accurate (avoid multiple notifications):
This is the only way to avoid sending 2 different customer notifications at the same time:
• One for "processing" orders status
• And one for "completed" orders status.
So customer is only notified once on "completed" order status.
Using the code below, will just change the paid order status (that is set by the payment gateway for paid orders) to "completed":
add_action( 'woocommerce_payment_complete_order_status', 'wc_auto_complete_paid_order', 10, 3 );
function wc_auto_complete_paid_order( $status, $order_id, $order ) {
return 'completed';
}
Code goes in function.php file of the active child theme (or active theme).
Related: WooCommerce: autocomplete paid orders based on shipping method
2018 - Improved version (For WooCommerce 3 and above)
Based on Woocommerce official hook, I found a solution to this problem *(Works with WC 3+).
In Woocommerce for all other payment gateways others than bacs (Bank Wire), cheque and cod (Cash on delivery), the paid order statuses are "processing" and "completed".
So I target "processing" order status for all payment gateways like Paypal or credit card payment, updating the order status to complete.
The code:
add_action( 'woocommerce_thankyou', 'wc_auto_complete_paid_order', 20, 1 );
function wc_auto_complete_paid_order( $order_id ) {
if ( ! $order_id )
return;
// Get an instance of the WC_Product object
$order = wc_get_order( $order_id );
// No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
if ( in_array( $order->get_payment_method(), array( 'bacs', 'cod', 'cheque', '' ) ) ) {
return;
}
// For paid Orders with all others payment methods (paid order status "processing")
elseif( $order->has_status('processing') ) {
$order->update_status( 'completed' );
}
}
Code goes in function.php file of the active child theme (or active theme).
Original answer (For all woocommerce versions):
The code:
/**
* AUTO COMPLETE PAID ORDERS IN WOOCOMMERCE
*/
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_paid_order', 10, 1 );
function custom_woocommerce_auto_complete_paid_order( $order_id ) {
if ( ! $order_id )
return;
$order = wc_get_order( $order_id );
// No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
if ( ( 'bacs' == get_post_meta($order_id, '_payment_method', true) ) || ( 'cod' == get_post_meta($order_id, '_payment_method', true) ) || ( 'cheque' == get_post_meta($order_id, '_payment_method', true) ) ) {
return;
}
// For paid Orders with all others payment methods (with paid status "processing")
elseif( $order->get_status() === 'processing' ) {
$order->update_status( 'completed' );
}
}
Code goes in function.php file of the active child theme (or active theme).
With the help of this post: How to check payment method on a WooCommerce order by id?
with this : get_post_meta( $order_id, '_payment_method', true ); from helgatheviking
"Bank wire" (bacs), "Cash on delivery" (cod) and "Cheque" (cheque) payment methods are ignored and keep their original order status.
Updated the code for compatibility with WC 3.0+ (2017-06-10)
For me this hook was called even if the payment did not go through or failed, and this resulted to completed failed payments. After some research I changed it to use 'woocommerce_payment_complete' because it's called only when payment is complete and it covers the issue that #LoicTheAztec mentions above –
add_action( 'woocommerce_payment_complete', 'wc_auto_complete_paid_order', 20, 1 );
function wc_auto_complete_paid_order( $order_id ) {
if ( ! $order_id )
return;
// Get an instance of the WC_Product object
$order = wc_get_order( $order_id );
// No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
if ( in_array( $order->get_payment_method(), array( 'bacs', 'cod', 'cheque', '' ) ) ) {
return;
// Updated status to "completed" for paid Orders with all others payment methods
} else {
$order->update_status( 'completed' );
}
}
For me, the simplest hook to modify order status when payment is completed is 'woocommerce_order_item_needs_processing' as this filter hook is meant to modify the target order status when payment completes.
This is what the final snippet will look alike:
add_filter('woocommerce_order_item_needs_processing', '__return_false',999);
It also compatible with the other plugins on sites.
For me, on a testing system with PayPal Sandbox (WooCommerce PayPal Payments plugin) the LoicTheAztec solution (2019 update) worked only when I added the $order->update_status( 'completed' ); code line. The return 'completed'; has not an effect in my case, I left it just because it's a filter.
add_filter( 'woocommerce_payment_complete_order_status', function( $status, $order_id, $order ) {
$order->update_status( 'completed' );
return 'completed';
}, 10, 3 );
If you are looking for autocompletion of virtual orders (like courses, ebooks, downloadables etc), this might be useful.
* Auto Complete all WooCommerce virtual orders.
*
* #param int $order_id The order ID to check
* #return void
*/
function custom_woocommerce_auto_complete_virtual_orders( $order_id ) {
// if there is no order id, exit
if ( ! $order_id ) {
return;
}
// No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
if ( in_array( $order->get_payment_method(), array( 'bacs', 'cod', 'cheque', '' ) ) ) {
return;
}
// get the order and its exit
$order = wc_get_order( $order_id );
$items = $order->get_items();
// if there are no items, exit
if ( 0 >= count( $items ) ) {
return;
}
// go through each item
foreach ( $items as $item ) {
// if it is a variation
if ( '0' != $item['variation_id'] ) {
// make a product based upon variation
$product = new WC_Product( $item['variation_id'] );
} else {
// else make a product off of the product id
$product = new WC_Product( $item['product_id'] );
}
// if the product isn't virtual, exit
if ( ! $product->is_virtual() ) {
return;
}
}
/*
* If we made it this far, then all of our items are virual
* We set the order to completed.
*/
$order->update_status( 'completed' );
}
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_virtual_orders' );
Adapted from https://gist.github.com/jessepearson/33f383bb3ea33069822817cfb1da4258