WC_Shipping_Legacy_Local_Pickup

Local Pickup Shipping Method.

Defined (1)

The class is defined in the following location(s).

/includes/shipping/legacy-local-pickup/class-wc-shipping-legacy-local-pickup.php  
  1. class WC_Shipping_Legacy_Local_Pickup extends WC_Shipping_Method { 
  2.  
  3. /** 
  4. * Constructor. 
  5. */ 
  6. public function __construct() { 
  7. $this->id = 'legacy_local_pickup'; 
  8. $this->method_title = __( 'Local pickup (legacy)', 'woocommerce' ); 
  9. $this->method_description = '<strong>' . sprintf( __( 'This method is deprecated in 2.6.0 and will be removed in future versions - we recommend disabling it and instead setting up a new rate within your <a href="%s">Shipping zones</a>.', 'woocommerce' ), admin_url( 'admin.php?page=wc-settings&tab=shipping' ) ) . '</strong>'; 
  10. $this->init(); 
  11.  
  12. /** 
  13. * Process and redirect if disabled. 
  14. */ 
  15. public function process_admin_options() { 
  16. parent::process_admin_options(); 
  17.  
  18. if ( 'no' === $this->settings['enabled'] ) { 
  19. wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=shipping§ion=options' ) ); 
  20. exit; 
  21.  
  22. /** 
  23. * Return the name of the option in the WP DB. 
  24. * @since 2.6.0 
  25. * @return string 
  26. */ 
  27. public function get_option_key() { 
  28. return $this->plugin_id . 'local_pickup' . '_settings'; 
  29.  
  30. /** 
  31. * init function. 
  32. */ 
  33. public function init() { 
  34.  
  35. // Load the settings. 
  36. $this->init_form_fields(); 
  37. $this->init_settings(); 
  38.  
  39. // Define user set variables 
  40. $this->enabled = $this->get_option( 'enabled' ); 
  41. $this->title = $this->get_option( 'title' ); 
  42. $this->codes = $this->get_option( 'codes' ); 
  43. $this->availability = $this->get_option( 'availability' ); 
  44. $this->countries = $this->get_option( 'countries' ); 
  45.  
  46. // Actions 
  47. add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) ); 
  48.  
  49. /** 
  50. * calculate_shipping function. 
  51. */ 
  52. public function calculate_shipping( $package = array() ) { 
  53. $rate = array( 
  54. 'id' => $this->id,  
  55. 'label' => $this->title,  
  56. 'package' => $package,  
  57. ); 
  58. $this->add_rate( $rate ); 
  59.  
  60. /** 
  61. * init_form_fields function. 
  62. */ 
  63. public function init_form_fields() { 
  64. $this->form_fields = array( 
  65. 'enabled' => array( 
  66. 'title' => __( 'Enable', 'woocommerce' ),  
  67. 'type' => 'checkbox',  
  68. 'label' => __( 'Once disabled, this legacy method will no longer be available.', 'woocommerce' ),  
  69. 'default' => 'no',  
  70. ),  
  71. 'title' => array( 
  72. 'title' => __( 'Title', 'woocommerce' ),  
  73. 'type' => 'text',  
  74. 'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),  
  75. 'default' => __( 'Local pickup', 'woocommerce' ),  
  76. 'desc_tip' => true,  
  77. ),  
  78. 'codes' => array( 
  79. 'title' => __( 'Allowed ZIP/post codes', 'woocommerce' ),  
  80. 'type' => 'text',  
  81. 'desc_tip' => __( 'What ZIP/post codes are available for local pickup?', 'woocommerce' ),  
  82. 'default' => '',  
  83. 'description' => __( 'Separate codes with a comma. Accepts wildcards, e.g. <code>P*</code> will match a postcode of PE30. Also accepts a pattern, e.g. <code>NG1___</code> would match NG1 1AA but not NG10 1AA', 'woocommerce' ),  
  84. 'placeholder' => 'e.g. 12345, 56789',  
  85. ),  
  86. 'availability' => array( 
  87. 'title' => __( 'Method availability', 'woocommerce' ),  
  88. 'type' => 'select',  
  89. 'default' => 'all',  
  90. 'class' => 'availability wc-enhanced-select',  
  91. 'options' => array( 
  92. 'all' => __( 'All allowed countries', 'woocommerce' ),  
  93. 'specific' => __( 'Specific countries', 'woocommerce' ),  
  94. ),  
  95. ),  
  96. 'countries' => array( 
  97. 'title' => __( 'Specific countries', 'woocommerce' ),  
  98. 'type' => 'multiselect',  
  99. 'class' => 'wc-enhanced-select',  
  100. 'css' => 'width: 400px;',  
  101. 'default' => '',  
  102. 'options' => WC()->countries->get_shipping_countries(),  
  103. 'custom_attributes' => array( 
  104. 'data-placeholder' => __( 'Select some countries', 'woocommerce' ),  
  105. ),  
  106. ),  
  107. ); 
  108.  
  109. /** 
  110. * Get postcodes for this method. 
  111. * @return array 
  112. */ 
  113. public function get_valid_postcodes() { 
  114. $codes = array(); 
  115.  
  116. if ( '' !== $this->codes ) { 
  117. foreach ( explode( ', ', $this->codes ) as $code ) { 
  118. $codes[] = strtoupper( trim( $code ) ); 
  119.  
  120. return $codes; 
  121.  
  122. /** 
  123. * See if a given postcode matches valid postcodes. 
  124. * @param string postcode 
  125. * @param string country code 
  126. * @return boolean 
  127. */ 
  128. public function is_valid_postcode( $postcode, $country ) { 
  129. $codes = $this->get_valid_postcodes(); 
  130. $postcode = $this->clean( $postcode ); 
  131. $formatted_postcode = wc_format_postcode( $postcode, $country ); 
  132.  
  133. if ( in_array( $postcode, $codes ) || in_array( $formatted_postcode, $codes ) ) { 
  134. return true; 
  135.  
  136. // Pattern matching 
  137. foreach ( $codes as $c ) { 
  138. $pattern = '/^' . str_replace( '_', '[0-9a-zA-Z]', preg_quote( $c ) ) . '$/i'; 
  139. if ( preg_match( $pattern, $postcode ) ) { 
  140. return true; 
  141.  
  142. // Wildcard search 
  143. $wildcard_postcode = $formatted_postcode . '*'; 
  144. $postcode_length = strlen( $formatted_postcode ); 
  145.  
  146. for ( $i = 0; $i < $postcode_length; $i++ ) { 
  147. if ( in_array( $wildcard_postcode, $codes ) ) { 
  148. return true; 
  149. $wildcard_postcode = substr( $wildcard_postcode, 0, -2 ) . '*'; 
  150.  
  151. return false; 
  152.  
  153. /** 
  154. * See if the method is available. 
  155. * @param array $package 
  156. * @return bool 
  157. */ 
  158. public function is_available( $package ) { 
  159. $is_available = "yes" === $this->enabled; 
  160.  
  161. if ( $is_available && $this->get_valid_postcodes() ) { 
  162. $is_available = $this->is_valid_postcode( $package['destination']['postcode'], $package['destination']['country'] ); 
  163.  
  164. if ( $is_available ) { 
  165. if ( 'specific' === $this->availability ) { 
  166. $ship_to_countries = $this->countries; 
  167. } else { 
  168. $ship_to_countries = array_keys( WC()->countries->get_shipping_countries() ); 
  169. if ( is_array( $ship_to_countries ) && ! in_array( $package['destination']['country'], $ship_to_countries ) ) { 
  170. $is_available = false; 
  171.  
  172. return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package ); 
  173.  
  174. /** 
  175. * clean function. 
  176. * @access public 
  177. * @param mixed $code 
  178. * @return string 
  179. */ 
  180. public function clean( $code ) { 
  181. return str_replace( '-', '', sanitize_title( $code ) ) . ( strstr( $code, '*' ) ? '*' : '' );