Advertisement
businessdad

WooCommerce - Filtering shipping states

May 13th, 2016
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.63 KB | None | 0 0
  1. /**
  2.  * Filter the available states only for shipping.
  3.  *
  4.  * @author Aelia <support@aelia.co>
  5.  */
  6. global $filter_shipping_states;
  7. $filter_shipping_states = false;
  8.  
  9. /**
  10.  * Filter the available states. The filter is only applied
  11.  * if the global "filter_shipping_states" is active.
  12.  *
  13.  * @param array states An array of countries and states.
  14.  * @return array An array of countries and states, with the
  15.  * values eventually filtered.
  16.  * @see WC_Countries::get_states()
  17.  * @author Aelia <support@aelia.co>
  18.  */
  19. add_filter('woocommerce_states', function($states) {
  20.     global $filter_shipping_states;
  21.     if($filter_shipping_states === true) {
  22.         $states['US'] = array(
  23.             'PA' => 'Pennsylvania',
  24.         );
  25.     }
  26.     return $states;
  27. });
  28.  
  29. /**
  30.  * Enables the filering of states for the shipping calculator
  31.  * on the cart page.
  32.  *
  33.  * @author Aelia <support@aelia.co>
  34.  */
  35. add_action('woocommerce_before_shipping_calculator', function() {
  36.     global $filter_shipping_states;
  37.     $filter_shipping_states = true;
  38. });
  39.  
  40. /**
  41.  * Enables the filtering of states after the shipping calculator
  42.  * has been rendered,on the cart page.
  43.  *
  44.  * @author Aelia <support@aelia.co>
  45.  */
  46.  add_action('woocommerce_after_shipping_calculator', function() {
  47.     global $filter_shipping_states;
  48.     $filter_shipping_states = false;
  49. });
  50.  
  51. /**
  52.  * Enables the filering of states for the shipping field on the
  53.  * checkout page.
  54.  *
  55.  * @author Aelia <support@aelia.co>
  56.  */
  57. add_action('woocommerce_form_field_args', function($args, $key, $value) {
  58.     global $filter_shipping_states;
  59.     $filter_shipping_states = ($args['type'] === 'state') && ($key === 'shipping_state');
  60.  
  61.     return $args;
  62. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement