Dhirendrasinh DRC
Dhirendrasinh DRC

Reputation: 137

How to Change the price before adding to cart in magento with custom option selection price?

I have tried to change price with custom option but it is not working.

For example, my product price is $100 and after selection custom option price is $110 and it is fine, but I want to when I click on Add to cart so, this time, i want to add extra $50 in $110. so finally $160 in cart page.

This is my Observer code for change price. Please guide me something is wrong on code.

class DRC_PriceChange_Model_Observer  {

    public function change_price(Varient_Event_Observer $observer) 
    {    
        $new_price = 50;
        $event = $observer->getEvent();
        $quote_item = $event->getQuoteItem();
        $item->setCustomPrice($new_price);
        $item->setOriginalCustomPrice($new_price);
        $item->getProduct()->setIsSuperMode(true);      
    }
}
?>

Upvotes: 0

Views: 1498

Answers (1)

Dhirendrasinh DRC
Dhirendrasinh DRC

Reputation: 137

I found the solution for this. Use the code given at below:

<?php
class DRC_PriceChange_Model_Observer  {

    public function change_price(Varient_Event_Observer $observer) 
    {   
        $item = $observer->getQuoteItem();
        if ($item->getParentItem()) {$item = $item->getParentItem();}
        $price = $item->getProduct()->getFinalPrice();
        $new_price = 20 + $price;

        $item->setCustomPrice($new_price);
        $item->setOriginalCustomPrice($new_price);
        $item->getProduct()->setIsSuperMode(true);      
    }
}
?>

Upvotes: 1

Related Questions