Change date and time labels based on selected shipping method

In v1.7.11 of WooCommerce Delivery Slots I made it possible to change the date and time labels based on the selected shipping method.

By default, you will see this:

But what if your shipping method is "Local Pickup". Having the labels "Delivery Details", "Delivery Date", and ""Please choose a time slot for your delivery" wouldn't be relevant.

Fortunately, it's simple to change with some code. You can add this to your child theme's functions.php file, or add it as a separate plugin.

/**
 * Modify delivery slots labels.
 *
 * @param array         $labels
 * @param null|WC_Order $order
 *
 * @return array
 */
function iconic_modify_delivery_slots_labels( $labels, $order ) {
	$chosen_shipping = $order ? iconic_get_order_shipping_method( $order ) : iconic_get_selected_shipping_method();

	if ( ! $chosen_shipping ) {
		return $labels;
	}

	if ( $chosen_shipping === 'flat_rate:7' ) {
		$labels['details'] = __( 'Collection Details', 'iconic' );
		$labels['date'] = __( 'Collection Date', 'iconic' );
		$labels['select_date'] = __( 'Select a collection date', 'iconic' );
		$labels['choose_date'] = __( 'Please choose a date for your collection.', 'iconic' );
		$labels['choose_time_slot'] = __( 'Please choose a time slot for your collection.', 'iconic' );
	}

	return $labels;
}

add_filter( 'iconic_wds_labels', 'iconic_modify_delivery_slots_labels', 10, 2 );

We're using the filter iconic_wds_labels which should return an array of labels. It accepts 2 parameters:

  • $labels
    An array of labels.
  • $order
    Either null, or a WC_Order object.

If an order object is present, we know that this order has already been placed. As such we want to fetch the shipping method from the order object. Otherwise, we want to use the currently selected shipping method at checkout.

I'm using 2 helper functions to do this; iconic_get_order_shipping_method() and iconic_get_selected_shipping_method().

/**
 * Get order shipping method.
 *
 * @param WC_Order $order
 *
 * @return bool|string
 */
function iconic_get_order_shipping_method( $order ) {
	$chosen_methods = $order->get_shipping_methods();

	if ( empty( $chosen_methods ) ) {
		return false;
	}

	$chosen_shipping = array_shift( $chosen_methods );
	return sprintf( '%s:%s', $chosen_shipping->get_method_id(), $chosen_shipping->get_instance_id() );
}

/**
 * Get selected shipping method.
 *
 * @return string|bool
 */
function iconic_get_selected_shipping_method() {
	if ( is_admin() ) {
		return false;
	}

	$chosen_methods  = WC()->session->get( 'chosen_shipping_methods' );

	if ( empty( $chosen_methods ) ) {
		return false;
	}

	return $chosen_methods[0];
}

In the iconic_modify_delivery_slots_labels() function, I then check the value of $chosen_shipping_method. If it's a collection method, I change the labels.

These labels will now be more relevant at checkout, on the thank you page, and in the admin area.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.