I'm using a couple of plugins to help with visibility surrounding pre-order products.
The first is Custom Order Status Pro which I use to create additional order statuses beyond what default WooCommerce offers.
The second is Product Pre-Orders for WooCommerce which allows for a slightly different add-to-cart and tracking function for out-of-stock items than the default WooCommerce "backorders" functionality.
Currently when orders are accepted they briefly go to "processing" status and then automatically updated to "completed" once payment has been confirmed.
I would like to add some PHP code that will automatically identify when a pre-order product has been purchased and update to the custom status "Preorder".
Here is a reference to a previous post that references the preorder function
CodePudding user response:
then automatically updated to "completed" once payment has been confirmed.
I am not sure about the above line, how you can automatic completed
status, generally, product status goto on-hold
then goes to processing
when payment is done, order complete should be done manually unless these are not virtual items ordered.
Maybe you have some kind of custom code to automatically order the completed system.
Leaving that aside, you'll have to use woocommerce_payment_complete_order_status
filter hook, check details on this hook here https://wp-kama.com/plugin/woocommerce/hook/woocommerce_payment_complete_order_status
this hook provides you 3 @params:
$status
- Payment status that should be set on payment complete$order_id
- Order ID$order
- Order object.
What you can do here, you can attach a function with this filter hook and you can loop through order items and validate if any of the items is a pre-order product, then you can return the order status that you want to set for the order, if you don't find anything then you can return the original status that was provided as hook param.
Here is the code snippet with an explanation, you'll have to do some modifications in this snippet based on your need.
/**
* Set pre order status when order has any pre order items
*
* @param string $status Payment status that should be set on payment complete.
* @param int $order_id Order ID.
* @param WC_Order $order Order object.
*
* @return string
*/
function vh_sto_wc_set_pre_order_status_on_payment_complete( $status, $order_id, $order ) {
// Make a check that new status is "processing" as we don't want to do anything for virtual orders.
// If you have to perform check for virtual orders as well then you can add or check ('processing' === $status || 'completed' === $status).
if ( 'processing' === $status && ( 'on-hold' === $order->status || 'pending' === $order->status || 'failed' === $order->status ) ) {
// Define has pre order false initially.
$has_pre_order = false;
// Loop through order items.
foreach ( $order->get_items() as $item_id => $item ) {
// Get the product object from the order item.
$product = $item->get_product();
// Get the product id.
$product_id = $product->get_id();
// Write your logic to validate if product is a pre order product or not.
// then store in $pre_order_checked as true/false.
$pre_order_checked = '';
if ( $pre_order_checked ) {
// assuming you perform a true condition test in if statement
// then set has pre order to true.
$has_pre_order = true;
// Break the loop as we got what we want.
break;
}
}
if ( $has_pre_order ) {
$status = 'YOUR DESIRED STATUS NAME';
} else {
// Autocomplete the processing order if the pre-order is unidentified.
$status = 'completed';
}
}
return $status;
}
add_action( 'woocommerce_payment_complete_order_status', 'vh_sto_wc_set_pre_order_status_on_payment_complete', 20, 3 );