我正在尝试显示一条消息,检查邮政编码并返回是否可以使用货到付款和运送邮政编码。但是我只能通过一个条件得到回复。这里我发布了我正在使用的代码。您可以帮助我如何以不同的方式显示COD和运输信息。 这是我的phtml文件。
<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文件检查有效的zipcode并显示响应。
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 :(得分: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));
}
}