businessdad icon

WooCommerce - Filtering shipping states

businessdad | PRO | 05/13/16 12:28:24 PM UTC | 0 ⭐ | 913 👁️ | Never ⏰ | []
PHP |

1.63 KB

|

None

|

0 👍

/

0 👎

/**
 * Filter the available states only for shipping.
 *
 * @author Aelia <[email protected]>
 */
global $filter_shipping_states;
$filter_shipping_states = false;
 
/**
 * Filter the available states. The filter is only applied
 * if the global "filter_shipping_states" is active.
 *
 * @param array states An array of countries and states.
 * @return array An array of countries and states, with the
 * values eventually filtered.
 * @see WC_Countries::get_states()
 * @author Aelia <[email protected]>
 */
add_filter('woocommerce_states', function($states) {
    global $filter_shipping_states;
    if($filter_shipping_states === true) {
        $states['US'] = array(
            'PA' => 'Pennsylvania',
        );
    }
    return $states;
});
 
/**
 * Enables the filering of states for the shipping calculator
 * on the cart page.
 *
 * @author Aelia <[email protected]>
 */
add_action('woocommerce_before_shipping_calculator', function() {
    global $filter_shipping_states;
    $filter_shipping_states = true;
});
 
/**
 * Enables the filtering of states after the shipping calculator
 * has been rendered,on the cart page.
 *
 * @author Aelia <[email protected]>
 */
 add_action('woocommerce_after_shipping_calculator', function() {
    global $filter_shipping_states;
    $filter_shipping_states = false;
});
 
/**
 * Enables the filering of states for the shipping field on the
 * checkout page.
 *
 * @author Aelia <[email protected]>
 */
add_action('woocommerce_form_field_args', function($args, $key, $value) {
    global $filter_shipping_states;
    $filter_shipping_states = ($args['type'] === 'state') && ($key === 'shipping_state');
 
    return $args;
});

Comments