Exemplo n.º 1
0
 private function addItem(ProductInterface $product, $quantity, OrderInterface $cart)
 {
     /** @var OrderItemInterface $item */
     $item = $this->orderItemFactory->createNew();
     $item->setUnitPrice($product->getPrice());
     $item->setProduct($product);
     $this->modifier->modify($item, $quantity);
     $event = new CartItemEvent($cart, $item);
     $this->eventDispatcher->dispatch(SyliusCartEvents::ITEM_ADD_INITIALIZE, $event);
     $this->eventDispatcher->dispatch(SyliusCartEvents::CART_CHANGE, new GenericEvent($cart));
     $this->eventDispatcher->dispatch(SyliusCartEvents::CART_SAVE_INITIALIZE, $event);
     $this->eventDispatcher->dispatch(SyliusCartEvents::ITEM_ADD_COMPLETED, new FlashEvent());
 }
Exemplo n.º 2
0
 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;
 }