Check Delivery Availability by Zip/Pin Code
First create a module by any name
My module name = products
Database = magento1
Table = products
Path = D:\xampp\htdocs\magento1\app\design\frontend\rwd\default\template\products\products.phtml
Code:
<div class="pincode-check">
<p class="pincode-title">Check Cash on delivery availability</p></div>
<input type="text" name="pin" id="pin-input" placeholder ="Enter your pincode" /><br>
<button id="submit-pin">Check</button>
<p id="message"></p>
</div>
<input type="text" name="pin" id="pin-input" placeholder ="Enter your pincode" /><br>
<button id="submit-pin">Check</button>
<p id="message"></p>
</div>
<!-- Ajax Block-->
<script type="text/javascript">
var pincodeValue = $j( "#pin-input" ).val();
$j( "#submit-pin" ).click(function(){
pincodeValue = $j( "#pin-input" ).val();
$j('#message').hide();
// alert(pincodeValue);
$j.ajax({
url: "<?php echo $this->getUrl('products/index/pincode') ?>",
type: "POST",
data: {
pincodeValue:pincodeValue,},
success: function(data) {
$j('#message').show();
$j('#message').html(data);
},
error: function(MLHttpRequest, textStatus, errorThrown){
alert(errorThrown);
}
});
});
</script>
<script type="text/javascript">
var pincodeValue = $j( "#pin-input" ).val();
$j( "#submit-pin" ).click(function(){
pincodeValue = $j( "#pin-input" ).val();
$j('#message').hide();
// alert(pincodeValue);
$j.ajax({
url: "<?php echo $this->getUrl('products/index/pincode') ?>",
type: "POST",
data: {
pincodeValue:pincodeValue,},
success: function(data) {
$j('#message').show();
$j('#message').html(data);
},
error: function(MLHttpRequest, textStatus, errorThrown){
alert(errorThrown);
}
});
});
</script>
Path = D:\xampp\htdocs\magento1\app\design\frontend\rwd\default\layout\products.xml
Code:
<?xml version="1.0"?>
<layout version="0.1.0">
<default>
</default>
<products_index_index>
<reference name="content">
<block type="products/products" name="products" template="products/products.phtml" />
</reference>
</products_index_index>
<catalog_product_view>
<reference name="product.info">
<block type="catalog/product_view" name="product.info.addtocart" as="addtocart" template="catalog/product/view/addtocart.phtml">
<block type="products/products" name="products" template="products/catalog/product/view/products.phtml"/>
</block>
</reference>
</catalog_product_view>
</layout>
<layout version="0.1.0">
<default>
</default>
<products_index_index>
<reference name="content">
<block type="products/products" name="products" template="products/products.phtml" />
</reference>
</products_index_index>
<catalog_product_view>
<reference name="product.info">
<block type="catalog/product_view" name="product.info.addtocart" as="addtocart" template="catalog/product/view/addtocart.phtml">
<block type="products/products" name="products" template="products/catalog/product/view/products.phtml"/>
</block>
</reference>
</catalog_product_view>
</layout>
Controller
Path =D:\xampp\htdocs\magento1\app\code\local\Bluethink\Products\controllers\IndexController.php
Code:
<?php
class Bluethink_Products_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout();
$this->renderLayout();
}
public function pincodeAction()
{
$pinCode=$_POST['pincodeValue']; // pincode entered by the user
if (!preg_match("/^\d{6}$/i",$pinCode)){
$result="Please enter a valid pincode.";
}
else{
$model = Mage::getModel('products/products')->getCollection()->addFieldToFilter('pincode', $pinCode);
// echo "<pre>";
// print_r($model->getData());
// $model->printLogQuery(true);
// exit;
if(empty($model->getData())){
$result="COD not available";
}else{
$result="COD available";
}
}
$this->getResponse()->setBody($result);
}
// $this->loadLayout();
// $this->renderLayout();
}
class Bluethink_Products_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout();
$this->renderLayout();
}
public function pincodeAction()
{
$pinCode=$_POST['pincodeValue']; // pincode entered by the user
if (!preg_match("/^\d{6}$/i",$pinCode)){
$result="Please enter a valid pincode.";
}
else{
$model = Mage::getModel('products/products')->getCollection()->addFieldToFilter('pincode', $pinCode);
// echo "<pre>";
// print_r($model->getData());
// $model->printLogQuery(true);
// exit;
if(empty($model->getData())){
$result="COD not available";
}else{
$result="COD available";
}
}
$this->getResponse()->setBody($result);
}
// $this->loadLayout();
// $this->renderLayout();
}
When you open your module by {127.0.0.1/magento/(Module name)}
My url = {http://127.0.0.1:500/magento1/products}
Create Database with pincode and add values to check as mine
shown in pic below
If you want to call in product and check there
You have to create a folder inside products in template
catalog\product\view\products.phtml (copy product.phtml there)
(make these folder inside template ) see path in pic
When you open this in magento it shows like this
When you open it in products it shows like this by overriding it there
I have it will helps if any problem comes you may comment us




Comments
Post a Comment