phpwordpresswoocommerceordersstatus

How to set a custom WooCommerce order status based on the pickup location of Local Pickup Plus


To offer pickup locations during checkout on a WooCommerce installation we use Local Pickup Plus of Skyverge. This tool works as expected and orders are set to "Pending". However, we want the ordersstatus to be set to a custom status based on the selected location.

I already created the custom statusses with the following code:

// Add the custom orderstatusses "In Behandeling Antwerpen" and "In Behandeling Mechelen"
add_action( 'init', 'register_custom_order_statuses' );
function register_custom_order_statuses() {
    register_post_status( 'wc-in_behandeling_locatie_antwerpen', array(
        'label'                     => 'In Behandeling Antwerpen',
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'In Behandeling Antwerpen (%s)', 'In Behandeling Antwerpen (%s)' )
    ));

    register_post_status( 'wc-in_behandeling_locatie_mechelen', array(
        'label'                     => 'In Behandeling Mechelen',
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'In Behandeling Mechelen (%s)', 'In Behandeling Mechelen (%s)' )
    ));
}

// Display the orderstatusses
add_filter( 'wc_order_statuses', 'add_custom_order_statuses_to_wc' );
function add_custom_order_statuses_to_wc( $order_statuses ) {
    $order_statuses['wc-in_behandeling_locatie_antwerpen'] = 'In Behandeling Antwerpen';
    $order_statuses['wc-in_behandeling_locatie_mechelen'] = 'In Behandeling Mechelen';
    return $order_statuses;
}

My approach to set the above statusses to the chosen location:

// Set the status based on the selected location during checkout
add_action( 'woocommerce_checkout_order_processed', 'change_order_status_based_on_pickup_location', 10, 3 );
function change_order_status_based_on_pickup_location( $order_id, $posted_data, $order ) {
    foreach( $order->get_shipping_methods() as $item_id => $item ){
        $location_id = $item->get_meta('_pickup_location_id');

        if ( $location_id == '8124' ) {
            $order->update_status( 'wc-in_behandeling_locatie_antwerpen', 'In Behandeling Antwerpen' );
        } elseif ( $location_id == '8123' ) {
            $order->update_status( 'wc-in_behandeling_locatie_mechelen', 'In Behandeling Mechelen' );
        } else {
            error_log( 'Afhaallocatie niet herkend: ' . print_r( $location_id, true ) );
        }
    }
}

This code doens't work as expected. The status is still "pending".


Solution

  • There are some missing things in your code and your custom status slugs are too long… Here, I better use woocommerce_new_order hook, to update the status.

    Try the following revised code (compatible with High Performance Order Storage):

    // Add the custom orderstatusses "In Behandeling Antwerpen" and "In Behandeling Mechelen"
    add_action( 'init', 'register_custom_order_statuses' );
    function register_custom_order_statuses() {
        register_post_status( 'wc-await-antwerpen', array(
            'label'                     => _x( 'In Behandeling Antwerpen', 'shop_order', 'woocommerce' ),
            'public'                    => true,
            'exclude_from_search'       => false,
            'show_in_admin_all_list'    => true,
            'show_in_admin_status_list' => true,
            'label_count'               => _n_noop( 'In Behandeling Antwerpen (%s)', 'In Behandeling Antwerpen (%s)' )
        ));
    
        register_post_status( 'wc-await-mechelen', array(
            'label'                     => _x( 'In Behandeling Mechelen', 'shop_order', 'woocommerce' ),
            'public'                    => true,
            'exclude_from_search'       => false,
            'show_in_admin_all_list'    => true,
            'show_in_admin_status_list' => true,
            'label_count'               => _n_noop( 'In Behandeling Mechelen (%s)', 'In Behandeling Mechelen (%s)' )
        ));
    }
    
    // Display custom order statusses
    add_filter( 'wc_order_statuses', 'add_custom_order_statuses_to_wc' );
    function add_custom_order_statuses_to_wc( $order_statuses ) {
        $order_statuses['wc-await-antwerpen'] = _x( 'In Behandeling Antwerpen', 'wc-order', 'woocommerce' );
        $order_statuses['wc-await-mechelen']  = _x( 'In Behandeling Mechelen', 'wc-order', 'woocommerce' );
    
        return $order_statuses;
    }
    
    add_action( 'woocommerce_new_order', 'change_order_status_based_on_pickup_location', 10, 2 );
    function change_order_status_based_on_pickup_location( $order_id, $order ) {
        foreach( $order->get_shipping_methods() as $item_id => $item ){
            $location_id = $item->get_meta('_pickup_location_id');
    
            if ( $location_id == '8124' ) {
                $order->update_status( 'wc-await-antwerpen', 'In Behandeling Antwerpen' );
            } elseif ( $location_id == '8123' ) {
                $order->update_status( 'wc-await-mechelen', 'In Behandeling Mechelen' );
            }
        }
    }
    

    It should work for you.