2013-05-08 66 views

回答

1

你有一個函數

public function isAvailable($quote = null) 

在最後一行之前return $checkResult->isAvailable;

你把一個if條件if($checkResult->isAvailable)呼叫

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

,並在此功能適用的邏輯來獲得計費從報價對象中找到郵政編碼並檢查允許的郵編列表並設置標誌。

注意:修改時不要修改核心代碼,使用重寫或覆蓋Magento的概念。

1

打開您的應用程序\設計\前端\基地\默認\模板\簽出\ onepage \ payment \ methods.phtml文件。

在這個文件中你可以得到報價運送地址從報價對象,只需從地址和foreach ($methods as $_method):內部郵政編碼檢查您的條件,如果郵政編碼和$_method->getCode();匹配,然後返回。 你可以在這裏獲得方法代碼。

在COD
+0

非常感謝,你讓我的一天。 – 2014-01-14 16:45:20

0

我做了以下

在 應用程序\代碼\本地\法師\付款方式\型號\方法\ 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; 
} 

和應用\ 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; 
     } 
    } 

注:

我有一個名爲pin碼錶中的所有pincodes。

我將開發這個小模塊,因爲這是一個快速解決我的要求

2

URL1- How to restrict default COD in magento to certain zip codes only?

STEP 1
轉到文件

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

Line no 596<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> 


STEP 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; 
} 

STEP-3前10 轉到文件
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 } ?> 
+0

永遠不要修改核心。 Magento開發中的第1課 – 2015-07-22 12:56:36

0

所有你需要的是編輯文件/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); 
}