1

My Magento version -> 1.6.2

I am using an external php file that receives the $product_id and $my_price parameter via jQuery post.

var priceNewValue = XX; // My custom price value
var product_id = optionsPrice.productId; // Product id

jQuery.post("http://flyingcakes.in/eshop/ajaxPriceCal.php", { price: priceNewValue, pid: product_id });

On my "ajaxPriceCal.php" page, I catch the values:

$product_id = $_POST['pid'];
$my_price = $_POST['price'];    

Now I want Magento to set the price of this product ($product_id) equal to $my_price. So that:

  1. This changed price is reflected when product is added to the cart.
  2. The price of the product is changed only temporarily i.e. not saved to the database.

How should I do this?

Ashish Sharma
  • 83
  • 3
  • 14
  • 1
    you cant do this way you have to create one module for that http://magento.stackexchange.com/questions/4318/dynamically-calculated-prices-save-before-add-to-cart this link help you – Keyur Shah Dec 20 '13 at 09:09

2 Answers2

2

You have to build an observer that catches the add-to-cart event sales_quote_add_item and then you can do the php-stuff in the observer to change the price for only this product with $observer->getEvent()->getQuoteItem()->setOriginalCustomPrice([your price]).

Its explained in more detail on this page: Changing the price in quote while adding product to cart: magento.

This worked for me...

Community
  • 1
  • 1
Gerard de Visser
  • 7,590
  • 9
  • 50
  • 58
  • I confirm you should use this kind of solution. How is used the Ajax call ? What are conditions to use the special price ? Couldn't you consider just using an observer (on add to cart event). – dagfr Dec 20 '13 at 13:45
  • @gerard thanks! Might I ask, is there any way that I can tell the Observer.php to set a price that that is equal to what I calculate on the front-end using Jquery? I mean, I want to pass the value of the price to the observer and not calculate it in observer.php, because, the value of the price is dependent upon what customer selects via dropdowns and check-boxes. – Ashish Sharma Dec 21 '13 at 06:44
  • I would not recommend sending the javascriptprice to the observer and use that value for the price because of security reasons. It might be manipulable. It's better to calculate it in PHP and get the price on frontend with Ajax than doing it the other way round, like you want. – Gerard de Visser Dec 24 '13 at 17:43
0

You need to load the product as -

$_product=Mage::getModel('catalog/product')->load($product_id);
$_product->setPrice($my_price);
$_product-Save();

Hope this will help you.

Tejas Shah
  • 563
  • 1
  • 4
  • 12