• Need to arrive cart weight and based on the shipping slab rate, cart shipping charge to be determined.

    My cart – 1) Coir – 5.05Kg 2) Vermicompost – 10.05kg = Total cart weight = 15.10 kg

    Shipping slab: Upto 2 kg = Rs. 45; For every additional 1 kg upto 5Kg =Rs.12/kg; For every additional 1 kg above 5 Kg = Rs. 14/Kg

    How to arrive formula. I use default woocommerce shipping not installed any other plugin. pls guide. i have enabled shipping class 0-5 kg for coir and 10kg for vermi.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator James Huff

    (@macmanx)

    I recommend getting in touch with WooCommerce’s support about this via https://woocommerce.com/my-account/contact-support/?if you have any of their paid WooCommerce products or https://www.ads-software.com/support/plugin/woocommerce/?if you do not.

    Amit Dholiya

    (@amitdholiya1990)

    Adding Custom Shipping Calculation via Functions.php

    You can add a function in your theme’s functions.php file to calculate the shipping cost based on the weight slabs you’ve provided. This approach will add the shipping fee dynamically based on the total weight of the cart.

    // Custom shipping calculation based on weight
    function custom_shipping_cost_based_on_weight($cart) {
    // Ensure that the cart has items in it
    if (is_admin() && !defined('DOING_AJAX')) return;

    // Get total cart weight
    $total_weight = $cart->get_cart_contents_weight();

    // Initialize shipping cost
    $shipping_cost = 0;

    // Shipping logic based on weight
    if ($total_weight <= 2) {
    // Flat rate for up to 2 kg
    $shipping_cost = 45;
    } elseif ($total_weight <= 5) {
    // Rate for 2-5 kg
    $shipping_cost = 45 + ($total_weight - 2) * 12;
    } else {
    // Rate for above 5 kg
    $shipping_cost = 45 + 3 * 12 + ($total_weight - 5) * 14;
    }

    // Add the calculated shipping cost as a fee
    $cart->add_fee('Shipping Charge', $shipping_cost);
    }

    // Hook into WooCommerce to apply the custom shipping fee during cart calculation
    add_action('woocommerce_cart_calculate_fees', 'custom_shipping_cost_based_on_weight');

    Always backup your functions.php file before making changes, and test in a staging environment if possible to avoid any issues on your live site.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.