Skip to content

Instantly share code, notes, and snippets.

@bekarice
Created June 30, 2018 02:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bekarice/f7dd67a7e4d6db21f1a7fe8b0ccb6257 to your computer and use it in GitHub Desktop.
Save bekarice/f7dd67a7e4d6db21f1a7fe8b0ccb6257 to your computer and use it in GitHub Desktop.
<?php // only copy if needed!
/**
* Filters the shipping method cost to account for tiered quantity pricing.
*
* @param float $cost the shipping method cost
* @param \WC_Shipping_Rate $method the shipping method
* @return float cost
*/
function swwp_wc_shipping_cost_tiers( $cost, $method ) {
// TODO: change the numbers in this array with your desired instance IDs
// see if this shipping instance is one we want to modify cost for
if ( in_array( $method->get_instance_id(), array( 22, 23 ) ) && WC()->cart ) {
$cart_item_count = WC()->cart->get_cart_contents_count();
// if we have items that need shipping, round the quantity / 2 to the nearest whole number
// this produces tiered cost increases for every 2 items
if ( $cart_item_count > 1 ) {
$cost = round( $cart_item_count / 2 ) * $cost;
}
}
return $cost;
}
add_filter( 'woocommerce_shipping_rate_cost', 'swwp_wc_shipping_cost_tiers', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment