0

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

6
  • 1
    There is a setting in admin to display error message if particular method is not available. Have you tried there ?
    – Prateek
    Commented Oct 1, 2015 at 10:38
  • actually COD payment method is available only for some zip codes Commented Oct 1, 2015 at 10:39
  • 1
    Yes, but when you return false, Magento should be able to display that error message if you have enabled that.
    – Prateek
    Commented Oct 1, 2015 at 10:40
  • than i iwll try now..... Commented Oct 1, 2015 at 10:41
  • 1
    Please see my answer below.
    – Prateek
    Commented Oct 1, 2015 at 12:32

3 Answers 3

1

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

Apologies, I confused with shipping method error messages.

To achieve what you are trying, you will need to override block Mage_Payment_Block_Form_Container and change method getMethods() so that it includes not available COD method.

Then you'll need to change the template file checkout/onepage/payment/methods.phtml for your custom error message. There you'll need to check for COD and then if it's available for checkout.

0
1

Require to check quote by using !empty($quote) to prevent error

please try this:

  public function isAvailable($quote = null)
    {
         $restrictedZips = array('641004','641006');


        if(parent::isAvailable($quote) && !empty($quote)):
            $address = $quote->isVirtual() ? $quote->getBillingAddress() : $quote->getShippingAddress();
            $customerZip = $address->getPostcode();
             if (in_array($customerZip, $restrictedZips)) {
                return true;
            }
            return  false;
        endif;
       return  false;      
    }

Additional:

Instead do code at core class.It will be better idea to use Magento Event/Observer.

For your case use: payment_method_is_active event

See an example is How to disable Cash On Delivey on specific product type.

Just change at code of observer.

<?php
class YOURNANESPACE_YOURMODULE_Model_Observer {

    public function filterpaymentmethod(Varien_Event_Observer $observer) {
    /* call get payment method */
    $method = $observer->getEvent()->getMethodInstance();

    /*   get  Quote  */
    $quote = $observer->getEvent()->getQuote();
    $restrictedZips = array('641004','641006');   

    if($method->getCode()=='cashondelivery' ){

      if(!empty($quote)):
            $address = $quote->isVirtual() ? $quote->getBillingAddress() : $quote->getShippingAddress();
            $customerZip = $address->getPostcode();
             if (in_array($customerZip, $restrictedZips)) {
               return $result->isAvailable= true;
            }
             return $result->isAvailable=  false;
           endif;  
        }
   return $result->isAvailable=false  
    }
}
8
  • i will try this... Commented Oct 1, 2015 at 10:55
  • where i have place " cod is not avaialble " text Commented Oct 1, 2015 at 11:03
  • 1
    This is not possible for this code..In magento, payment method avaiable only check by true /false
    – Amit Bera
    Commented Oct 1, 2015 at 11:05
  • 1
    May be use Mage::throwException(Mage::helper('payment')->__('cod is not available for this zip code.')); but not sure 100%
    – Amit Bera
    Commented Oct 1, 2015 at 11:10
  • 1
    Just above return false;
    – Amit Bera
    Commented Oct 1, 2015 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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