Last active
April 10, 2020 15:32
-
-
Save bolderelements/30de50b17afb0a8929a8ffe8084f84ea to your computer and use it in GitHub Desktop.
Add new condition to Table Rate settings page Reviewing Stock Status
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 find and compare the stock status of items in Table Rate shipping | |
* 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 theme's functions.php file | |
*/ | |
function betrs_add_stock_status_cond( $conditions ) { | |
// add new option to list | |
$conditions['stock_status'] = 'Stock Status'; | |
return $conditions; | |
} | |
add_filter( 'betrs_shipping_cost_conditionals', 'betrs_add_stock_status_cond', 10, 1 ); | |
function betrs_add_stock_status_cond_secondary( $conditions ) { | |
// add new option to list | |
$conditions['includes']['conditions'][] = 'stock_status'; | |
$conditions['excludes']['conditions'][] = 'stock_status'; | |
return $conditions; | |
} | |
add_filter( 'betrs_shipping_cost_conditionals_secondary', 'betrs_add_stock_status_cond_secondary', 10, 1 ); | |
function betrs_add_stock_status_cond_tertiary( $type, $item, $row_ID, $option_ID, $cond_key ) { | |
// add select box of available stock status options | |
$terms = wc_get_product_stock_status_options(); | |
$op_name_tertiary = ( isset( $option_ID ) ) ? "cond_tertiary[" . $option_ID . "][" . $row_ID . "]" : "method_cond_tertiary"; | |
$cond_key = abs( $cond_key ); | |
?> | |
<select name="<?php echo $op_name_tertiary; ?>[<?php echo $cond_key; ?>]" class="cond_tertiary"> | |
<?php foreach( $terms as $slug => $status ) : ?> | |
<option value="<?php echo $slug; ?>" <?php selected( $slug, $item['cond_tertiary'], true ); ?>><?php echo $status; ?></option> | |
<?php endforeach; ?> | |
</select> | |
<?php | |
} | |
add_action( 'betrs_shipping_cost_conditionals_tertiary', 'betrs_add_stock_status_cond_tertiary', 10, 5 ); | |
function betrs_add_stock_status_calc( $data, $items ) { | |
$status_ar = array(); | |
// determine longest length | |
foreach( $items as $key => $values ) { | |
$status_ar[] = $values[ 'data' ]->get_stock_status(); | |
} | |
// remove duplicated | |
$status_ar = array_unique( $status_ar ); | |
$data['stock_status'] = $status_ar; | |
return $data; | |
} | |
add_filter( 'betrs_calculated_totals-per_order', 'betrs_add_stock_status_calc', 10, 2 ); | |
function betrs_determine_condition_stock_status( $return, $cond, $cart_data ) { | |
// exit if the custom condition is not a stock status | |
if( sanitize_title( $cond['cond_type'] ) != 'stock_status' ) return $return; | |
// calculate longest length and init comparison numbers | |
$comparison = (array) $cart_data['stock_status']; | |
$cond_tertiary = sanitize_title( $cond['cond_tertiary'] ); | |
// determine how to compare the sum to the setting | |
switch( sanitize_text_field( $cond['cond_secondary'] ) ) { | |
case 'includes': | |
if( in_array( $cond_tertiary, $comparison ) ) | |
return true; | |
break; | |
case 'excludes': | |
if( ! in_array( $cond_tertiary, $comparison ) ) | |
return true; | |
break; | |
} | |
// return false if the condition is not met | |
return $return; | |
} | |
add_filter( 'betrs_determine_condition_result', 'betrs_determine_condition_stock_status', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment