2017-08-01 63 views
-1

我想顯示一條消息,檢查郵政編碼,並返回是否可以爲郵政編碼進入交貨和運輸。但是我能夠得到只有一個條件的答覆。在這裏發佈我正在使用的代碼。可以幫助我如何顯示消息的COD和運輸方式不同。 這是我的phtml文件。顯示2條回覆ajax成功

<script> 
     function checkPostcode() { 


      var deliveryMessage = jQuery('#checkDelivery-form #is_deliverable'); 
      var deliveryMessage1 = jQuery('#checkDelivery-form #cod_delivery'); 
      deliveryMessage.removeClass('success').removeClass('error').hide(); 
      deliveryMessage1.removeClass('success').removeClass('error').hide(); 
      var deliveryForm = jQuery('#checkDelivery-form'); 
      if(deliveryForm.valid()) { 
       jQuery.ajax({ 
        type: "POST", 
        url:'<?php echo $this->getUrl('checkDelivery/postcode/check') ?>', 
        data: deliveryForm.serialize(), 
        dataType: "json", 
        showLoader: true, 
        success: function(response) { 
         deliveryMessage.removeClass('mage-error'); 
         deliveryMessage.removeClass('mage-success'); 
         deliveryMessage.addClass(response.type).html(response.message).show(); 
         deliveryMessage.attr('generated','true'); 
        } 
        } 
       }); 
      } 
      return false; 
     } 
</script> 

這是我的PHP文件檢查有效的郵政編碼並顯示響應依據。

public function execute() 
    { 
     ob_start(); 
     $response = []; 
     try { 
      if (!$this->getRequest()->isAjax()) { 
       throw new \Exception('Invalid request.'); 
      } 
      if (!$postcode = $this->getRequest()->getParam('zipcode')) { 
       throw new \Exception('Please enter pincode'); 
      } 

      $region = $this->zipcodeRegion->getCollection()->addFieldToSelect('*')->addFieldToFilter('zip_code',$postcode)->getFirstItem(); 

      if($region['zip_code'] == $postcode) 
      { 
       if($region['is_blocked'] == 1) 
       { 
        if($region['is_cod_available'] == 0) 
        { 
         $response['type'] = 'mage-error'; 
         $response['message'] = 'No delivery available at selected pincode'; 
        } 

       } 
       else 
       { 
        $response['type'] = 'mage-success'; 
        $response['message'] = 'Delivery available at selected pincode'; 
       } 
      } 
      else 
      { 
       $response['type'] = 'mage-error'; 
       $response['message'] = 'Please enter valid pincode'; 
      } 
     } catch (\Exception $e) { 
      $response['type'] = 'mage-error'; 
      $response['message'] = $e->getMessage(); 

     } 
     $this->getResponse()->setContent(json_encode($response)); 
    } 

} 

回答

0

如果沒有航運是可以不惜一切,你不需要在你的if($region['is_blocked'] == 1){...檢查COD。檢查else中的COD,並在那裏顯示相應的消息。

試試這個:

public function execute() 
    { 
     ob_start(); 
     $response = []; 
     try { 
      if (!$this->getRequest()->isAjax()) { 
       throw new \Exception('Invalid request.'); 
      } 
      if (!$postcode = $this->getRequest()->getParam('zipcode')) { 
       throw new \Exception('Please enter pincode'); 
      } 

      $region = $this->zipcodeRegion->getCollection()->addFieldToSelect('*')->addFieldToFilter('zip_code',$postcode)->getFirstItem(); 

      if($region['zip_code'] == $postcode) 
      { 
       if($region['is_blocked'] == 1) 
       { 

         $response['type'] = 'mage-error'; 
         $response['message'] = 'No delivery available at selected pincode'; 

       } 
       else 
       { 

        if($region['is_cod_available'] == 0) 
        { 
         $response['type'] = 'mage-success'; 
         $response['message'] = 'Delivery available at selected pincode'; 
        }else{ 
         $response['type'] = 'mage-success'; 
         $response['message'] = 'Delivery available at selected pincode but no COD'; 
        } 

       } 
      } 
      else 
      { 
       $response['type'] = 'mage-error'; 
       $response['message'] = 'Please enter valid pincode'; 
      } 
     } catch (\Exception $e) { 
      $response['type'] = 'mage-error'; 
      $response['message'] = $e->getMessage(); 

     } 
     $this->getResponse()->setContent(json_encode($response)); 
    } 

} 
+0

你應該能夠只使用''
打破行。例如:'='在選定的PIN碼處可用的傳送。
化學需氧量不可用。' – Miro

+0

完成..有很多米羅! – user3313626