public function updateOfferFromCart(OnlineShop_OfferTool_AbstractOffer $offer, OnlineShop_Framework_ICart $cart, array $excludeItems = array(), $save = true) { $excludedItemKeys = $this->getExcludedItemKeys($excludeItems); if ($cart->getId() != $offer->getCartId()) { throw new Exception("Cart does not match to the offer given, update is not possible"); } //Update existing offer items $offerItems = $offer->getItems(); $newOfferItems = array(); foreach ($offerItems as $offerItem) { $cartItem = $cart->getItem($offerItem->getCartItemKey()); if ($cartItem && !$excludedItemKeys[$offerItem->getCartItemKey()]) { $newOfferItems[$offerItem->getCartItemKey()] = $this->updateOfferItem($cartItem, $offerItem); } } //Add non existing cart items to offer $cartItems = $cart->getItems(); foreach ($cartItems as $cartItem) { if (!$newOfferItems[$cartItem->getItemKey()] && !$excludedItemKeys[$cartItem->getItemKey()]) { $offerItem = $this->createOfferItem($cartItem, $offer); $newOfferItems[$offerItem->getCartItemKey()] = $offerItem; } } //Delete offer items which are not needed any more foreach ($offerItems as $offerItem) { if (!$newOfferItems[$offerItem->getCartItemKey()]) { $offerItem->delete(); } } $offer->setItems($newOfferItems); //Update total price $offer = $this->updateTotalPriceOfOffer($offer); if ($save) { $offer->save(); } return $offer; }