1

I want to set shipping cost quantity wise on my woocommerce theme. I want to do this option :

For 1 to 5 products shiping cost will be 15%. More than 5 products whipping cost will be $6.99 .

Can i add this shipping option without a plugin ?

aynber
  • 22,380
  • 8
  • 50
  • 63
Shahin Ahmad
  • 11
  • 1
  • 4

3 Answers3

1

You need to hook a function to woocommerce_calculate_totals action which is triggered right before calculating the final cart total. The woocommerce_calculate_totals action provides the WC_Cart instance, on which you can perform manipulation as per your requirement.

add_action('woocommerce_calculate_totals', 'modify_shipping_totals');

function modify_shipping_totals($cart) {
    if($cart->get_cart_contents_count() < 6) {
        $cart->shipping_total = ( 15/100 ) * $this->cart_contents_total; 
        // shipping cost will be 15% of cart content total

        // you may also want to modify the shipping tax.

        $cart->shipping_tax_total = 0; 
    } else {
        $cart->shipping_total = 6.99;
        $cart->shipping_tax_total = 0;
    }


}

For further reference regarding changable variables refer to WC_Cart documentation.

Joe Beans
  • 294
  • 2
  • 4
  • Hi Pranav , I am not an advance developer . Can i make this system with table rate shipping cost plugin ? Because client want to change the fee anytime . I am facing trouble with this problem . No one can give me a right answer . Could you please help me ? – Shahin Ahmad May 07 '16 at 04:31
  • This did not worked for me. Maybe this is caused through a new version of woocommerce. – Frank Roth Nov 09 '18 at 15:00
1
// **Note**: This code is working only when you set Flat rate Settings 
// cost value is 1

add_filter( 'woocommerce_package_rates', 'custom_package_rates', 10, 2 );
 function custom_package_rates( $rates, $packages ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    $cart_count = WC()->cart->get_cart_contents_count();
    $cart_total =  WC()->cart->cart_contents_total;

    foreach($rates as $rate_key => $rate_values ) {
        $method_id = $rate_values->method_id;
        $rate_id = $rate_values->id;

        if( $method_id == 'flat_rate' ){
            if( $cart_count < 99 ){
                $flat_rate_value = 4.95; //"Applay Flat rate less then 99 quatity"
                $cart_10_percent = 0; // No percent discount
            }   
            if( $cart_count > 99 ){
                $flat_rate_value = 9.95; // "Applay Flat rate greater then 99 quatity"
                $cart_10_percent = 0; // No percent discount
            }
            $rate_cost = $flat_rate_value > $cart_10_percent ? $flat_rate_value - $cart_10_percent : 0;

            // Set the new calculated rate cost
            $rates[$rate_id]->cost = number_format( $rates[$rate_id]->cost * $rate_cost, 2 );

        }
    }
    return $rates;
} 
djs
  • 3,947
  • 3
  • 14
  • 28
Ahmed
  • 11
  • 3
0

The Pranav solution works if you call the add_action inside the init hook, like this:

function init_shop(){
    add_action('woocommerce_calculate_totals', 'modify_shipping_totals', 10);
}
add_action( 'init', 'init_shop');
lightbyte
  • 566
  • 4
  • 10