Wednesday, 19 November 2014

Cash On Delivery with zip code in opencart


Cash On Delivery with zip code in opencart

Here i will show you very simple method about using Zip code in cash on delivery payment method. I think it is not perfect structer but may be it will be useful to any person. Just follow the below steps carefully and get the Cash On Delivery method as per available Zip Code.

Note : You can check availability on Checkout page only.

Open admin\view\template\payment\cod.tpl and add below code between table.

<tr>
    <td>Pin Code :</td>
    <td><textarea name="pincode" id="pincode" rows="10" cols="50" ><?php echo $pincode; ?></textarea></td>
</tr>

Open admin\controller\payment\cod.php and add below code between index() function.

if (isset($this->request->post['pincode'])) {
    $this->data['pincode'] = $this->request->post['pincode'];
} else {
    $this->data['pincode'] = $this->config->get('pincode');
}

Open catalog\model\payment\cod.php and follow the below step carefully.

if ($status) {
    $method_data = array(
        'code'       => 'cod',
        'title'      => $this->language->get('text_title'),
        'pincode' => $this->config->get('pincode'),    // Add this line
        'sort_order' => $this->config->get('cod_sort_order')
    );
}

Open catalog\view\theme\yourtheme\template\checkout\payment.tpl and add below code.

   // Add this code on top
 
   <?php
    $pincode = '';
    if ($this->customer->getAddressId()) {
            $address_id = $this->customer->getAddressId();
            $this->load->model('account/address');
            $address = $this->model_account_address->getAddress($address_id);
            $pincode = $address['postcode'];
         }else{
    $pincode = $this->session->data['guest']['payment']['postcode'];
         }
   ?>

  // Replace whole the foreach loop with below one.

  <?php foreach ($payment_methods as $payment_method) { ?>
<?php if($payment_method['code'] == 'cod'){ ?>
  <?php if (strpos($payment_method['pincode'],$pincode) !== false) { ?>
   
          <tr>
            <td style="width: 1px;"><?php if ($payment_method['code'] == $code || !$code) { ?>
              <?php $code = $payment_method['code']; ?>
              <input type="radio" name="payment_method" value="<?php echo $payment_method['code']; ?>" id="<?php echo $payment_method['code']; ?>" checked="checked" />
              <?php } else { ?>
              <input type="radio" name="payment_method" value="<?php echo $payment_method['code']; ?>" id="<?php echo $payment_method['code']; ?>" />
              <?php } ?></td>
            <td><label for="<?php echo $payment_method['code']; ?>"><?php echo $payment_method['title']; ?></label></td>
          </tr>
<?php } else { ?>
          <tr>
            <td style="width: 1px;">
              <input type="radio" name="payment_method" disabled="disabled" id="<?php echo $payment_method['code']; ?>" />
            <td><label style="color:red;" for="<?php echo $payment_method['code']; ?>">Cash On Delivery not available for this PostCode.</label></td>
          </tr>

        <?php } ?>
       
    <?php } else { ?>
          <tr>
            <td style="width: 1px;"><?php if ($payment_method['code'] == $code || !$code) { ?>
              <?php $code = $payment_method['code']; ?>
              <input type="radio" name="payment_method" value="<?php echo $payment_method['code']; ?>" id="<?php echo $payment_method['code']; ?>" checked="checked" />
              <?php } else { ?>
              <input type="radio" name="payment_method" value="<?php echo $payment_method['code']; ?>" id="<?php echo $payment_method['code']; ?>" />
              <?php } ?></td>
            <td><label for="<?php echo $payment_method['code']; ?>"><?php echo $payment_method['title']; ?></label></td>
          </tr>
<?php } ?>
  <?php } ?>

8 comments:

Divij said...

Hi, Thanks a lot for this tutorial. It works well. However, right now, it is pulling the pin code from billing address and not from delivery adress. I would be grateful if you could let me know how to make the script pull up pindcode from delivery address rather than pulling it from billing address.

Anany said...

In catalog\view\theme\yourtheme\template\checkout\payment.tpl
replace this

$pincode = $this->session->data['guest']['payment']['postcode'];

with this

$pincode = $this->session->data['shipping_postcode'];

to get delivery address

Anonymous said...

Thanks a lot Anany :)

Bhaskara rao.thota said...

can you provide zip based delivery availability in product page. so customer can check before add to cart. plz i am using oc 2.0.3.1.

Dhruvi Mistry said...

Hello Mr. Chandresh,

Can you please provide the same code with a separate button.So that I can use it on any page for opencart version 2.x.

Thanks in advance..:)

Unknown said...

Hi, i am not good in php. Please tell how can we call uploaded file of zipcodes in this tutorial. Thanks in Advance. Hope you help me

bindashsandeep said...

Did followed the steps correctly but unable to see the validation on checkout page. Anythings more required to change?

Unknown said...

I already using a pincode based extension in which i want to add cod payment system. You can check here: www.e-bozar.com Please help me to solve this matter.