• Resolved rabiancone

    (@rabiancone)


    Hello,

    I would like preorders to automatically go to a custom status I’ve created aptly titled “preorder”

    Can you provide a php code snippet that I can add to my functions.php that would allow me to dictate the default status for preorder items?

    Thanks,
    Ryan

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter rabiancone

    (@rabiancone)

    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 );
    Plugin Support angelagrey

    (@angelagrey)

    Thank you for the effort!

    The code runs smoothly!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Change default order status’ is closed to new replies.