Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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
You can’t perform that action at this time.