/** * @param CartInterface $cart * @param CartItemInterface $item */ private function resolveCartItem(CartInterface $cart, CartItemInterface $item) { foreach ($cart->getItems() as $existingItem) { if ($item->equals($existingItem)) { $this->orderItemQuantityModifier->modify($existingItem, $existingItem->getQuantity() + $item->getQuantity()); return; } } $cart->addItem($item); }
function it_changes_quntity_of_item_if_same_cart_item_already_exists(OrderProcessorInterface $orderProcessor, OrderItemQuantityModifierInterface $orderItemQuantityModifier, CartInterface $cart, CartItemInterface $newItem, CartItemInterface $existingItem) { $cart->getItems()->willReturn([$existingItem]); $newItem->equals($existingItem)->willReturn(true); $existingItem->getQuantity()->willReturn(2); $newItem->getQuantity()->willReturn(3); $cart->addItem($existingItem)->shouldNotBeCalled(); $orderItemQuantityModifier->modify($existingItem, 5)->shouldBeCalled(); $orderProcessor->process($cart)->shouldBeCalled(); $this->addToCart($cart, $newItem); }
public function resolve(CartItemInterface $item, $request) { $productId = $request->query->get('productId'); // If no product id given, or product not found, we throw exception with nice message. if (!$productId || !($product = $this->getProductRepository()->find($productId))) { throw new ItemResolvingException('Requested product was not found'); } // Assign the product to the item and define the unit price. $item->setVariant($product); $item->setUnitPrice($product->getPrice()); // Everything went fine, return the item. return $item; }
public function resolve(CartItemInterface $item, $request) { $productId = $request->get('product'); $quantity = intval($request->get('quantity')); if ($quantity < 1) { throw new ItemResolvingException('Quantity must be 1 or higher'); } // If no product id given, or product not found, we throw exception with nice message. if (!$productId || !($product = $this->productRepository->find($productId))) { throw new ItemResolvingException('Requested product was not found'); } /** @var $product ProductInterface */ $item->setUnitPrice($product->getPrice()); /** @var $item OrderItem */ $item->setProduct($product); $this->modifier->modify($item, $quantity); // Everything went fine, return the item. return $item; }