Change default order status | WordPress.org

I figured it out. Here’s the code I used for variable products. Will require additional modification to apply to simple products as well.

/**
 * 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();
			$variation_id = $item->get_variation_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 = get_post_meta( $variation_id, '_wpro_variable_is_preorder', true );

            if ( $pre_order_checked == 'yes' ) {
                // 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 == 'yes' ) {
            $status = 'preorder';
        } 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 );

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Your Advertisement Here