Created
March 2, 2018 19:44
-
-
Save bolderelements/fa586a845e4577a9bd9195bc05508c7a to your computer and use it in GitHub Desktop.
Add shipping surcharge based on specific equation
This file contains hidden or 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
/** | |
* Add surcharge for every item where this dimensions equation exceeds 3m | |
* Code snippets should be added to the functions.php file of your child theme | |
* | |
* @return array | |
*/ | |
function add_surcharge_for_large_items( $rates, $package ) { | |
// information used in calculations that can be edited | |
$shipping_method = 'betrs_shipping; | |
$fee = 4.99; | |
$max_dimensions = 3; | |
$total_width = $highest_length = $highest_height = 0; | |
// determine calculated dimensions for entire order | |
foreach( $package['contents'] as $key => $values ) { | |
$cart_data = $values['data']; | |
// combine all widths | |
$total_width += $cart_data->get_width(); | |
// find largest length | |
if( $cart_data->get_length() > $highest_length ) | |
$highest_length = $cart_data->get_length(); | |
// find largest height | |
if( $cart_data->get_height() > $highest_height ) | |
$highest_height = $cart_data->get_height(); | |
} | |
$calc = floatval( $highest_length ) + floatval( $total_width * 2 ) + floatval( $highest_height * 2 ); | |
if( $calc >= $max_dimensions ) | |
$extra_fee = $fee * $large_items; | |
// add fee to all options from method specified above | |
foreach( $rates as $key => $rate ) { | |
if( $rate->method_id != $shipping_method ) | |
continue; | |
$rates[ $key ]->cost += $extra_fee; | |
} | |
return $rates; | |
} | |
add_filter( 'woocommerce_package_rates', 'add_surcharge_for_large_items', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment