0

I need to set the price of a product dynamically (based on the value of a variable that I will populate) when the catalog and the product page are loaded.

Currently I use this code in the Module of a catalog_controller_product_view observer:

$event = $observer->getEvent();
$product = $event->getProduct();
$product_id = $product->getId();

function setPrice($item, $new_price){
  $product->setOriginalCustomPrice($new_price);
  $product->save();
}
switch($product_id){
  case 22116:
    $new_price = 9999;
    setPrice($item, $new_price);
    break;
  case 22076:
    $new_price = 9999;
    setPrice($item, $new_price);
    break;
  default:
    break;
}

The code cause a 404 error page. Any idea why? Is the catalog_controller_product_view event the right one to use?

2
  • Please check system.log and exception.log file. and you want to change price for added to cart only or update price for this product permanently.
    – Nits
    Sep 19, 2016 at 13:23
  • I don't want to modify the price permanently in magento, but just display a modified price and keep the modified price when the product is added to cart.
    – davideghz
    Sep 19, 2016 at 13:26

2 Answers 2

0

As per your requirement, there is no need to save that price using save function at product view page load. just follow step or instruction.

1) In catalog/product/view.phtml display price according to your populated dynamic variable.

2) Update this price in cart while add to cart event using observer and checkout_cart_product_add_after event(check this in below reference URL answer).
http://stackoverflow.com/questions/7270261/magento-how-to-change-item-price-when-adding-it-into-the-cart

I hope this will help you.

5
  • just to clarify: does save() function save the price permanently?
    – davideghz
    Sep 19, 2016 at 13:42
  • Yes dear in catalog :)
    – Nits
    Sep 19, 2016 at 13:44
  • does it change it only for the active user/session of for everybody/everywhere? I would like to change the price everywhere just for some specific sessions.. I think your suggested approach works but I would like to do it all - if possible - in one observer ...
    – davideghz
    Sep 19, 2016 at 13:46
  • Above my solution is update price for particular user/customer only. and your solution will update price for all everywhere and everybody.
    – Nits
    Sep 19, 2016 at 13:49
  • I didn't want to add logic to the templates file so I ended up using two observers: catalog_product_load_after and priceupdate_observer for editing the price on product pages and on cart..now I miss the third one for the catalog view...
    – davideghz
    Sep 19, 2016 at 14:40
1

Please use catalog_product_collection_load_after instead of catalog_controller_product_view. then put your logic.

Aslo see here it might help you.

1
  • the names of the two events you mentioned looks similar :)
    – davideghz
    Sep 19, 2016 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.