WooCommerce shipping cost based on cart item count

前端 未结 3 1842
长情又很酷
长情又很酷 2021-01-23 07:01

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 t

3条回答
  •  粉色の甜心
    2021-01-23 07:17

    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;
    }
    

提交回复
热议问题