コード例 #1
0
 /**
  * Adds item to cart or redirects to quick-view
  *
  * @param ProductInterface               $product
  * @param ProductAttributeInterface|null $attribute
  * @param int                            $quantity
  *
  * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse
  */
 public function addAction(ProductInterface $product, ProductAttributeInterface $attribute = null, $quantity = 1)
 {
     if ($product->getAttributes()->count() && !$product->getAttributes()->contains($attribute)) {
         return $this->redirectToRoute('front.product.view', ['id' => $product->getId()]);
     }
     try {
         $this->manager->addProductToCart($product, $attribute, $quantity);
     } catch (AddCartItemException $exception) {
         return $this->jsonResponse(['error' => $exception->getMessage()]);
     }
     $category = $product->getCategories()->first();
     $recommendations = $this->manager->getProductProvider()->getProductRecommendationsForCategory($category);
     $basketModalContent = $this->renderView('WellCommerceCartBundle:Front/Cart:add.html.twig', ['product' => $product, 'recommendations' => $recommendations]);
     $cartPreviewContent = $this->renderView('WellCommerceCartBundle:Front/Common:preview.html.twig');
     return $this->jsonResponse(['basketModalContent' => $basketModalContent, 'cartPreviewContent' => $cartPreviewContent]);
 }