How to Display Error Message if Cash on Delivery Not Available

cash-on-deliverypayment-methodsPHP

we are using the following cashondelivery.php : http://pastebin.com/iD28fF2F

app/code/core/Mage/Payment/Model/Method/Cashondelivery.php

here we restricted only some zip codes for allowing cash on delivery payment method, means what zip codes we entered in following file,

if we enter those zip codes than only cash on delivery payment method will display,

if we enter other zip codes cash on delivery payment method will not display . but we have to

display the error message "cash on delivery is not available"

how we can achieve this….

Best Answer

We can not say this is the write way but it might be work for you.

app/design/frontend/base/default/template/checkout/onepage/payment/methods.phtml

You can replace your methods.phtml code with below code.

<?php
$methods = $this->getMethods();
$oneMethod = count($methods) <= 1;

$quote = Mage::getSingleton('checkout/session')->getQuote();
$restrictedZips = array('560043', '641006');
$address = $quote->isVirtual() ? $quote->getBillingAddress() : $quote->getShippingAddress();
$customerZip = $address->getPostcode();
?>

<?php if (empty($methods)): ?>
    <dt>
    <?php echo $this->__('No Payment Methods') ?>
    </dt>
    <?php
else:
    foreach ($methods as $_method):
        $_code = $_method->getCode();
        ?>
        <dt id="dt_method_<?php echo $_code ?>">
        <?php if (!$oneMethod): ?>

            <?php if ($_code == 'cashondelivery' && !in_array($customerZip, $restrictedZips)): ?>

            <?php else: ?>        
                <input id="p_method_<?php echo $_code ?>" value="<?php echo $_code ?>" type="radio" name="payment[method]" title="<?php echo $this->escapeHtml($_method->getTitle()) ?>" onclick="payment.switchMethod('<?php echo $_code ?>')"<?php if ($this->getSelectedMethodCode() == $_code): ?> checked="checked"<?php endif; ?> class="radio" />
            <?php endif; ?>    

        <?php else: ?>

            <span class="no-display"><input id="p_method_<?php echo $_code ?>" value="<?php echo $_code ?>" type="radio" name="payment[method]" checked="checked" class="radio" /></span>

            <?php $oneMethod = $_code; ?>

        <?php endif; ?>

        <?php if ($_code == 'cashondelivery' && !in_array($customerZip, $restrictedZips)): ?>
            <label style="color: red;"><?php echo $this->__('cash on delivery is not available'); ?>   </label>
        <?php else: ?> 
            <label for="p_method_<?php echo $_code ?>"><?php echo $this->escapeHtml($this->getMethodTitle($_method)) ?> <?php echo $this->getMethodLabelAfterHtml($_method) ?></label>
        <?php endif; ?>

        </dt>
        <?php if ($html = $this->getPaymentMethodFormHtml($_method)): ?>
            <dd id="dd_method_<?php echo $_code ?>">
                <?php echo $html; ?>
            </dd>
        <?php endif; ?>
        <?php
    endforeach;
endif;
?>
<?php echo $this->getChildChildHtml('additional'); ?>
<script type="text/javascript">
    //<![CDATA[
<?php echo $this->getChildChildHtml('scripts'); ?>
    payment.init();
<?php if (is_string($oneMethod)): ?>
        payment.switchMethod('<?php echo $oneMethod ?>');
<?php endif; ?>
    //]]>
</script>
Related Topic