Remain Woocommerce Order Status to On-Hold - php

I do not want Woocommerce to automatically update the order status from "On hold" to "Completed". I want it to remain as as "On hold" since we are sending a Replacement Item and we are waiting for the original item to come back to us. Basically, I want it to set as "On-hold" even after the Item has been shipped. Is there a way to achieve this?
I tried using the code below but no luck:
add_action( 'woocommerce_payment_complete', 'cancel_completed_status' );
function cancel_completed_status( $order_id ){
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $item_id => $item ) {
$product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
if ( $order->has_status( 'on-hold' )) {
$order->update_status( 'on-hold' );
$order->save();
}
}
}

add_action('woocommerce_order_status_completed', 'completed_to_onhold');
function completed_to_onhold($order_id) {
$order = new WC_Order($order_id);
$order->update_status('on-hold');
}

Related

Set order status to "Processing" after payment for a single virtual product in WooCommerce

In WooCommerce, any virtual product order status is automatically marked as "Completed" after payment.
I need the status to be set to "Processing" after payment. This behavior should apply only to a single virtual product ID that I specify.
The closest solution that I found, for I'm looking for, is this:
Auto change order status to completed for specific products in WooCommerce
The only issue is that it sets the status to "Completed" instead of "Processing", and I need it the other way around.
function virtual_product_woocommerce_order_status( $order_id ) {
if( ! $order_id ) return;
$order = wc_get_order( $order_id );
$order_items = $order->get_items();
$is_virtual_found = false;
$order_product_ids = array();
$specific_product_id = 1342;
foreach ( $order_items as $item ) {
// Get product id
$product = wc_get_product( $item['product_id'] );
// Add the product IDS to the list
$order_product_ids[] = $item['product_id'];
// Is virtual
$is_virtual = $product->is_virtual();
// Is_downloadable
$is_downloadable = $product->is_downloadable();
// Backorders allowed
$backorders_allowed = $product->backorders_allowed();
if( $is_virtual && $is_downloadable && $backorders_allowed ) {
$is_virtual_found = true;
break;
}
}
// true
if( $is_virtaul_found && in_array($specific_product_id, $order_product_ids) ) {
$order->update_status( 'processing' );
}
}
add_action('woocommerce_thankyou', 'virtual_product_woocommerce_order_status', 10, 1 );

Disable order received emails for virtual products

I am currently using the code below to automatically complete virtual and downloadable products.
add_action('woocommerce_thankyou', 'wpd_autocomplete_virtual_orders', 10, 1 );
function wpd_autocomplete_virtual_orders( $order_id ) {
if( ! $order_id ) return;
// Get order
$order = wc_get_order( $order_id );
// get order items = each product in the order
$items = $order->get_items();
// Set variable
$only_virtual = true;
foreach ( $items as $item ) {
// Get product id
$product = wc_get_product( $item['product_id'] );
// Is virtual
$is_virtual = $product->is_virtual();
// Is_downloadable
$is_downloadable = $product->is_downloadable();
if ( ! $is_virtual && ! $is_downloadable ) {
$only_virtual = false;
}
}
// true
if ( $only_virtual ) {
$order->update_status( 'completed' );
}
}
The issue I am facing is that woocommerce sends out two emails in this scenario to the customer. Order Received + Order Completed. I am wondering if there is anyway to stop the order received email from going through to the customer and only have the order completed email sent.
Thank You
To prevent the "Order Received" email from being sent to the customer, you can use the woocommerce_email_enabled_customer_processing_order filter hook to disable the email.
Please try to add the following code inside your if ( $only_virtual )
add_filter( 'woocommerce_email_enabled_customer_processing_order', '__return_false' );
The easiest way to achieve this would be to bypass the WooCommerce update_status function as the email sending is built-in to the function.
Instead just update the order in wp_posts:
Replace: $order->update_status( 'completed' );
With:
wp_update_post(['ID' => $order_id, 'post_status' => 'wc-completed']);
You will then only receive the Order Received email!

Reduce stock only for specific order statuses and payment method in Woocommerce

I'm working on a bit of custom Woocommerce functionality for a client. They use the BACS payment gateway to handle manual payments.
However, the gateway currently reduces the stock too early for our needs, i.e., when the order is "On Hold". I would like to ONLY reduce the stock when the order is marked "Processing" or "Complete" (avoiding duplicate reductions).
I have manged to prevent the stock from reducing itself while "on hold" with the following snippet:
function wcs_do_not_reduce_onhold_stock( $reduce_stock, $order ) {
if ( $order->has_status( 'on-hold' ) ) {
$reduce_stock = false;
}
return $reduce_stock;
}
add_filter( 'woocommerce_can_reduce_order_stock', 'wcs_do_not_reduce_onhold_stock', 10, 2 );
I'm not too sure how to proceed though. While the above code works, adding the following condition does not:
else if ( $order->has_status( 'processing' ) || $order->has_status( 'completed' ) ) {
$reduce_stock = true;
}
In short, I'd ideally like the stock to change depending on the following stock statuses:
On Hold - Does nothing
Completed or Processing - Reduce Stock (Only once)
Cancelled - Increase Stock (Only if initially reduced)
Any help is much appreciated!
Using a custom function hooked in woocommerce_order_status_changed you will be able to target 'processing' and 'completed' order statuses change reducing order items stock.
I have added in your function a condition to target only "BACS" payment gateway on orders.
add_filter( 'woocommerce_can_reduce_order_stock', 'wcs_do_not_reduce_onhold_stock', 10, 2 );
function wcs_do_not_reduce_onhold_stock( $reduce_stock, $order ) {
if ( $order->has_status( 'on-hold' ) && $order->get_payment_method() == 'bacs' ) {
$reduce_stock = false;
}
return $reduce_stock;
}
add_action( 'woocommerce_order_status_changed', 'order_stock_reduction_based_on_status', 20, 4 );
function order_stock_reduction_based_on_status( $order_id, $old_status, $new_status, $order ){
// Only for 'processing' and 'completed' order statuses change
if ( $new_status == 'processing' || $new_status == 'completed' ){
$stock_reduced = get_post_meta( $order_id, '_order_stock_reduced', true );
if( empty($stock_reduced) && $order->get_payment_method() == 'bacs' ){
wc_reduce_stock_levels($order_id);
}
}
}
Code goes in function.php file of the active child theme (or active theme).
Tested and works
The accepted answer did not work for me. The first part did but not the second, this is how I modified it:
// This is the same as the accepted answer
add_filter( 'woocommerce_can_reduce_order_stock', 'wcs_do_not_reduce_onhold_stock', 10, 2 );
function wcs_do_not_reduce_onhold_stock( $reduce_stock, $order ) {
if ( $order->has_status( 'on-hold' ) && $order->get_payment_method() == 'bacs' ) {
$reduce_stock = false;
}
return $reduce_stock;
}
// This is what I changed
add_action( 'woocommerce_order_status_processing', 'reduce_stock_on_bacs_order_status_change', 10, 1 );
add_action( 'woocommerce_order_status_completed', 'reduce_stock_on_bacs_order_status_change', 10, 1 );
function reduce_stock_on_bacs_order_status_change( $order_id ) {
// Get the order object
$order = wc_get_order( $order_id );
// Check if the order was paid using BACS
if ( 'bacs' == $order->get_payment_method() ) {
// Check if the stock reduction has already been done for this order
$stock_reduction_done = get_post_meta( $order_id, '_stock_reduction_done', true );
if ( ! $stock_reduction_done ) {
// Iterate over the order items
foreach( $order->get_items() as $item_id => $item ) {
// Reduce stock for each item
$product = $item->get_product();
$qty = $item->get_quantity();
$product->reduce_stock( $qty );
}
// Mark the stock reduction as done for this order
update_post_meta( $order_id, '_stock_reduction_done', true );
}
}
}
This will not reduce the stock for BACS payments until the order is marked as processing or completed.

Woocommerce change user role on purchase

I am trying to use this snippet of code to update my users from the default role of 'Subscriber' to the role of 'Premium' on the purchase of a product from my store.
add_action( 'woocommerce_order_status_completed','change_role_on_purchase' );
function change_role_on_purchase( $order_id ) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
$products_to_check = array( '416' );
foreach ( $items as $item ) {
if ( $order->user_id > 0 && in_array( $item['product_id'], $products_to_check ) ) {
$user = new WP_User( $order->user_id );
// Change role
$user->remove_role( 'Subscriber' );
$user->add_role( 'Premium' );
// Exit the loop
break;
}
}
}
I only have 1 product in my store and it has the product ID 416 (which I have inserted in the code).
I have put this into functions.php, but i'm not having any luck. The role isn't being updated after any successful purchase. Any ideas?
Have a try with this one:
function change_role_on_purchase( $order_id ) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
if ( $order->user_id > 0 && $product_id == '416' ) {
update_user_meta( $order->user_id, 'paying_customer', 1 );
$user = new WP_User( $order->user_id );
// Remove role
$user->remove_role( 'subscriber' );
// Add role
$user->add_role( 'premium' );
}
}
}
add_action( 'woocommerce_order_status_processing', 'change_role_on_purchase' );
what if we want to check product category instead of product id? How do we tweak this? The code below is for verifying multiple products with their respective IDs.
add_action( 'woocommerce_order_status_processing', 'change_role_on_purchase' );
function change_role_on_purchase( $order_id ) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
$products_to_check = array( '27167', '27166' );
foreach ( $items as $item ) {
if ( $order->user_id > 0 && in_array( $item['product_id'], $products_to_check ) ) {
$user = new WP_User( $order->user_id );
// Change role
$user->remove_role( 'friends' );
$user->add_role( 'customer' );
// Exit the loop
break;
}
}
}
In case someone is interested in this. If your customer wants to have in the future more products that will update the buyers role after purchase, instead of manually adding the product ids in the function, you can use this one
$products_to_check = wc_get_products( array( 'return' => 'ids', 'tag' => array('your tag here') ) );
Your customer can edit by him/herself a product and assign the product tag, that will allow the update of the user role.
Same thing here... Please keep in mind that woocommerce_order_status_processing means just that: that the order status is set to "processing". This does NOT mean that the order is "complete", meaning that it has been paid for. If you use this hook, you run the risk of making content available to your customer even though his order may not have been paid for. This is what woocommerce_order_status_completed is there for, but that one doesn't seem to work for virtual products, which get the order status automatically set to "completed".
woocommerce_order_status_completed only worked for me with virtual products disabled/unchecked in the products section, meaning I had to manually "complete" the order in order to trigger this hook. Still looking for a solution...

Automatically apply a status for new orders woocommerce

I want to update the status of any and all orders (paid or unpaid) to completed, triggering the emails from woocommerce. Sounds counter intuitive but it needs to happen.
I thought something like this would work:
add_action('woocommerce_order_status_changed','status_changed_processsing');
function status_changed_processsing( $order_id, $checkout = null ) {
global $woocommerce;
$order = new WC_Order( $order_id );
//assign statu to that order
$order->status = 'completed';
}
}
But I have not been successful.
TIA. Appreciate any help!
Have a try with this one:
add_action( 'woocommerce_thankyou', 'status_changed_processsing' );
function status_changed_processsing( $order_id ) {
$order = new WC_Order( $order_id );
$order->update_status( 'completed' );
}
$order = new WC_Order($order_id);
if (!empty($order)) {
$order->update_status( 'completed' );
}
try like this

Categories