Jump to content

PHP Query for master coders....any help appreciated!


Recommended Posts

Problem:Free Shipping to Delivery Address Specific To UK Post Codes (for ecommerce site)

 

Does It Work: The following code seems to work for the postcodes I manually entered. It works on registered address or a delivery address (whichever is specified at the checkout stage) however...

 

Problem- Now i dont profess to be a php coder of any sort so someone may have an answer to this problem (much appreciated if you do)- Some post codes have the same first three eg CV1 & CV11, now the code will pick up on both as CV1 and offer the free shipping option. I need to figure out how to exclude any that may be cv11 or cv12 etc. Again help much appreciated

 

  if ($check_flag == false) {
          $this->enabled = false;
        }
	 // check to see if postal code is in list of allowed codes:
      } else {
        $free_ship_postcodes = array('CV1','CV2','CV3','CV4','CV5','CV6','CV7');
        $customer_postcode = $order->delivery['postcode'];
        $check3digitcode = substr($customer_postcode, 0, 3);
        $valid_digits = in_array($check3digitcode, $free_ship_postcodes);
        if ($valid_digits) $this->enabled = true;

How many non free shipping postal codes are there?

hey cronix - thanks for taking an interest!

 

I believe I would be looking at 15 (unless I have missed one)

 

CV10, CV11, CV12, CV13, CV21, CV22, CV23, CV31, CV32, CV33, CV34, CV35, CV36, CV37, CV47

 

Appreciate your input!  :)

Maybe just rewrite what you have and put all of those values in an array, then check the first 4 characters and if it finds them, its not free.  Just the opposite of what you are doing....

 

Ahh I think i may have confused the issue, those ones that are not free only relate to the specific ones that are free where the 4th number causes an issue. The rest of the world (post codes/zips etc) are on a standard rate.

 

I didnt originate the code provided - i located it elsewhere and am trying to adapt...

<?php
$free_ship_postcodes = array('CV1','CV2','CV3','CV4','CV5','CV6','CV7');
$customer_postcode = $order->delivery['postcode'];
$check3digitcode = substr($customer_postcode, 0, 3);
$valid_digits = in_array($check3digitcode, $free_ship_postcodes);
//added from here down...first check against valid digits, then invalid if passed first validation
$invalid_digits = true;
if($valid_digits){
    $non_free_postcodes = array('CV10', 'CV11', 'CV12', 'CV13', 'CV21', 'CV22', 'CV23', 'CV31', 'CV32', 'CV33', 'CV34', 'CV35', 'CV36', 'CV37', 'CV47');
    $check4digitcode = substr($non_free_postcodes, 0, 4);
    $invalid_digits = in_array($check4digitcode, $non_free_postcodes);
}

if ($valid_digits && $invalid_digits) $this->enabled = true;

Might not be the best, but should do what you want.

This works...

<?php
$free_ship_postcodes = array('CV1','CV2','CV3','CV4','CV5','CV6','CV7');
$customer_postcode = $order->delivery['postcode'];
$check3digitcode = substr($customer_postcode, 0, 3);
$valid_digits = in_array($check3digitcode, $free_ship_postcodes);
//added from here down...first check against valid digits, then invalid if passed first validation
$invalid_digits = false;
if($valid_digits){
    $non_free_postcodes = array('CV10', 'CV11', 'CV12', 'CV13', 'CV21', 'CV22', 'CV23', 'CV31', 'CV32', 'CV33', 'CV34', 'CV35', 'CV36', 'CV37', 'CV47');
    $check4digitcode = substr($customer_postcode, 0, 4);
    $invalid_digits = in_array($check4digitcode, $non_free_postcodes);
}
if ($valid_digits && !$invalid_digits) $this->enabled = true;

in your original code, replace

 

$check3digitcode = substr($customer_postcode, 0, 3);

 

with

$check3digitcode = trim(substr($customer_postcode, 0, strlen($customer_postcode)-3)); 

 

to get the first part of the post code (3 or 4 chars)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.