0

we are using custom module for checking COD is available or not in view page.

we entered zip code and checked "COD is available or not", now if we click

on other product page, it should remember the zip code that we entered last time & should display message by default.

enter image description here

checkdelivery.phtml

<div class="block block-list block-check-delivery">
    <div class="block-title">
        <?php $blockLabel = Mage::getStoreConfig('checkdelivery/general/block_title'); ?>
        <strong><span><?php echo $this->__($blockLabel) ?></span></strong>
    </div>
    <div class="block-content" >        
        <br>
            <input name="zipcode" size="17" type="text" id="zipcode" maxlength="10" class="input-text" placeholder="<?php echo $this->__('Enter ZIP Code'); ?>"/>
            <button type="button" name="zip-check" title="Check" class="button" 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>

        <br>        
    </div>
</div>

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

view.phtml

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

indexcontroller.php

<?php
class NextBits_CheckDelivery_IndexController extends Mage_Core_Controller_Front_Action
{
    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;
    }
}
  • show your checkdelivery/index controller – Asish Hira Apr 25 '16 at 11:12
  • are you asking for indexcontroller.php file ? – Baby in Magento Apr 25 '16 at 11:14
  • Yes!!!!!!!!!!!! – Asish Hira Apr 25 '16 at 11:14
  • we are using this module – Baby in Magento Apr 25 '16 at 11:14
  • updated code in question, please check it. – Baby in Magento Apr 25 '16 at 11:15
1

Just replace your checkdelivery.phtml with below code

<style>
.block-check-delivery .block-title strong {background-image:none; font-size: 11px;}
.block-check-delivery .block-content {padding:0 10px;  }
.block-check-delivery .button {float: right;}
.block-check-delivery input{width: 107px;}
</style>
<div class="block block-list block-check-delivery">
    <div class="block-title">
        <?php $blockLabel = Mage::getStoreConfig('checkdelivery/general/block_title'); ?>
        <strong><span><?php echo $this->__($blockLabel) ?></span></strong>
    </div>
    <div class="block-content" >        
        <br>
            <input name="zipcode" size="17" type="text" id="zipcode" value="<?php echo Mage::getModel('core/cookie')->get('zip'); ?>" maxlength="10" class="input-text" placeholder="<?php echo $this->__('Enter ZIP Code'); ?>"/>
            <button type="button" name="zip-check" title="Check" class="button" 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 if(Mage::getModel('core/cookie')->get('message')){
    echo Mage::getModel('core/cookie')->get('message');
}
else{
    $defaultHtml; } ?></div>

        <br>        
    </div>
</div>

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

And the indexController.php

<?php
class NextBits_CheckDelivery_IndexController extends Mage_Core_Controller_Front_Action
{
    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;
        }

        Mage::getModel('core/cookie')->set('message', $response['message']);
        Mage::getModel('core/cookie')->set('zip', $zip);
        Mage::getModel('core/cookie')->set('html', $response['html']);
        echo json_encode($response);exit;
    }
}

Here i saved the customer entered data on cookie using

 Mage::getModel('core/cookie')->set('message', $response['message']);
    Mage::getModel('core/cookie')->set('color', $response['color']);
    Mage::getModel('core/cookie')->set('html', $response['html']);

And simple retrieve the value using Mage::getModel('core/cookie')->get('message').

Hope this will help you.. Happy learning

  • but it didt worked for me. – Baby in Magento Apr 25 '16 at 11:33
  • Did you replace your code with my code??? – Asish Hira Apr 25 '16 at 11:34
  • ya, i did that..... – Baby in Magento Apr 25 '16 at 11:34
  • Show me your indexController.php and checkdelivery.phtml code – Asish Hira Apr 25 '16 at 11:34
  • 1
    It is my pleasure that i can help you – Asish Hira Apr 25 '16 at 12:24
2

You can save your zip code for user's session in your controller checkdelivery/index/index. It will look like this:

Mage::getSingleton('customer/session')->setData('custom_zip_code', $this->getRequest()->getParam('zipcode'));

And get it everywhere during a single user’s session:

$zip = Mage::getSingleton('customer/session')->getData('custom_zip_code');
  • i tried your code in checkdelivery.phtml code as here but it didt worked, correct me if i am wrong. – Baby in Magento Apr 25 '16 at 11:27
  • This code Mage::getSingleton('customer/session')->setData('custom_zip_code', $this->getRequest()->getParam('zipcode')); should be used in the controller. – MageWorx Apr 25 '16 at 11:31
  • This will not work if customer is not logged in! – Asish Hira Apr 25 '16 at 11:36
  • @MageWorx same code is there in answer also right? i tried with same, please check it. i added your code in checkdelivery.phtml file – Baby in Magento Apr 25 '16 at 11:43
1

to store zip code you can use registry like Mage::register('zip-code', $your-data); and on product page you have to use $zip-code= Mage::registry('zip-code'); and then if($zip-code > 0) { echo 'Cash on delivery is available';} and put the value in textbox.

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.