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 } ?>