このバージョンでは、製品オプションと第1段階の価格を考慮に入れています。
<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()は、第2層から始まる価格を返します。これにより、数量が少ない場合は間違った価格が表示されます。