Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bolderelements/e0c61f43998fcda90f3c7cba427bc8ba to your computer and use it in GitHub Desktop.
Save bolderelements/e0c61f43998fcda90f3c7cba427bc8ba to your computer and use it in GitHub Desktop.
Add Longest Length Condition's Calculation for Per Class Setups
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