-
-
Save bekarice/f7dd67a7e4d6db21f1a7fe8b0ccb6257 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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