1

I've the following observer

<events>
  <sales_quote_add_item>
    <observers>
      <priceupdate_observer>
        <type>singleton</type>
        <class>termostore_scontitrovaprezzi/observer</class>
        <method>applyDiscount</method>
      </priceupdate_observer>
    </observers>
  </sales_quote_add_item>
</events>

And the following method

public function applyDiscount(Varien_Event_Observer $observer)
  {
    $event = $observer->getEvent();
    $item = $event->getQuoteItem();
    $product_id = $item->getId();

    function setPrice($new_price){
      $item->setOriginalCustomPrice($new_price);
      $item->save();
    }

    switch($product_id){
        case 22116:
            $new_price = 1000;
            setPrice();
            break;
        case 22076:
            $new_price = 1000;
            setPrice();
            break;
        case 37476:
            $new_price = 500;
            setPrice();
            break;
        case 22115:
            $new_price = 500;
            setPrice();
            break;
        case 22077:
            $new_price = 500;
            setPrice();
            break;
        default:

            break;
    }
}

It doesn't work and I believe the problem is in getting the product ID. What am I doing wrong?

EDIT The error says something like: It's not possible to add the item to the cart.

2 Answers 2

0

got it :) this is how it should be:

$event = $observer->getEvent();
$item = $event->getQuoteItem();
$product_id = $item->getProductId();
0

Try This.

public function applyDiscount(Varien_Event_Observer $observer)
{
    $event = $observer->getEvent();
    $quote_item = $event->getQuoteItem();
    $form_data=Mage::app()->getRequest()->getParams(); 
    $product_id = $quote_item->product_id;
    $new_price=0;// calculated price
    switch($product_id){
        case 22116:
            $new_price = 1000;
            //setPrice();
            break;
        case 22076:
            $new_price = 1000;
            //setPrice();
            break;
        case 37476:
            $new_price = 500;
            //setPrice();
            break;
        case 22115:
            $new_price = 500;
            //setPrice();
            break;
        case 22077:
            $new_price = 500;
            //setPrice();
            break;
        default:
            break;
    } 
    $quote_item->setOriginalCustomPrice($new_price); // its for change item's price
    $quote_item->getProduct()->setIsSuperMode(true); // its for change item's price
    return $this;
}   

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