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