Search code examples
magento

sales_quote_add_item - error on save quote


I have the observer sales_quote_add_item in config.xml which run function below when item is added to cart:

    public function updatePrice( $observer ){

        $event = $observer->getEvent();

        $quote_item = $event->getQuoteItem();

        $new_price = 200;
        $quote_item->setOriginalCustomPrice($new_price);
        $quote_item->setTotalPrice(350);

        $quote_item->save();
}

It's working fine if user is registered but if we are as guest then this line

$quote_item->save();

give error in var/exception.log

2012-10-23T05:12:28+00:00 DEBUG (7): Exception message: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`bluning_mage`.`sales_flat_quote_item`, CONSTRAINT `FK_SALES_QUOTE_ITEM_SALES_QUOTE` FOREIGN KEY (`quote_id`) REFERENCES `sales_flat_quote` (`entity_id`) ON DELETE CASCADE ON UPDATE CASCADE)

How can I solve it?


Solution

  • You should not save quote item object in observer, so just remove this line:

    $quote_item->save();

    It will save the object automatically, as it is being passed by reference.