2

最近、数人の人から質問がありましたが、答えがあまりなく、他の場所で多くの情報を見つけることができません。

デフォルトでは、製品ページに表示される価格は、カスタムオプションに基づいて動的に更新されます。階層型の価格体系にもとづいて動的に価格を更新するのは大変な苦痛になるのでしょうか。基本的に、ユーザーが段階的価格設定の製品にアクセスし、段階的価格設定の対象となる数量を入力すると、選択した段階的価格設定と数量に基づいて価格が更新されます。

一部のjQueryブードゥーはそれほど難しくないはずなので、値に基づいて価格が再計算されますが、他の誰かが以前にこれを行ったことがあるかどうか、およびこれを行うことで潜在的な落とし穴を認識しているかどうかは興味があります。

これを行わない非常に正当な理由がありますか...言い換えれば、これがMagento Coreの一部として構築されなかった非常に正当な理由がありますか?

4

6 に答える 6

4

はい、javascriptを使用してこれを行うことができます。テンプレート内のいくつかの変数に階層データを配置するだけで<script>、次のように機能します(jQueryを使用する場合)。

テンプレート: catalog \ product \ view.phtml

<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 echo json_encode($_product->getTierPrice()) ?>;
        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 updatePrice = function(price){
            $('.price').html(price);
        };
        // you can use more events here if you want a live response while the user is typing
        $('#qty').change(function(){
            var price = getPrice(this.value);
            if(price !== null){
                updatePrice(price);
            }
        });
    });
</script>
于 2012-09-30T12:35:53.130 に答える
2

簡単な解決策を見つけました。為替レートを取得すると、正常に機能します。次のようなコード

<script type="text/javascript">
jQuery(function($$){
    var inst_price_format = <?php echo Mage::helper('core')->jsonEncode( Mage::app()->getLocale()->getJsPriceFormat() ); ?>;
    var rate = <?php echo Mage::app()->getStore()->getCurrentCurrencyRate(); ?>;
    var tierPrices = <?php echo json_encode($_product->getTierPrice()) ?>;
    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 updatePrice = function(price) { 
        $$('.price-box .price').html( formatCurrency( (price*rate), inst_price_format) ); 
    };
    var updateTotalPrice = function(price, qty) { 
        $$('.total-price').html( formatCurrency( ((price*rate) * qty), inst_price_format) ); 
    };

    $$('#qty').change( function(){
        var price = getPrice(this.value);
        var qty = this.value;
        if(price !== null) { 
            updatePrice(price); 
            updateTotalPrice(price, qty); 
        } else { 
            updatePrice(<?php echo $_product->getPrice(); ?>); 
            updateTotalPrice(<?php echo $_product->getPrice(); ?>, qty);
        }
    });
});

于 2016-10-10T19:11:44.183 に答える
0

私は週末にしばらく時間を費やしてこれを機能させることができましたが、クラスを介して「price_qty」を取得できるようにtierprices.phtmlテンプレートを変更しているのが気に入らなかった。私はそれを交換し、あなたが提案したように代わりに$ _product-> getTierPrice()を使用しました。私が最終的に作成したコードは次のとおりです。

----編集----特別価格もサポートするように書き直しました。

    <script type="text/javascript">
    var $j = jQuery;
    var $p = {};
    var prices = {};
    //dom elements being used
    $p["old"] = $j(".price-box .old-price .price");
    $p["special"] = $j(".price-box .special-price .price");
    $p["regular"] = $j(".price-box .regular-price .price");

    //save original price to reset back if quantity is reset
    //Checking for special price
    if ($p["special"].html()) {
        var specialPrice = $p["special"].html();
        var oldPrice = $p["old"].html();
    } else {
        var originalPrice = $p["regular"].html();
    }

    //lets get to work.
    $j(function(){
        var tiers = <?php echo json_encode($_product->getTierPrice()) ?>;
        var h = tiers.length;
        while (h--) {
            var key = h;
            var line = {};
            //just build the prices object for use later
            line["qty"] = parseInt(tiers[h]["price_qty"]);
            line["price"] = parseFloat(tiers[h]["price"]).toFixed(2);
            prices[key] = line;
        }
        //keyup event works nicely here
        $j("#qty").on("keyup",function(){
            var quantity = $j(this).val();
            for (var i in prices) {
                var z = i;
                //save lowest tier for reset back to original price
                var lowest = prices[0]["qty"];
                //set the range
                var bottom = prices[i]["qty"];
                var top = prices[z++]["qty"];
                //format to currency -- should probably switch to magento's helper method.
                var price = "<?php echo Mage::app()->getLocale()->currency(Mage::app()->getStore()->
     getCurrentCurrencyCode())->getSymbol() ?>"+prices[i]["price"];
                //check if the price needs to be reset after quantity is reset < lowest
                if (quantity < lowest) {
                    if (specialPrice) {
                        $p["special"].html(specialPrice);
                        $p["old"].html(oldPrice);
                    } else {
                        $p["regular"].html(originalPrice);
                    }
                    break;
                }
                //check the ranges, set the price accordingly.
                if (quantity >= bottom) {
                    if (quantity >= top) {
                        if (specialPrice) {
                            $p["special"].html(price);
                        } else {
                            $p["regular"].html(price);
                        }
                        continue;
                    } else {
                        break;
                    }
                }
            }
        })
    })
</script>

ライブフィードバックの代わりに$j( "#qty")。on( "keyup"、function(){})を使用しました。おそらく、これをクリーンアップして、設定したif構造の代わりにwhileを使用することもできますが、少なくともそれは代替方法です。

助けてくれてありがとう。

于 2012-10-01T02:10:43.930 に答える
0

私は今、私たちのマルチストアシステムに対してまったく同じことをしました。

価格セットが1つしかない場合は、フォールバックも追加しました。ユーザーエクスペリエンスを向上させるために、出力をフォーマットしています。

このコードを自由に使用してください:

<script type="text/javascript">
    jQuery(function($){
        // This was built using https://stackoverflow.com/questions/12647770/ and https://himansuboity.wordpress.com/2014/09/30/magento-tip-how-to-get-the-store-price-format-by-javascript/
        var inst_price_format = <?php echo Mage::helper('core')->jsonEncode( Mage::app()->getLocale()->getJsPriceFormat() ); ?>

        var tierPrices = <?php echo json_encode($_product->getTierPrice()) ?>;

        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 updatePrice = function(price, qty) { $('.price').html( formatCurrency( (price * qty), inst_price_format) ); };

        $('#qty').change( function() {
            var price = getPrice(this.value);
            var qty = this.value;
            if(price !== null) { updatePrice(price, qty); }
            // no tier prices set, use base price
            else { updatePrice(<?php echo $product->getPrice(); ?>, qty); }
        });
    });
</script>
于 2016-05-30T10:28:02.150 に答える
0

これはマルチストアでは機能しません。通貨は変更されますが、価格はデフォルトの通貨で表示されます。

于 2016-10-10T18:52:37.553 に答える
0

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

于 2016-11-14T21:21:44.807 に答える