Last active
December 4, 2017 04:08
-
-
Save bolderelements/eb35313fff97f9403b78160f1ae776e7 to your computer and use it in GitHub Desktop.
Add new condition to Table Rate settings page
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 ability to compare the sum of all dimensions to Table Rate shipping | |
* https://codecanyon.net/item/table-rate-shipping-for-woocommerce/3796656?ref=bolderelements | |
* | |
* Code should be added to theme's functions.php file | |
*/ | |
function betrs_add_dimensions_sum_cond( $conditions ) { | |
// add new option to list | |
$conditions['dimensions_sum'] = 'Dimensions Sum'; | |
return $conditions; | |
} | |
add_filter( 'betrs_shipping_cost_conditionals', 'betrs_add_dimensions_sum_cond', 10, 1 ); | |
function betrs_add_dimensions_sum_cond_secondary( $conditions ) { | |
// add new option to list | |
$conditions['greater_than']['conditions'][] = 'dimensions_sum'; | |
$conditions['less_than']['conditions'][] = 'dimensions_sum'; | |
$conditions['equal_to']['conditions'][] = 'dimensions_sum'; | |
return $conditions; | |
} | |
add_filter( 'betrs_shipping_cost_conditionals_secondary', 'betrs_add_dimensions_sum_cond_secondary', 10, 1 ); | |
function betrs_determine_condition_sum( $return, $cond, $cart_data ) { | |
// exit if the custom condition is not a dimensional sum | |
if( sanitize_title( $cond['cond_type'] ) != 'dimensions_sum' ) return $return; | |
// calculate dimensional sum and init comparison numbers | |
$comparison = floatval( $cart_data['height'] ) + floatval( $cart_data['width'] ) + floatval( $cart_data['length'] ); | |
$cond_tertiary = floatval( $cond['cond_tertiary'] ); | |
// determine how to compare the sum to the setting | |
switch( sanitize_text_field( $cond['cond_secondary'] ) ) { | |
case 'greater_than': | |
if( $comparison >= $cond_tertiary ) | |
return true; | |
break; | |
case 'less_than': | |
if( $comparison <= $cond_tertiary ) | |
return true; | |
break; | |
case 'equal_to': | |
if( $comparison == $cond_tertiary ) | |
return true; | |
break; | |
} | |
// return false if the condition is not met | |
return $return; | |
} | |
add_filter( 'betrs_determine_condition_result', 'betrs_determine_condition_sum', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment