WooCommerce is a powerful plugin for managing eCommerce, powering over 25% of all online stores. However, there are times when you may need to add custom functionality not included by default. In this example, we’ll explore filtering shipping methods based on a specified WooCommerce shipping class. If you’re new to shipping classes, they essentially act as “tags” that can be assigned to products to trigger specific functions. Here, we’ll demonstrate how to display only our FedEx Flat Rate shipping method when a product with the Flat Rate shipping class is in the cart.
The Code
<?php
add_filter( 'woocommerce_package_rates', 'emoxie_filter_shipping_methods', 9, 2 );
function emoxie_filter_shipping_methods( $rates, $package ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$fedex_shipping_method_key = null;
// Locate FedEx Flat Rate
foreach ( $rates as $rate_key => $rate ) {
if ( is_object( $rate ) && method_exists( $rate, 'get_label' ) && $rate->get_label() === "FedEx Flat Rate" ) {
$fedex_shipping_method_key = $rate_key;
break;
}
}
// If FedEx Flat Rate is found
if ( $fedex_shipping_method_key !== null ) {
$flat_rate_product = false;
// Check if any product in the package has 'fedex-flat-rate' shipping class
foreach ( $package['contents'] as $item ) {
$product = wc_get_product( $item['product_id'] );
if ( $product && 'fedex-flat-rate' === $product->get_shipping_class() ) {
$flat_rate_product = true;
break;
}
}
// Retain only FedEx Flat Rate if a flat-rate product is found
if ( $flat_rate_product ) {
foreach ( $rates as $key => $rate ) {
if ( $key !== $fedex_shipping_method_key ) {
unset( $rates[$key] );
}
}
}
} else {
// If FedEx Flat Rate not found, remove it
unset( $rates[$fedex_shipping_method_key] );
}
return $rates;
}
The Code Explained
1. Adding a Filter to Shipping Rates:
The add_filter function hooks into WooCommerce’s shipping rates calculation (woocommerce_package_rates) to modify the available shipping methods for a customer’s order by calling the custom function emoxie_filter_shipping_methods.
2. Function Definition:
The emoxie_filter_shipping_methods function takes two parameters: $rates, which contains all available shipping methods, and $package, which holds information about the items in the cart.
3. Early Return in Admin:
The first line in the function checks if the code is running in the admin area and isn’t part of an AJAX call. If both are true, it skips this filtering logic to prevent any modifications in the WooCommerce admin.
4. Finding the FedEx Flat Rate:
The code loops through $rates to look for a shipping method labeled “FedEx Flat Rate.” If found, it stores the corresponding key ($rate_key) in $fedex_shipping_method_key and exits the loop.
5. Checking for Products with a Specific Shipping Class:
If “FedEx Flat Rate” shipping is found, the code then checks if any items in the cart have a “fedex-flat-rate” shipping class. This is done by looping through each item in $package[‘contents’], retrieving the WooCommerce product object, and checking its shipping class. If a product with this class is found, it sets $flat_rate_product to true and stops the loop.
6. Filtering Out Other Shipping Methods:
If the product with the “fedex-flat-rate” shipping class is in the cart, the code keeps only the “FedEx Flat Rate” shipping option. It loops through $rates, unsetting (removing) any other shipping methods that don’t match the FedEx Flat Rate key.
7. Returning the Modified Rates:
After filtering, the function returns the $rates array, which now contains only the FedEx Flat Rate shipping method (if applicable).
This code ensures that if a product in the cart has a specific shipping class, only the FedEx Flat Rate shipping option is shown to the customer, effectively filtering out all other options.