我在 magento 1.7.0.2 中使用货到付款付款方式。
我仅对某些邮政编码/密码需要此付款选项。
谁能帮忙?
第 1
步转到文件
app\code\core\Mage\Payment\etc\system.xml
Line no Nearly596
下<cashondelivery translate="label">
xml 节点
添加代码
<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>
第 2
步转到文件
app\code\core\Mage\Payment\Model\Method\Cashondelivery.php
Class Mage_Payment_Model_Method_Cashondelivery
在结束标记之前添加以下函数}
/**
* 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;
}
第 3 步
转到文件
app\code\core\Mage\Payment\Block\Form\Container.php
Class Mage_Payment_Block_Form_Container
在结束标记之前在函数后面添加广告}
/**
* 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;
}
}
第 4 步
转到文件
app\design\frontend\base\default\template\checkout\onepage\payment\methods.phtml
在第 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>
用。。。来代替
<?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 } ?>
打开你的 app\design\frontend\base\default\template\checkout\onepage\payment\methods.phtml 文件。
在此文件中,您可以从报价对象中获取报价送货地址,只需从地址中获取邮政编码,然后在内部foreach ($methods as $_method):
检查您的情况,如果邮政编码$_method->getCode();
匹配然后返回。您可以在此处获取方法代码。
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.
我做了以下
在 app\code\local\Mage\Payment\Model\Method\Cashondelivery.php
我添加了以下功能
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;
}
并在 app\code\local\Mage\Payment\Block\Form\Container.php
protected function _canUseMethod($method){
我在...下添加了以下代码
在以下块之后
if((!empty($minTotal) && ($total < $minTotal)) || (!empty($maxTotal) && ($total > $maxTotal))) {
return false;
}
添加
if($method->getCode() == "cashondelivery"){
$postcodes = Mage::getModel('payment/method_cashondelivery')->getCODPincodes();
$shippingPincode = $this->getQuote()->getShippingAddress()->getData('postcode');
if(!in_array($shippingPincode, $postcodes)){
return false;
}
}
笔记:
我在一个名为 pincode 的表中拥有所有的密码。
我将为此开发一个小模块,因为这是满足我要求的快速解决方案
您只需要编辑文件 /app/code/core/Mage/Payment/Model/Method/Cashondelivery.php
打开它并将下面的代码添加到类的最后(就在文件中的最后一个“}”之前):
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);
}