
In a Business Bloomer Club Slack thread, a member recently sought advice on a unique WooCommerce shipping scenario: they support a small wholesale business with dedicated delivery trucks for specific areas and wanted to add a custom field for each customer identifying the truck route associated with their delivery location.
The goal was to include this truck route metadata in every order, which would then be uploaded to the warehouse system. This setup presents a few challenges, particularly when it comes to associating specific trucks with customers and orders automatically.
Here’s an overview of our discussion, including plugin options and code snippet solutions to accomplish this.
This request involves creating a dynamic setup where each customer has an assigned delivery truck route. The route metadata then needs to flow seamlessly into each new order for backend processing. Here’s how our community recommended tackling it.
Step 1: Adding a Custom Customer Field for Truck Route
The first step is to create a custom user field in the WooCommerce checkout or customer profile that identifies the truck assigned to each customer. This field can be populated manually or through an automated process based on customer location data, such as zip code or address.
Here’s a basic code snippet to add a custom field to the customer’s profile, specifically for truck route data:
add_action('show_user_profile', 'add_truck_route_field');
add_action('edit_user_profile', 'add_truck_route_field');
function add_truck_route_field($user) {
?>
<h3>Truck Route</h3>
<table class="form-table">
<tr>
<th><label for="truck_route">Truck Route</label></th>
<td>
<input type="text" name="truck_route" id="truck_route" value="<?php echo esc_attr(get_the_author_meta('truck_route', $user->ID)); ?>" class="regular-text" />
<p class="description">Assign the truck route associated with this customer.</p>
</td>
</tr>
</table>
<?php
}
Step 2: Saving Truck Route Data to Orders
Once the truck route is associated with each customer, you need to pass this information to every order they place. A helpful resource here is our Business Bloomer guide on adding custom fields to orders. With this approach, you can save the truck route field to the order upon checkout.
Using the example in Part 3 of the snippet guide, you can automatically pull in the truck route field data and save it as order metadata:
add_action('woocommerce_checkout_update_order_meta', 'save_truck_route_to_order');
function save_truck_route_to_order($order_id) {
if ($user_id = get_current_user_id()) {
$truck_route = get_user_meta($user_id, 'truck_route', true);
if ($truck_route) {
update_post_meta($order_id, 'truck_route', sanitize_text_field($truck_route));
}
}
}
Step 3: Extracting and Exporting Truck Route Data
Once truck route metadata is saved to orders, it can be exported using WooCommerce reporting or through plugins like WooCommerce Customer/Order/Coupon Export. This allows the data to be extracted and uploaded to the warehouse backend system efficiently, as required.
Plugin Alternatives for Easy Setup
If coding seems complex or your team needs a quicker solution, consider Advanced Custom Fields or WooCommerce Customer/Order XML Export Suite. These plugins can simplify creating, storing, and exporting custom fields with minimal development.
By adding custom customer and order fields, this WooCommerce setup allows the wholesaler to assign truck routes per customer and ensures this critical delivery data is stored with each order. Whether using custom code or leveraging WooCommerce plugins, this solution streamlines order fulfillment and improves backend integration for a smoother, more customized logistics process.