Skip to content

Instantly share code, notes, and snippets.

@aliboy08
Created October 5, 2018 02:35
Show Gist options
  • Save aliboy08/4711521316034b7079a265291804a872 to your computer and use it in GitHub Desktop.
Save aliboy08/4711521316034b7079a265291804a872 to your computer and use it in GitHub Desktop.
Woocommerce - modify shipping cost depending on quantity
// Free retail shipping
add_filter('woocommerce_package_rates','ff_override_flat_rate_price',100,2);
function ff_override_flat_rate_price($rates,$package) {
// Modify shipping cost for Books depending on quantity
if( ff_get_cart_total_quantity() >= 2 && ff_get_cart_total_quantity() <= 3 ) {
// 2 or 3 = 15.95
foreach ($rates as $rate) {
//Set the price
$rate->cost = 15.95;
//Set the TAX
$rate->taxes[1] = 0;
}
} else if ( ff_get_cart_total_quantity() >= 4 ) {
// 4 or more = 19.95
foreach ($rates as $rate) {
//Set the price
$rate->cost = 19.95;
//Set the TAX
$rate->taxes[1] = 0;
}
}
return $rates;
}
function ff_get_cart_total_quantity(){
global $woocommerce;
$quantity = 0;
foreach( $woocommerce->cart->cart_contents as $p ) {
if( $p['data']->virtual != 'yes' ) {
$quantity += $p['quantity'];
}
}
return $quantity;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment