Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add a fixed amount with shipping cost if total number of item in the cart reach limit.
add_filter('woocommerce_package_rates', 'wf_modify_rate', 10, 3);
function wf_modify_rate( $available_shipping_methods, $package ){
$item_count_limit = 10; //if total item number exceeds 10, the below amount will get added with every shipping methods
$amount_to_add_domestic = 10;
$amount_to_add_international = 20;
$origin_country = 'US';
$amount_to_add = ($package['destination']['country'] == $origin_country) ? $amount_to_add_domestic : $amount_to_add_international;
$item_count = 0;
foreach ($package['contents'] as $key => $item) {
$item_count += $item['quantity'];
}
if( $item_count > $item_count_limit ){
foreach ($available_shipping_methods as $methord_name => $methord) {
$available_shipping_methods[$methord_name]->cost += $amount_to_add;
}
}
return $available_shipping_methods;
}
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.