2016-03-04 58 views
2

後,限制郵政編碼爲自定義運輸方式不起作用我們真的在這裏面臨很奇怪的問題。隱藏運輸方法

我們正在使用1.9.0.1和自定義發貨方式。

我們使用了應答通過@Marius這裏隱藏在結賬送貨方法步驟:

https://magento.stackexchange.com/questions/53355/remove-shipping-steps-in-onepage-checkout

但是我們這裏有一個問題。

我們只允許一些郵政編碼下訂單。

但這裏它允許所有的郵政編碼,當用戶第一次進入。

一旦用戶輸入郵政編碼,然後點擊 「繼續」 按鈕,將進入下一步[付款方式]

enter image description here

付款方法步驟

enter image description here

如果用戶再次來到上一步並輸入相同的郵政編碼,並且用戶點擊「繼續」

按鈕,比它會彈出顯示錯誤消息 - 「無效的運輸方式」

enter image description here

這是好的,但當用戶首次輸入的郵政編碼也應該不會允許。

在一切正常之前,一旦我們隱藏運輸方法,就會發生此問題。但對於默認的運輸方式,它工作正常。

EX網站:link &郵編000000

我們正在使用此代碼限制郵政編碼和尋找運費。

<?php 
class extension_Mpperproductshipping_Model_Carrier_LocalDelivery extends Mage_Shipping_Model_Carrier_Abstract 
{ 
    /* Use group alias */ 
    protected $_code = 'mpperproductshipping'; 

    public function collectRates(Mage_Shipping_Model_Rate_Request $request){   
    $postCode = $request->getDestPostcode(); 
    $restrictedCodes = array(

110001, 
110002 

); //restricted values. they can come from anywhere 
    if (!in_array($postCode, $restrictedCodes)) { 
     return false; 

    } 

     $result = Mage::getModel('shipping/rate_result'); 

     /* Edited by vikas_mageworx */ 
     $postcode=$request->getDestPostcode(); 
     $countrycode=$request->getDestCountry(); 
     $items=$request->getAllItems(); 
     /* End Editing by vikas_mageworx */ 

     $postcode=str_replace('-', '', $postcode); 
     $shippingdetail=array(); 

     /* one start */ 
     $shippostaldetail=array('countrycode'=>$countrycode,'postalcode'=>$postcode,'items'=>$items); 
     /* one end */ 



     foreach($items as $item) { 
      $proid=$item->getProductId(); 
      $options=$item->getProductOptions(); 
      $mpassignproductId=$options['info_buyRequest']['mpassignproduct_id']; 
      if(!$mpassignproductId) { 
       foreach($item->getOptions() as $option) { 
        $temp=unserialize($option['value']); 
        if($temp['mpassignproduct_id']) { 
         $mpassignproductId=$temp['mpassignproduct_id']; 
        } 
       } 
      } 
      if($mpassignproductId) { 
       $mpassignModel = Mage::getModel('mpassignproduct/mpassignproduct')->load($mpassignproductId); 
       $partner = $mpassignModel->getSellerId(); 
      } else { 
       $collection=Mage::getModel('marketplace/product') 
        ->getCollection()->addFieldToFilter('mageproductid',array('eq'=>$proid)); 
       foreach($collection as $temp) { 
        $partner=$temp->getUserid(); 
       } 
      } 

      $product=Mage::getModel('catalog/product')->load($proid)->getWeight(); 
      $weight=$product*$item->getQty(); 
      if(count($shippingdetail)==0){ 
       array_push($shippingdetail,array('seller_id'=>$partner,'items_weight'=>$weight,'product_name'=>$item->getName(),'qty'=>$item->getQty(),'item_id'=>$item->getId())); 
      }else{ 
       $shipinfoflag=true; 
       $index=0; 
       foreach($shippingdetail as $itemship){ 
        if($itemship['seller_id']==$partner){ 
         $itemship['items_weight']=$itemship['items_weight']+$weight; 
         $itemship['product_name']=$itemship['product_name'].",".$item->getName(); 
         $itemship['item_id']=$itemship['item_id'].",".$item->getId(); 
         $itemship['qty']=$itemship['qty']+$item->getQty(); 
         $shippingdetail[$index]=$itemship; 
         $shipinfoflag=false; 
        } 
        $index++; 
       } 
       if($shipinfoflag==true){ 
        array_push($shippingdetail,array('seller_id'=>$partner,'items_weight'=>$weight,'product_name'=>$item->getName(),'qty'=>$item->getQty(),'item_id'=>$item->getId())); 
       } 
      } 
     } 
     $shippingpricedetail=$this->getShippingPricedetail($shippingdetail,$shippostaldetail); 

     if($shippingpricedetail['errormsg']!==""){ 
      Mage::getSingleton('core/session')->setShippingCustomError($shippingpricedetail['errormsg']); 
      return $result; 
     } 
     /*store shipping in session*/ 
     $shippingAll=Mage::getSingleton('core/session')->getData('shippinginfo'); 
     $shippingAll[$this->_code]=$shippingpricedetail['shippinginfo']; 
     Mage::getSingleton('core/session')->setData('shippinginfo',$shippingAll); 

     $method = Mage::getModel('shipping/rate_result_method'); 
     $method->setCarrier($this->_code); 
     $method->setCarrierTitle(Mage::getStoreConfig('carriers/'.$this->_code.'/title')); 
     /* Use method name */ 
     $method->setMethod($this->_code); 
     $method->setMethodTitle(Mage::getStoreConfig('carriers/'.$this->_code.'/name')); 
     $method->setCost($shippingpricedetail['handlingfee']); 
     $method->setPrice($shippingpricedetail['handlingfee']); 
     $result->append($method); 
     return $result; 
    } 

public function getShippingPricedetail($shippingdetail,$shippostaldetail) { 
     $shippinginfo=array(); 
     $handling=0; 
     $session = Mage::getSingleton('checkout/session'); 
     $customerAddress = $session->getQuote()->getShippingAddress(); 


/* Edited by vikas_boy */ 
$customerPostCode = $shippostaldetail['postalcode']; 
$items = $shippostaldetail['items']; 
/* End Editing by vikas_boy */ 


/* one */ 

     foreach($shippingdetail as $shipdetail) { 
      $seller = Mage::getModel("customer/customer")->load($shipdetail['seller_id']); 
      $sellerAddress = $seller->getPrimaryShippingAddress(); 
      $distance = $this->getDistanse($sellerAddress->getPostcode(),$customerPostCode); 
      // echo "distance ".$distance;die; 
      $price = 0; 
      $itemsarray=explode(',',$shipdetail['item_id']); 
      foreach($items as $item) { 
       $proid=$item->getProductId(); 
       $options=$item->getProductOptions(); 
       $mpassignproductId=$options['info_buyRequest']['mpassignproduct_id']; 
       if(!$mpassignproductId) { 
        foreach($item->getOptions() as $option) { 
         $temp=unserialize($option['value']); 
         if($temp['mpassignproduct_id']) { 
          $mpassignproductId=$temp['mpassignproduct_id']; 
         } 
        } 
       } 
       if (Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($proid)) 
       { 
        continue; 
       } 
       $mpshippingcharge = 0; 
       $localDistance = Mage::getStoreConfig('marketplace/mpperproductshipping/local_shipping_distance'); 
       $regionalDistance = Mage::getStoreConfig('marketplace/mpperproductshipping/regional_shipping_distance'); 
       $stateDistance = Mage::getStoreConfig('marketplace/mpperproductshipping/state_shipping_distance'); 
       if(in_array($item->getId(),$itemsarray)) { 
        if($mpassignproductId) { 
         if($distance < $localDistance) { 
          $mpshippingcharge=Mage::getModel('mpassignproduct/mpassignproduct')->load($mpassignproductId)->getLocalShippingCharge(); 
         } elseif($distance > $localDistance && $distance < $regionalDistance) { 
          $mpshippingcharge=Mage::getModel('mpassignproduct/mpassignproduct')->load($mpassignproductId)->getRegionalShippingCharge(); 
         } elseif($distance > $regionalDistance) { 
          $mpshippingcharge=Mage::getModel('mpassignproduct/mpassignproduct')->load($mpassignproductId)->getStateShippingCharge(); 
         } 
        } else { 
         // echo "imte ".$item->getProductId(); 
         if($distance < $localDistance) { 
          $mpshippingcharge=Mage::getModel('catalog/product')->load($item->getProductId())->getMpLocalShippingCharge(); 
          // echo "imte ".$item->getProductId(); 
          // echo "ship ".$mpshippingcharge; 
         } elseif($distance > $localDistance && $distance < $regionalDistance) { 
          $mpshippingcharge=Mage::getModel('catalog/product')->load($item->getProductId())->getMpRegionalShippingCharge(); 
         } elseif($distance > $regionalDistance) { 
          $mpshippingcharge=Mage::getModel('catalog/product')->load($item->getProductId())->getMpStateShippingCharge(); 
         } 
        } 

        /* tt */ 
        // echo "test ".$mpshippingcharge;die; 
        if(!is_numeric($mpshippingcharge)){ 
         $price=$price+floatval($this->getConfigData('defalt_ship_amount')* floatval($item->getQty())); 
        }else{ 
         $price=$price+($mpshippingcharge * floatval($item->getQty())); 
        } 

       } 
      } 

      $handling = $handling+$price; 
      $submethod = array(array('method'=>Mage::getStoreConfig('carriers/'.$this->_code.'/title'),'cost'=>$price,'error'=>0)); 
      array_push($shippinginfo,array('seller_id'=>$shipdetail['seller_id'],'methodcode'=>$this->_code,'shipping_ammount'=>$price,'product_name'=>$shipdetail['product_name'],'submethod'=>$submethod,'item_ids'=>$shipdetail['item_id'])); 
     } 
     $msg=""; 
     return array('handlingfee'=>$handling,'shippinginfo'=>$shippinginfo,'errormsg'=>$msg); 
    } 


/* one end */ 

    /* tt start */ 

    private function getDistanse($origin,$destination) { 
     $url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=".$origin.",india&destinations=".$destination.",india&mode=driving&language=en-EN&sensor=false"; 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($ch, CURLOPT_PROXYPORT, 3128); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
     $response = curl_exec($ch); 
     curl_close($ch); 
     $response_all = json_decode($response); 
     $distance = $response_all->rows[0]->elements[0]->distance->value/1000; 
     if($distance==0){ 
      $zips = array(
       $origin,$destination 
       // ... etc ... 
      ); 

      $geocoded = array(); 
      $serviceUrl = "http://maps.googleapis.com/maps/api/geocode/json?components=postal_code:%s&sensor=false"; 
      $curl = curl_init(); 
      foreach ($zips as $zip) { 
       curl_setopt($curl, CURLOPT_URL, sprintf($serviceUrl, urlencode($zip))); 
       curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); 
       $data = json_decode(curl_exec($curl)); 
       $info = curl_getinfo($curl); 
       if ($info['http_code'] != 200) { 
        // Request failed 
       } else if ($data->status !== 'OK') { 
        // Something happened, or there are no results 
       } else { 
        $geocoded[$zip] =$data->results[0]->geometry->location; 
       } 
      } 
      $distance=$this->DistAB($geocoded[$zips[0]]->lat,$geocoded[$zips[0]]->lng,$geocoded[$zips[1]]->lat,$geocoded[$zips[1]]->lng); 

      } 
     return $distance; 
    } 


public function DistAB($lat_a,$lon_a,$lat_b,$lon_b) 

     { 

     $measure_unit = 'kilometers'; 

     $measure_state = false; 

     $measure = 0; 

     $error = ''; 

      $delta_lat = $lat_b - $lat_a ; 
      $delta_lon = $lon_b - $lon_a ; 
      $earth_radius = 6372.795477598; 

      $alpha = $delta_lat/2; 
      $beta  = $delta_lon/2; 
      $a  = sin(deg2rad($alpha)) * sin(deg2rad($alpha)) + cos(deg2rad($this->lat_a)) * cos(deg2rad($this->lat_b)) * sin(deg2rad($beta)) * sin(deg2rad($beta)) ; 
      $c  = asin(min(1, sqrt($a))); 
      $distance = 2*$earth_radius * $c; 
      $distance = round($distance, 4); 

     $measure = $distance; 
     return $measure; 

     } 

    } 

