/** * {@inheritdoc} */ public function addProduct(ProductInterface $product, $indexName = ProductIndexerInterface::DEFAULT_INDEX_NAME) { $index = $this->searchIndexManager->getIndex(ProductIndexerInterface::DEFAULT_INDEX_NAME); $document = new Document(); $document->addField(Field::unIndexed('identifier', $product->getId())); $document->addField(Field::text('name', $product->translate('en')->getName())); $document->addField(Field::text('shortDescription', $product->translate()->getShortDescription())); $document->addField(Field::text('description', $product->translate()->getDescription())); $index->addDocument($document); $index->commit(); }
/** * 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(), 'previousError' => $exception->getPrevious()->getMessage()]); } $category = $product->getCategories()->first(); $recommendations = $this->get('product.helper')->getProductRecommendationsForCategory($category); $basketModalContent = $this->renderView('WellCommerceAppBundle:Front/Cart:add.html.twig', ['product' => $product, 'recommendations' => $recommendations]); $cartPreviewContent = $this->renderView('WellCommerceAppBundle:Front/Common:preview.html.twig'); return $this->jsonResponse(['basketModalContent' => $basketModalContent, 'cartPreviewContent' => $cartPreviewContent]); }
public function __construct(ProductInterface $product, ProductAttributeInterface $attribute = null, $quantity, \Exception $previous) { $message = sprintf('Cannot add item with id: "%s", attribute: "%s" and quantity: "%s" to cart', $product->getId(), null === $attribute ? 0 : $attribute->getId(), $quantity); parent::__construct($message, $previous->getCode(), $previous); }