Ashish Sharma
Ashish Sharma

Reputation: 81

Magento: Change product price by getting product id

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?

Upvotes: 1

Views: 2412

Answers (2)

Gerard de Visser
Gerard de Visser

Reputation: 8050

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...

Upvotes: 2

Tejas Shah
Tejas Shah

Reputation: 563

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.

Upvotes: 0

Related Questions