VIVEK-MDU
VIVEK-MDU

Reputation: 2559

How to change price value dynamically in 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.

Upvotes: 0

Views: 7072

Answers (1)

Sahal
Sahal

Reputation: 4136

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.

Upvotes: 2

Related Questions