Last active
October 15, 2019 21:11
-
-
Save bolderelements/e0c61f43998fcda90f3c7cba427bc8ba to your computer and use it in GitHub Desktop.
Add Longest Length Condition's Calculation for Per Class Setups
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
function betrs_add_longest_length_calc_class( $data, $items ) { | |
$item_lengths = array(); | |
// cycle through cart items | |
foreach( $items as $item_ar ) { | |
// only count the ones that apply to shipping | |
if( isset( $item_ar['data'] ) && $item_ar['data']->needs_shipping() ) { | |
// initialize necessary variables | |
$item = $item_ar['data']; | |
$shipping_class_id = apply_filters( 'betrs_settings_shipping_class', $item->get_shipping_class_id() ); | |
$item_length = $item->get_length(); | |
if( ! isset( $item_lengths[ $shipping_class_id ] ) || | |
( isset( $item_lengths[ $shipping_class_id ] ) && $item_length > $item_lengths[ $shipping_class_id ] ) ) { | |
$item_lengths[ $shipping_class_id ] = $item_length; | |
} | |
} | |
} | |
// add highest length to data array | |
foreach( $item_lengths as $s_class => $length ) { | |
$data[ $s_class ]['longest_length'] = $length; | |
} | |
return $data; | |
} | |
add_filter( 'betrs_calculated_totals-per_class', 'betrs_add_longest_length_calc_class', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment