• I have added this code snippet to enable shipping rates per line item in the cart:

    add_filter( ‘woocommerce_package_rates’, ‘shipping_cost_based_on_number_of_items’, 10, 2 );
    function shipping_cost_based_on_number_of_items( $rates, $package ) {
    $numer_of_items = (int) sizeof($package[‘contents’]);

    // Loop through shipping rates
    foreach ( $rates as $rate_key => $rate ){
    // Targetting “Flat rate” shipping method
    if( ‘flat_rate’ === $rate->method_id ) {
    $has_taxes = false;

    // Set the new cost
    $rates[$rate_key]->cost = $rate->cost * $numer_of_items;

    // Taxes rate cost (if enabled)
    foreach ($rates[$rate_key]->taxes as $key => $tax){
    if( $tax > 0 ){
    // New tax calculated cost
    $taxes[$key] = $tax * $numer_of_items;
    $has_taxes = true;
    }
    }
    // Set new taxes cost
    if( $has_taxes )
    $rates[$rate_key]->taxes = $taxes;
    }
    }
    return $rates;
    }

    However, along with this, I also want to enable free shipping for products with shipping class in cart. With this code in place I am unable to do that. What would be the code snippet to enable it and where should I add it in this code? I am using the latest version of Storefront theme.

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Code Snippet For Enabling Shipping Class’ is closed to new replies.