I’m trying to display “Free Shipping” for products in the WooCommerce cart that have a shipping cost of 0. I am using Webiwork Shipping Per Product WooCommerce plugin to add per-product shipping, and I want to show this message only for those products, while leaving products with non-zero shipping charges unchanged, like this screenshot:
I attempted to use the following code:
add_action('woocommerce_after_cart_item_name', 'display_free_shipping_for_cart_items', 20, 2);
function display_free_shipping_for_cart_items($cart_item, $cart_item_key) {
$packages = WC()->shipping->get_packages();
foreach ($packages as $package) {
if (isset($package['contents'][$cart_item_key])) {
$product = $package['contents'][$cart_item_key];
if (!empty($package['rates'])) {
$shipping_rate = reset($package['rates']);
$shipping_cost = $shipping_rate->cost;
if ($shipping_cost == 0) {
echo '<p class="product-shipping-cost">Free Shipping</p>';
}
}
}
}
}
But it seems to either show nothing or doesn’t work as expected.