/** * @param Request $request * @return CartItemInterface */ public function resolve(Request $request) { if (!($id = $request->get('id'))) { throw new ItemResolvingException('Error while trying to add item to cart.'); } // Create cart item $item = $this->cartItemFactory->createNew(); $channel = $this->channelContext->getChannel(); if (!($product = $this->productRepository->findOneByIdAndChannel($id, $channel))) { throw new ItemResolvingException('Requested product was not found.'); } // We use forms to easily set the quantity and pick variant, // the same way Sylius does it while resolving products when adding to cart $form = $this->formFactory->create('sylius_cart_item', $item, array('product' => $product)); $form->submit($request); // Get appropriate variant $variant = $item->getVariant() ? $item->getVariant() : $product->getMasterVariant(); return $variant; }