Working on a file that displays/overlays pricing for Magento 2.3.3 and I have the following JQuery statement:
if(price != 0){
$('#product-price-'+product_id+' .price').html(price);
}
I want to change that so that if the price = 0, it displays a message to call for a quote. I have tried the following with no luck. It still displays all 0 prices.
if(price != 0){
alert('Call For Quote');
}
Here is the full script:
<script type="text/javascript">
require(["jquery","mage/loader"],function($) {
$(document).ready(function() {
var customurl = "<?php echo $this->getUrl() . 'customer/index/price'?>";
var productId = "<?php echo $productId; ?>";
var loaderImg = "<?= $block->getViewFileUrl('images/loader-1.gif') ?>";
var loaderHtml = '<div id="price-panel-'+productId+'" data-role="pannel" class="price-panel" style="position: absolute;"><div data-role="loader" class="loading-mask" style="position: relative;"><div class="loader"><img style="position: relative;" src="'+loaderImg+'" alt="loading" width="30px"></div></div>';
$('[data-role=priceBox]').hide();
$('#product-price-'+productId+' .price').hide();
$('[data-product-id='+productId+']').before(loaderHtml);
$("#price-panel-"+productId+ " .loader").show();
$.ajax({
url: customurl,
type: 'POST',
dataType: 'json',
data: {
productId: productId,
},
complete: function(response) {
for (let [key, value] of Object.entries(response.responseJSON)) {
product_id = value.productId;
price = value.price;
qty = value.qty;
/* product_id = response.responseJSON.productId;
price = response.responseJSON.price;
qty = response.responseJSON.qty;*/
if(product_id){
$('[data-role=priceBox]').show();
loaderClass = "div.price-panel-"+product_id;
$("#price-panel-"+product_id).hide();
$('#product-price-'+product_id+' .price').show();
if (price != 0) {
$('#product-price-' + product_id + ' .price').html(price);
} else {
alert("Call for quote");
}
/* if(qty != 0){
$('#product-available-qty-'+product_id).html('Available: '+qty);
}*/
console.log(price);
}
}
},
error: function (xhr, status, errorThrown) {
console.log('Error happens. Try again.');
}
});
});
});
</script>
2
Answers
Put the alert in the
else
block.Have you tried to use the
!==
operator? as it compares the value and also checks the type.e.g.
price !== 0