Skip to content

Instantly share code, notes, and snippets.

@xadapter xadapter/functions.php

Last active Mar 11, 2020
Embed
What would you like to do?
Snippet to add a fixed amount to the shipping cost if total number of item in the cart reach a pre-defined limit. Supports PluginHive Shipping Plugins: https://www.pluginhive.com/product-category/woocommerce-plugin/woocommerce-shipping/
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.