Adding Custom Shipping to WooCommerce Checkout for Heavy Products

Adding Custom Shipping to WooCommerce Checkout for Heavy Products

When running an e-commerce store, shipping logistics can be a crucial aspect to consider, especially when dealing with heavy products. WooCommerce provides a flexible platform that allows you to customize shipping rules based on various conditions. In this blog post, we will walk you through the steps to add custom shipping to the WooCommerce checkout if a product in the cart weighs more than 1000kg.

Step 1: Add the Custom Shipping Function

First, you need to add a function that checks the weight of the products in the cart and applies a custom shipping fee if any product exceeds the specified weight limit. Place this code in your theme’s functions.php file or a custom plugin.

// Add custom shipping fee for heavy products
add_action( 'woocommerce_cart_calculate_fees', 'add_custom_shipping_for_heavy_products' );

function add_custom_shipping_for_heavy_products() {
    global $woocommerce;

    // Define the weight limit
    $weight_limit = 1000; // Weight in kg

    // Define the custom shipping fee amount
    $custom_shipping_fee = 50; // Change this to the desired fee amount

    // Check if any product in the cart exceeds the weight limit
    $heavy_product_found = false;
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $product = $cart_item['data'];
        $product_weight = $product->get_weight();

        if ( $product_weight > $weight_limit ) {
            $heavy_product_found = true;
            break;
        }
    }

    // If a heavy product is found, add the custom shipping fee
    if ( $heavy_product_found ) {
        WC()->cart->add_fee( __( 'Heavy Product Shipping', 'woocommerce' ), $custom_shipping_fee );
    }
}

Step 2: Customize the Shipping Fee and Weight Limit

In the code snippet above, you can customize the following parameters:

  • $weight_limit: Set this variable to the weight limit (in kg) for which you want to apply the custom shipping fee.
  • $custom_shipping_fee: Set this variable to the amount you want to charge as a custom shipping fee.

Step 3: Add the Custom Shipping Label Translation

To ensure the custom shipping label is translatable, wrap the label in the __() function, as shown in the code above. You can replace 'Heavy Product Shipping' with any other label that suits your needs.

Step 4: Testing the Custom Shipping Fee

After adding the code to your functions.php file or custom plugin, it’s important to test the functionality:

  1. Add a Heavy Product: Add a product that weighs more than the defined weight limit (1000kg) to the cart.
  2. Proceed to Checkout: Go to the checkout page and verify that the custom shipping fee is applied correctly.
  3. Remove the Product: Remove the heavy product from the cart and ensure that the custom shipping fee is no longer applied.

Example Use Case

Let’s consider an example use case where you sell industrial machinery, some of which weigh more than 1000kg. By implementing this custom shipping rule, you can ensure that customers purchasing these heavy items are charged an additional shipping fee to cover the extra shipping costs.

Conclusion

By following these steps, you can easily add a custom shipping fee to the WooCommerce checkout process for heavy products. This customization can help you manage additional shipping costs for specific products and improve your store’s functionality.

Feel free to modify the code to fit your specific requirements. Happy customizing!

Get A No Obligation Quote

Do You Need Help With Your WooCommerce Site?

Click through to the next page and complete the form to get a free no obligation quote to fix any issue you are having with your WooCommerce site.