dtz30833 2013-01-26 05:19
浏览 34
已采纳

如何在magento中动态更改价格值

I am a beginner in Magento. I want to change the price value in product detail page dynamically using ajax. In the meantime i want to calculate this price value in the cart page also

Refer this URL: http://dev.tangoprint.ch/magento/index.php/plakate/a1.html

This page contains a calculator to calculate a price value and dynamically I want to change price in the cart page as well. Please refer this image: enter image description here

Any suggestions will be appreciated.

  • 写回答

1条回答 默认 最新

  • dougua3705 2013-01-26 06:03
    关注

    Step 1 - include jQuery in page.xml (app/design/frontend/mytheme/default/layout/page.xml)

    <action method="addJs"><script>jquery/jquery-1.5.2.no-conflict.min.js</script></action>
    

    Step 2 - add price calculation php page (/myscripts/ajaxPriceCal.php)

    <?php
    include_once '../app/Mage.php';
    Mage::app();
    
    if(isset($_POST['qty']) && !empty($_POST['qty'])){
    
        $product_id = $_POST['pid'];
        $my_qty = $_POST['qty'];
        $my_price = 0;
    
        $_product = Mage::getModel('catalog/product')->load($product_id);
    
        $_tierPrices = $_product->tier_price;
    
        $_tierPrices = array_reverse($_tierPrices);
    
        for($i=0; $i < count($_tierPrices); $i++){
            if($my_qty >= $_tierPrices[$i]['price_qty']){
                $my_price = $_tierPrices[$i]['price'];        
                break;
            }
        }
    
        $calculated_price = $my_price*$my_qty;
    
        echo number_format($calculated_price,2,'.',',');
    }
    ?>
    

    Step 3 - modify tier price qty text option page (app/design/frontend/mytheme/default/template/catalog/product/view/options/type/text.phtml)

    add following script to the very beginning of text.phtml page

    <script type="text/javascript">
    $j = jQuery.noConflict();
    
    function get_total_qty(){
        var qty = parseInt(0);
        var qty = $("#calculator_qty").val();
        /*
        * AJAX call
        */
        var quantity = parseInt($j('#qty').val()) + parseInt(qty); // get final quantity
        var product_id = $j('#prod_id').val(); // get product id
        $j.post("/magento/scripts/ajaxPriceCal.php", { qty: quantity, pid: product_id },
            function(data){
                $j('.price').html(data);
        });
    
    }
    
    $j(document).ready(function(){
        $j('.calculate').click(function(){
            if($("#calculator_qty").val()){
                get_total_qty();
            }
        });    
    });
    </script>
    

    This will help you. You are changing the price value and when you do add to cart it will reflect in cart.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!