Skip to content
This repository
Browse code

Added wildcard support to local delivery postcodes. Closes #1477.

  • Loading branch information...
commit 1d95fef681421252bb464805912a91abab4243f1 1 parent 8c65efe
Mike Jolley authored September 06, 2012
40  classes/shipping/class-wc-local-delivery.php
@@ -125,7 +125,7 @@ function init_form_fields() {
125 125
 			'codes' => array(
126 126
 				'title' 		=> __( 'Zip/Post Codes', 'woocommerce' ),
127 127
 				'type' 			=> 'textarea',
128  
-				'description' 	=> __( 'What zip/post codes would you like to offer delivery to? Separate codes with a comma.', 'woocommerce' ),
  128
+				'description' 	=> __( 'What zip/post codes would you like to offer delivery to? Separate codes with a comma. Accepts wildcards, e.g. P* will match a postcode of PE30.', 'woocommerce' ),
129 129
 				'default'		=> ''
130 130
 			),
131 131
 			'availability' => array(
@@ -179,15 +179,37 @@ function is_available( $package ) {
179 179
 
180 180
 		// If post codes are listed, let's use them.
181 181
 		$codes = '';
182  
-		if($this->codes != '') {
183  
-			foreach(explode(',',$this->codes) as $code) {
184  
-				$codes[] = $this->clean($code);
  182
+		if ( $this->codes != '' ) {
  183
+			foreach( explode( ',', $this->codes ) as $code ) {
  184
+				$codes[] = $this->clean( $code );
185 185
 			}
186 186
 		}
187  
-
188  
-		if (is_array($codes))
189  
-			if ( ! in_array($this->clean( $package['destination']['postcode'] ), $codes))
  187
+		
  188
+		if ( is_array( $codes ) ) {
  189
+			
  190
+			$found_match = false;
  191
+			
  192
+			if ( in_array( $this->clean( $package['destination']['postcode'] ), $codes ) )
  193
+				$found_match = true;
  194
+			
  195
+			// Wildcard search
  196
+			if ( ! $found_match ) {
  197
+				
  198
+				$customer_postcode = $this->clean( $package['destination']['postcode'] );
  199
+				$customer_postcode_length = strlen( $customer_postcode );
  200
+				
  201
+				for ( $i = 0; $i <= $customer_postcode_length; $i++ ) {
  202
+					
  203
+					if ( in_array( $customer_postcode, $codes ) ) 
  204
+						$found_match = true;
  205
+					
  206
+					$customer_postcode = substr( $customer_postcode, 0, -2 ) . '*';
  207
+				}
  208
+			}
  209
+			
  210
+			if ( ! $found_match )
190 211
 				return false;
  212
+		}
191 213
 
192 214
 		// Either post codes not setup, or post codes are in array... so lefts check countries for backwards compatability.
193 215
 		$ship_to_countries = '';
@@ -215,8 +237,8 @@ function is_available( $package ) {
215 237
      * @param mixed $code
216 238
      * @return string
217 239
      */
218  
-    function clean($code) {
219  
-    	return str_replace('-','',sanitize_title($code));
  240
+    function clean( $code ) {
  241
+    	return str_replace( '-', '', sanitize_title( $code ) ) . ( strstr( $code, '*' ) ? '*' : '' );
220 242
     }
221 243
 
222 244
 }
1  readme.txt
@@ -154,6 +154,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc
154 154
 * Feature - Added sales by category report
155 155
 * Feature - Multiple downloadable files per product/variation (kudos Justin Stern)
156 156
 * Feature - Download expirey for variations (kudos niravmehta)
  157
+* Feature - Added wildcard support to local delivery postcodes
157 158
 * Templating - email-order-items.php change get_downloadable_file_url() to get_downloadable_file_urls() to support multiple files
158 159
 * Tweak - If a PayPal prefix is changed, IPN requests break for all existing orders - fixed. new woocommerce_get_order_id_by_order_key() function added. Thanks Brent.
159 160
 * Tweak - On add to cart success, redirect back.

0 notes on commit 1d95fef

Please sign in to comment.
Something went wrong with that request. Please try again.