此版本考虑了产品选项和一级价格:
<script type="text/javascript">
jQuery(function($){
// probably you want a custom method in your block for getting a better and safer tierPrices array here
// for example with formatted prices
var tierPrices = <?php
$_tier_json = json_encode($_product->getTierPrice());
$_tier_json = substr($_tier_json,0,1) . '{"price":"'.$_product->getFinalPrice().'","price_qty":"1"},' . substr($_tier_json,1);
echo $_tier_json;
?>;
var getPrice = function(qty){
qty = Number(qty);
var i = tierPrices.length;
while(i--)
{
if(qty >= tierPrices[i]['price_qty']){
return tierPrices[i]['price'];
}
}
return null;
};
var formatPrice = function(price) {
return '$' + parseFloat(price).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,');
};
var updatePrice = function(price){
// if product has options, use optionsPrice functionality
if ( typeof optionsPrice != 'undefined' && typeof optionsPrice.productPrice != 'undefined' ) {
optionsPrice.productPrice = price;
optionsPrice.reload();
} else {
// or if it is a simple product, change price directly in the html
$(".price-box .price").html( formatPrice(price) );
}
};
var updatePriceEvent = function() {
var price = getPrice( $('#qty').val() );
if(price !== null){
updatePrice(price);
}
};
$('#qty').change( updatePriceEvent );
$('div.qty-changer .qty_inc, div.qty-changer .qty_dec').click( updatePriceEvent );
});
</script>
如果是可配置产品,或带有自定义选项的产品,它将根据当前选择的选项调整价格。另外,$_product->getTierPrice() 返回从第二层开始的价格,这将在数量较少时显示错误的价格