0

please visit link

you can see there is an option to check delivery is available or not by entering zip code.

enter image description here

we saved list of zip codes in database table and if zip code is available in table, it show

"Delivery is avilable" otherwise it show " Not available".

example: 560043 is avialable. 5600431 is not available

I want to update this feature with following requirements.

for some zip codes, i want to display message as

1) Delivery is available, COD is available

for some other zip codes, i want to display message as

2) Delivery is available, COD is not available

Also for some,

3) Delivery is not available.

is this possible by saving zip codes in DB or is we have to save zip codes in files to get

this result ?

view.phtml

<?php  echo $this->getLayout()->createBlock('core/template')->setTemplate('checkdelivery/checkdelivery.phtml')->toHtml();?>

checkdelivery.phtml

<div class="block-content" >    <div id="block_info">
<img src ="" height= '35' width= '70'> 
<?php $blockLabel = Mage::getStoreConfig('checkdelivery/general/block_title'); ?>
<strong><span><?php echo $this->__($blockLabel) ?></span></strong>


<input type="text" placeholder="Enter your PIN Code" class="input-text" maxlength="120" id="zipcode" size="20" name="zipcode" onkeypress="if (event.keyCode == 13) { return false;}">


<button type="button" name="zip-check" title="Check" class="button1" id="zip-check" ><span><?php echo $this->__('CHECK'); ?></span></button>
<div id="delivery-message"></div>
<?php $defaultHtml = Mage::getStoreConfig('checkdelivery/general/default_html'); ?>
<div id="delivery-html"><?php echo $defaultHtml ?></div>

</div>  
</div>


<script>
    document.getElementById('zipcode').onkeypress = function(e){
        if (!e) e = window.event;
        var keyCode = e.keyCode || e.which;
        if (keyCode == '13'){
          // Do your ajax process which you are doing on zip-check click button same way.

              new Ajax.Request("<?php echo $this->getUrl('checkdelivery/index/index') ?>", {
                    method: "get",
                    parameters: {zipcode : $('zipcode').value },
                    onSuccess: function(transport) {
                             var json = transport.responseText.evalJSON();
                             $('delivery-message').update(json.message);                             
                             $('delivery-message').setStyle({ color: json.color});
                             $('delivery-html').update(json.html); 
                    }
            });

          return false;
        }
      }
</script>


<script>
    Event.observe('zip-check', 'click', function(event){
        new Ajax.Request("<?php echo $this->getUrl('checkdelivery/index/index') ?>", {
            method: "get",
            parameters: {zipcode : $('zipcode').value },
            onSuccess: function(transport) {
                 var json = transport.responseText.evalJSON();
                 $('delivery-message').update(json.message);                 
                 $('delivery-message').setStyle({ color: json.color});
                 $('delivery-html').update(json.html);  
            }
        });
    });
</script>

controllers/indexcontroller.php

public function indexAction() {

        $zip = $this->getRequest()->getParam('zipcode');
        $pincodearray = array();
        $pindata = Mage::getStoreConfig('checkdelivery/general/pincode');
        $pincodearray = explode(",", $pindata);
        $success = Mage::getStoreConfig('checkdelivery/general/success_messgae');
        $failure = Mage::getStoreConfig('checkdelivery/general/failure_messgae');
        $empty = Mage::getStoreConfig('checkdelivery/general/empty_messgae');
        $successHtml = Mage::getStoreConfig('checkdelivery/general/success_html');
        $failureHtml = Mage::getStoreConfig('checkdelivery/general/failure_html');
        $defaultHtml = Mage::getStoreConfig('checkdelivery/general/default_html');

        $trimedZip = trim($zip);
        $response = array();
        if(isset($trimedZip) && !empty($trimedZip)){
            if (in_array($trimedZip, $pincodearray)) {
                $response['message'] = $success;
                $response['color'] = 'green';
                $response['html'] = $successHtml;

            }else{
                $response['message'] = $failure;
                $response['color'] = 'red';
                $response['html'] = $failureHtml;

            }
        }else{
            $response['message'] = $empty;
            $response['color'] = 'orange';
            $response['html'] = $defaultHtml;
        }
        echo json_encode($response);exit;
    }
  • paste this checkdelivery/index/index code too – Qaisar Satti Mar 14 '16 at 7:54
  • just return message based on zipcode – Minesh Patel Mar 14 '16 at 8:38
  • @QaisarSatti please check updated question. – Baby in Magento Mar 14 '16 at 9:09
  • @MineshPatel we stored zip codes in database, can you explain with code. – Baby in Magento Mar 14 '16 at 9:10
  • what is difference between normal zip code and some zipcode you want to show the different message? – Qaisar Satti Mar 14 '16 at 9:16
