Inderjeet Singh
Inderjeet Singh

Reputation: 566

Disable Cash on Delivery for specific products

I am running Magento v1.9.0.1. I want to disable Cash on Delivery for specific products. I have installed COD extension from magento commerce.

I am new to Magento structure and no nothing to little about it. Tried some debugging on it but it is going up above my head.

Also, I have search every where over the internet but no one has ever provided the code for Magento v1.9.0.1.

Upvotes: 0

Views: 3683

Answers (2)

Pavan Kumar
Pavan Kumar

Reputation: 1735

Create a custom attribute with attribute code as cod and assign it to General of attribute sets.

Now as per you requirement change your methods.phtml (if there is no custom code included in this) as follows.

<?php
    $methods = $this->getMethods();
    $oneMethod = count($methods) <= 1;
?>
<?php if (empty($methods)): ?>
    <dt>
        <?php echo $this->__('No Payment Methods') ?>
    </dt>
<?php else:
    foreach ($methods as $_method):
        $_code = $_method->getCode();
?>
    <dt>
    <?php 
    //cod verification starts
    $attr_cod = array();
    $cartItems = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
    foreach ($cartItems as $item) {
        $attr_cod[] = Mage::getModel('catalog/product')->load($item->getProduct()->getId())->getCod();

    }
    if(in_array('1', $attr_cod) && $_code == 'cashondelivery') {continue; }
    //cod verification ends
?>
    <?php if(!$oneMethod): ?>

        <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 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; ?>
        <label for="p_method_<?php echo $_code ?>"><?php echo $this->escapeHtml($this->getMethodTitle($_method)) ?> <?php echo $this->getMethodLabelAfterHtml($_method) ?></label>
    </dt>
    <?php if ($html = $this->getPaymentMethodFormHtml($_method)): ?>
    <dd>
        <?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>

I have tested this and works fine.

Upvotes: 1

urfusion
urfusion

Reputation: 5501

COD is already in magento you didn't need to install it.

There are two way

  1. create a new config setting in which you can enter ids of products for which you didn't want COD option.
  2. You can create product attribute too for checking COD option.

and check these ids (if you create config settings ) with the product id or check for the product attribute on app\design\frontend\base\default\template\checkout\onepage\payment\methods.phtml. And by simple if else conditions you can avoid COD option for these products.

Upvotes: 1

Related Questions