/* tt end */ 

onepage。PHTMLapp/design/frontend/default/em0113/template/checkout/onepage.phml

<div class="page-title"> 
    <h1><?php echo $this->__('Checkout') ?></h1> 
</div> 
<script type="text/javascript" src="<?php echo $this->getJsUrl('varien/accordion.js') ?>"></script> 
<script type="text/javascript" src="<?php echo $this->getSkinUrl('js/opcheckout.js') ?>"></script> 
<ol class="opc" id="checkoutSteps"> 
<?php $i=0; foreach($this->getSteps() as $_stepId => $_stepInfo): ?> 
<?php if (!$this->getChild($_stepId) || !$this->getChild($_stepId)->isShow()): continue; endif; $i++ ?> 
    <li id="opc-<?php echo $_stepId ?>" class="section<?php echo !empty($_stepInfo['allow'])?' allow':'' ?><?php echo !empty($_stepInfo['complete'])?' saved':'' ?>"> 
     <div class="step-title"> 
      <span class="number"><?php echo $i ?>.</span> 
      <h2><?php echo $_stepInfo['label'] ?></h2> 
      <a href="#"><?php echo $this->__('Edit') ?></a> 
     </div> 
     <div id="checkout-step-<?php echo $_stepId ?>" class="step a-item" style="display:none;"> 
      <?php echo $this->getChildHtml($_stepId) ?> 
     </div> 
    </li> 
<?php endforeach ?> 
</ol> 
<script type="text/javascript"> 
//<![CDATA[ 
    var accordion = new Accordion('checkoutSteps', '.step-title', true); 
    <?php if($this->getActiveStep()): ?> 
    accordion.openSection('opc-<?php echo $this->getActiveStep() ?>'); 
    <?php endif ?> 
    var checkout = new Checkout(accordion,{ 
     progress: '<?php echo $this->getUrl('checkout/onepage/progress') ?>', 
     review: '<?php echo $this->getUrl('checkout/onepage/review') ?>', 
     saveMethod: '<?php echo $this->getUrl('checkout/onepage/saveMethod') ?>', 
     failure: '<?php echo $this->getUrl('checkout/cart') ?>'} 
    ); 
//]]> 
</script> 
+0

由於這些限制碼它不應該是'in_array'而不是'in_array' –

+0

對不起,這些都是允許的郵政編碼,我有問題錯誤地提及。 – fresher

+0

我的意思是隻有輸入代碼的郵政編碼應該允許下訂單,所有其他郵政編碼都是有限的郵政編碼。 – fresher

回答

1

你可以做的是綁定一個JavaScript事件PIN碼的輸入字段,併發送請求,每次當有人在PIN碼這樣的打字:

<input type = "text" onchange = "myfun(this.value)"> 

然後將一個javascript函數myfun綁定到它並向您的PIN碼檢查控制器發出一個AJAX調用,這會在您輸入文本框後輸入值

否則,您也可以使用

<input type = "text" onkeypress = "myfun(this.value)">  

然後JavaScript函數myfun重新綁定到它,使一個AJAX調用您的PIN碼檢查控制器,這將花費甚至當你輸入到文本框中

好吧,在你的onepage價值模板,寫這樣的代碼:

jQuery('#billing:postcode').change(function(){ 
    // your ajax call to your controller where you are verifying your pincode, something like: 

     jQuery.ajax({ 
      url: "path/to/your/controller", 
      data: this.value, 
      success: function(response){ 
         if(response == true){ 
          // your success action 
         } else{ 
          //your failure action 
         } 
        } 
     }) 
}) 
+0

您必須將此代碼放在onepage結帳頁面的phtml模板中,如果您可以將該代碼放在此處,那麼我可以幫助您解決問題 –

+0

我會在此處放置一頁結帳代碼。 – fresher

+0

你是要求'app/design/frontend/default/em0113/template/checkout/onepage.phml'代碼嗎? – fresher