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”.

Here’s an updated version of your code using the woocommerce_thankyou hook:

// Set the status based on the selected location after payment is completed
add_action( ‘woocommerce_thankyou’, ‘change_order_status_based_on_pickup_location’, 10, 1 );
function change_order_status_based_on_pickup_location( $order_id ) {
if ( !$order_id ) return;

$order = wc_get_order( $order_id );

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', 'Order status changed to In Behandeling Antwerpen based on pickup location.' );
    } elseif ( $location_id == '8123' ) {
        $order->update_status( 'wc-in_behandeling_locatie_mechelen', 'Order status changed to In Behandeling Mechelen based on pickup location.' );
    } else {
        error_log( 'Afhaallocatie niet herkend: ' . print_r( $location_id, true ) );
    }
}

}

  • Replaced woocommerce_checkout_order_processed with woocommerce_thankyou to run after the payment has been completed.
  • Modified the function to only accept the $order_id parameter, as woocommerce_thankyou provides just the order ID.
  • Used wc_get_order() to retrieve the order object.