【发布时间】:2020-09-08 18:12:53
【问题描述】:
处理显示/覆盖 Magento 2.3.3 定价的文件,我有以下 JQuery 语句:
if(price != 0){
$('#product-price-'+product_id+' .price').html(price);
}
我想改变它,如果价格 = 0,它会显示一条消息,要求报价。我试过以下没有运气。它仍然显示所有 0 价格。
if(price != 0){
alert('Call For Quote');
}
这是完整的脚本:
<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>
【问题讨论】:
-
如果我没看错,你可能只需要将
price != 0更改为price === 0.. -
已更改。价格仍显示为 0 美元。
-
“$”是否包含在价格变量的值中?
-
价格在前端显示为 0.00 美元。我在上面添加了完整的脚本。
-
console.log(typeof price)显示什么?