How to restrict default COD in magento to certain

2020-03-07 07:54发布

问题:

I am using Cash On Delivery Payment method in magento 1.7.0.2.

I need this payment option only for certain zip codes/pin codes.

can anyone help?

回答1:

in COD you have a function

public function isAvailable($quote = null)

in this before last line return $checkResult->isAvailable;

you place an if condition if($checkResult->isAvailable) call

$this->isZipCodeallowedForCod($zipCode,$quote)

and in this function apply logic to get billing address zip code from quote object and checking with list of zipcodes allowed and setting flag.

Note : When modifying this do not modify core code use rewrite or override concept of Magento.



回答2:

STEP 1
Go To file

app\code\core\Mage\Payment\etc\system.xml 

Line no Nearly 596 under the <cashondelivery translate="label"> xml Node
Add code

<pincode translate="label">
    <label>pincode</label>
    <frontend_type>textarea</frontend_type>
    <sort_order>63</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>1</show_in_store>
</pincode>


STEP 2
Go To File
app\code\core\Mage\Payment\Model\Method\Cashondelivery.php

Add following Function in Class Mage_Payment_Model_Method_Cashondelivery before closing tag }

/**
* Get All postcode allowed for COD cash on delivary module
* @return array
*/
 public function getCODPincodes(){
     $pincodes = Mage::getStoreConfig('payment/cashondelivery/pincode');
     $results=explode(',',$pincodes);
     $postcodes=array();
     if(count($results)>0){
        foreach($results as $result){
            $postcodes[] = $result;
        }
     }
     return $postcodes;
 }

STEP- 3
Go To File
app\code\core\Mage\Payment\Block\Form\Container.php

Ad following Function in Class Mage_Payment_Block_Form_Container before closing tag }

 /**
 * Chcek Payment Method is COD(cash on delivary) and Shiping Pin code is available in list
 * @return boolean
 */
 protected function checkmethodbypin($method){
    if($method->getCode() == "cashondelivery"){
        $shippingPincode = $this->getQuote()->getShippingAddress()->getData('postcode');
        $pincodes= Mage::getModel('payment/method_cashondelivery')->getCODPincodes();
        if(in_array($shippingPincode, $pincodes)) return true ;else return false;
    }
 }

Step 4
Go to File

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

Find code Near Line 43

<?php if(!$oneMethod): ?>
    <input id="p_method_<?php echo $_code ?>" value="<?php echo $_code ?>" type="radio" name="payment[method]"title="<?php echo $this->htmlEscape($_method->getTitle()) ?>" onclick="payment.switchMethod('<?php echo $_code ?>')"<?phpif($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)) ?></div><div>
<?php echo $this->getMethodLabelAfterHtml($_method) ?></label>

Replace with

<?php if(($_method->getCode() == "cashondelivery") && !$this->checkmethodbypin($_method)){?>
    <dt><label style="color:red">COD (Cash on Delivary) not available on this pin code</label></dt>
 <?php }else{?>
    <dt>
        <?php if(!$oneMethod): ?>
        <input id="p_method_<?php echo $_code ?>" value="<?php echo $_code ?>" type="radio" name="payment[method]"title="<?php echo $this->htmlEscape($_method->getTitle()) ?>" onclick="payment.switchMethod('<?php echo $_code ?>')"<?phpif($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)) ?> <?phpecho $this->getMethodLabelAfterHtml($_method) ?></label>
    </dt>
<?php } ?>


回答3:

Open your app\design\frontend\base\default\template\checkout\onepage\payment\methods.phtml file.

In this file you can get quote shipping address from quote object , just get zip codes from address and inside foreach ($methods as $_method): check your condition if zip code and $_method->getCode(); match then return. You can get method code here .



回答4:

I did the following

in app\code\local\Mage\Payment\Model\Method\Cashondelivery.php

I added the following function

public function getCODPincodes(){
    $write = Mage::getSingleton('core/resource')->getConnection('core_read');
    $query = "select pincode from `pincode`";
    $results = $write->fetchAll($query);
    foreach($results as $result){
        $postcodes[] = $result['pincode'];
    }
    return $postcodes;
}

and in app\code\local\Mage\Payment\Block\Form\Container.php

i added the following code under protected function _canUseMethod($method){...

after the following block

if((!empty($minTotal) && ($total < $minTotal)) || (!empty($maxTotal) && ($total > $maxTotal))) {
        return false;
    }

add

if($method->getCode() == "cashondelivery"){
        $postcodes = Mage::getModel('payment/method_cashondelivery')->getCODPincodes();
        $shippingPincode = $this->getQuote()->getShippingAddress()->getData('postcode');
        if(!in_array($shippingPincode, $postcodes)){
            return false;
        }
    }

Note:

I have all the pincodes in a table called pincode.

I will develop a small module on this as this is a quick fix for my requirement



回答5:

All you need is to edit the file /app/code/core/Mage/Payment/Model/Method/Cashondelivery.php

Open it and add the code below to the very end of the class (just before the last “}” in the file):

public function isAvailable($quote = null)
{
    if ($quote) {
        // Here is the list of restricted Zip Codes
        $restrictedZips = array(
            '85001',
            '87965'
        );
        $address = $quote->isVirtual() ? $quote->getBillingAddress() : $quote->getShippingAddress();
        $customerZip = $address->getPostcode();
        if (in_array($customerZip, $restrictedZips)) {
            return false;
        }
    }
    return parent::isAvailable($quote);
}