2

In your checkdelivery/index/index action use below code

public function indexAction() {

    $zip = $this->getRequest()->getParam('zipcode');
    $pincodearray = array();
    $pindata = Mage::getStoreConfig('checkdelivery/general/pincode');
    $pincodearray = explode(",", $pindata);
    $success = Mage::getStoreConfig('checkdelivery/general/success_messgae');
    $failure = Mage::getStoreConfig('checkdelivery/general/failure_messgae');
    $empty = Mage::getStoreConfig('checkdelivery/general/empty_messgae');
    $successHtml = Mage::getStoreConfig('checkdelivery/general/success_html');
    $failureHtml = Mage::getStoreConfig('checkdelivery/general/failure_html');
    $defaultHtml = Mage::getStoreConfig('checkdelivery/general/default_html');

    //in below array add zipcode which have cod is available. I have add 12345,67890 for example purpose
    $pincodeCODarray = array(12345,67890,....);


    $trimedZip = trim($zip);
    $response = array();
    if(isset($trimedZip) && !empty($trimedZip)){
        if (in_array($trimedZip, $pincodearray)) {
            if (in_array($trimedZip, $pincodeCODarray)) {
              //Delivery is available, COD is available
                $response['message'] = $success;
                $response['color'] = 'green';
                $response['html'] = $successHtml;

            }else{
            //Delivery is available, COD is not available
                $response['message'] = "Delivery is available, COD is not available";
                $response['color'] = 'green';
                $response['html'] = $successHtml;

            }
        }else{
        //Delivery not available
            $response['message'] = "Delivery not available";
            $response['color'] = 'red';
            $response['html'] = $failureHtml;

        }
    }
    echo json_encode($response);exit;
}
  • i placed your code in controllers/Indexcontroller.php by replacing this line by this line. $model = Mage::getModel('your zipcode model'); by $model = Mage::getModel('checkdelivery'); but it didt worked for me. – Baby in Magento Mar 14 '16 at 9:17
  • how to find our zipcode model ? – Baby in Magento Mar 14 '16 at 9:20
  • This is model which you have created and store available code if you don't have any model regarding it. you can create an array for it. updated answer check it. – Prashant Valanda Mar 14 '16 at 9:21
  • It is working in your site.I have just checked it. – Prashant Valanda Mar 14 '16 at 10:32
  • i hide your code, so it was working. now i used our code, please check. – Baby in Magento Mar 14 '16 at 10:36
1

Here is logic for your requirements

$codAvailabelZipCode = array('123456','678954'); // get it from another config or table

if(isset($trimedZip) && !empty($trimedZip)) {
    if (in_array($trimedZip, $pincodearray) && in_array($trimedZip,$codAvailabelZipCode)) {
        // 1) Delivery is available, COD is available

    }elseif (in_array($trimedZip, $pincodearray)) {
        //2) Delivery is available, COD is not available

    }else{
        // 3) Delivery is not available.

    }
}
  • than i have to store zip codes in another table? – Baby in Magento Mar 14 '16 at 9:37
  • You can store in another StoreConfig as well – Minesh Patel Mar 14 '16 at 9:38
  • but in 2 fields one with Delivery Available and one with COD Available – Minesh Patel Mar 14 '16 at 9:39
  • i' will try with different store now. – Baby in Magento Mar 14 '16 at 9:41
  • 2
    Don't expect ready made code every time from community. – Minesh Patel Mar 14 '16 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.