WooCommerce shipping cost based on cart item count

2019-08-19 01:23发布

I want to count shipping cost based on number of products add on cart like,

If I purchase one mobile then it will count shipping cost as 2.5 and after more than two or two mobile I purchased then shipping cost will be 5.0

 <?php

      $qty(1) * 2.5 = 2.5

      $qty(2) * 2.5 = 5.0

      $qty(3) * 2.5 = 5.0

  ?>

So is there any idea or suggestion how to count the shipping cost based on number of products ?

3条回答
forever°为你锁心
2楼-- · 2019-08-19 02:05

You can add a custom fee: Add to theme functions.php or use a plugin

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );

function woocommerce_custom_surcharge() {
  global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $price_per_mobile = 2.5;
    $shipcharge = ( $woocommerce->cart->cart_contents_total * $price_per_mobile);   
    $woocommerce->cart->add_fee( 'Total Shipping Cost', $shipcharge, true, '' );

}
查看更多
三岁会撩人
3楼-- · 2019-08-19 02:07

Go ahead with Woocommerce filter woocommerce_package_rates. Where you can customize all the shipping rates available in cart page.

Here is the code for adding extra cost to all items for both domestic and international shipement

add_filter('woocommerce_package_rates', 'wf_modify_rate', 10, 3);
function wf_modify_rate( $available_shipping_methods, $package ){
    $origin_country = 'US';
    $amount_to_add_domestic = 10;
    $amount_to_add_inter_national = 20;

    $amount_to_add = ($package['destination']['country'] == $origin_country) ? 
    $amount_to_add_domestic : $amount_to_add_inter_national;

    $item_count = 0;
    foreach ($package['contents'] as $key => $item) {
        $item_count += $item['quantity'];
    }

    foreach ($available_shipping_methods as $methord_name => $methord) {
        $available_shipping_methods[$methord_name]->cost += ($amount_to_add*$item_count);
    }
    return $available_shipping_methods;
}
查看更多
甜甜的少女心
4楼-- · 2019-08-19 02:18

Updated:

As your question is a bit unclear, you could just need to add [qty]*2.5 in the Flat rate shipping method cost (for each shipping zone) in your wooCommerce shipping settings.

But it will not work if you have 2 different items in cart like: item1 (qty 1) + item2 (qty 1)

So this answer will do it in all cases:

1) First you will need to set a "Flat rate" shipping method for each Shipping Zones which cost will be set to 2.5 (in your WooCommerce shipping settings).

2) Adding this code that will calculate for each cart items (based on the total quantity of items) the new updated shipping cost:

add_filter( 'woocommerce_package_rates', 'custom_flat_rate_cost_calculation', 10, 2 );
function custom_flat_rate_cost_calculation( $rates, $package )
{
    // The cart count (total items in cart)
    $cart_count = WC()->cart->get_cart_contents_count();
    $taxes = array();

    // If there is more than 1 cart item
    if( $cart_count > 1 ){
        // Iterating through each shipping rate
        foreach($rates as $rate_key => $rate_values){
            // Targeting "Flat Rate" shipping method
            if ( 'flat_rate' === $rate_values->method_id ) {
                // Set the new calculated rate cost
                $rates[$rate_id]->cost = number_format($rates[$rate_id]->cost * $cumulated_active_quantity, 2);
                // Taxes rate cost (if enabled)
                foreach ($rates[$rate_id]->taxes as $key => $tax){
                    if( $rates[$rate_id]->taxes[$key] > 0 ){ // set the new tax cost
                        $taxes[$key] = number_format( $rates[$rate_id]->taxes[$key] * $cumulated_active_quantity, 2 );
                        $has_taxes = true;
                    } else {
                        $has_taxes = false;
                    }
                }
                if( $has_taxes )
                    $rates[$rate_id]->taxes = $taxes; 
            }
        }
    }
    return $rates;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested on WooCommerce 3+ and works

You will need to refresh shipping zones caches: disabling the Flat rate, then save. And enabling back this rate and save.

查看更多
登录 后发表回答