Created
June 23, 2021 20:19
-
-
Save bolderelements/4d9239e2d8f5bc721e58abfd5c0f66bd to your computer and use it in GitHub Desktop.
Add a new condition to the Table Rate method that checks quantity of specific shipping class
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
/** | |
* Add ability to check for quantity 3 of a specific shipping class in Table Rate method | |
* Intended for Use with Per Order setups and Method Conditions | |
* https://codecanyon.net/item/table-rate-shipping-for-woocommerce/3796656?ref=bolderelements | |
* | |
* Code should be added to a child theme's functions.php file | |
*/ | |
function betrs_add_class_qty_cond( $conditions ) { | |
// add new option to list | |
$conditions['qty_shipping_class'] = '3 of Shipping Class'; | |
return $conditions; | |
} | |
add_filter( 'betrs_shipping_cost_conditionals', 'betrs_add_class_qty_cond', 10, 1 ); | |
function betrs_add_class_qty_cond_secondary( $type, $item, $option_ID, $row_ID ) { | |
// exit if the custom condition is not a stock status | |
if( sanitize_title( $type ) != 'qty_shipping_class' ) return $return; | |
// setup table rate form fields | |
$option_ID = (int) $option_ID; | |
$row_ID = (int) $row_ID; | |
$op_name_secondary = "cond_secondary[" . $option_ID . "][" . $row_ID . "][]"; | |
$op_name_tertiary = "cond_tertiary[" . $option_ID . "][" . $row_ID . "][]"; | |
$cond_secondary = ( isset( $item['cond_secondary'] ) ) ? sanitize_text_field( $item['cond_secondary'] ) : ''; | |
$terms = WC()->shipping->get_shipping_classes(); | |
?> | |
<select name="<?php echo $op_name_secondary; ?>" class="cond_secondary"> | |
<?php | |
foreach( $terms as $term ) : | |
if( isset( $term->term_id ) ) : | |
?> | |
<option value="<?php echo $term->term_id; ?>" <?php selected( $term->term_id, $cond_secondary, true ); ?>><?php echo $term->name; ?></option> | |
<?php | |
endif; | |
endforeach; | |
?> | |
<option value="0" <?php selected( 0, $cond_secondary, true ); ?>>No Class Selected</option> | |
</select> | |
<input type="hidden" name="<?php echo $op_name_tertiary; ?>[]" size="8" /> | |
<?php | |
} | |
add_action( 'betrs_shipping_cost_conditionals_after', 'betrs_add_class_qty_cond_secondary', 10, 4 ); | |
function betrs_add_class_qty_calc( $data, $items ) { | |
$class_counts = array(); | |
// count all shipping classes in cart | |
foreach( $items as $key => $values ) { | |
$class_id = (int) $values[ 'data' ]->get_shipping_class_id(); | |
if( ! isset( $class_counts[ $class_id ] ) ) { | |
$class_counts[ $class_id ] = $values['quantity']; | |
} else { | |
$class_counts[ $class_id ] += $values['quantity']; | |
} | |
} | |
// remove duplicated | |
$data['qty_shipping_class'] = $class_counts; | |
return $data; | |
} | |
add_filter( 'betrs_calculated_totals-per_order', 'betrs_add_class_qty_calc', 10, 2 ); | |
function betrs_determine_condition_qty_calc( $return, $cond, $cart_data ) { | |
// exit if the custom condition is not a class quantity | |
if( sanitize_title( $cond['cond_type'] ) != 'qty_shipping_class' ) return $return; | |
// get class variables for comparison | |
$cart_classes = (array) $cart_data['qty_shipping_class']; | |
$cond_class = intval( $cond['cond_secondary'] ); | |
// determine if the quantity of 3 is met | |
if( isset( $cart_classes[ $cond_class ] ) ) { | |
if( intval( $cart_classes[ $cond_class ] ) > 2 ) { | |
return true; | |
} | |
} | |
// return false if the condition is not met | |
return $return; | |
} | |
add_filter( 'betrs_determine_condition_result', 'betrs_determine_condition_qty_calc', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment