|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Omikron\FactFinder\Shopware6\Storefront\Controller; |
| 6 | + |
| 7 | +use Shopware\Core\Checkout\Cart\LineItem\LineItem; |
| 8 | +use Shopware\Core\Checkout\Cart\SalesChannel\CartService; |
| 9 | +use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity; |
| 10 | +use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; |
| 11 | +use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; |
| 12 | +use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter; |
| 13 | +use Shopware\Core\System\SalesChannel\SalesChannelContext; |
| 14 | +use Shopware\Storefront\Controller\StorefrontController; |
| 15 | +use Symfony\Component\HttpFoundation\RedirectResponse; |
| 16 | +use Symfony\Component\HttpFoundation\Request; |
| 17 | +use Symfony\Component\Routing\Annotation\Route; |
| 18 | + |
| 19 | +#[Route(defaults: ['_routeScope' => ['storefront']])] |
| 20 | +class AddToCartController extends StorefrontController |
| 21 | +{ |
| 22 | + public function __construct( |
| 23 | + private readonly CartService $cartService, |
| 24 | + private readonly EntityRepository $productRepository, |
| 25 | + ) { |
| 26 | + } |
| 27 | + |
| 28 | + #[Route( |
| 29 | + '/fact-finder/cart/add-by-number/{productNumber}', |
| 30 | + name: 'frontend.cart.add.by.number', |
| 31 | + defaults: ['XmlHttpRequest' => true], |
| 32 | + methods: ['GET', 'POST'] |
| 33 | + )] |
| 34 | + public function addToCartByNumber(string $productNumber, Request $request, SalesChannelContext $context): RedirectResponse |
| 35 | + { |
| 36 | + $referer = $request->headers->get('referer'); |
| 37 | + $criteria = new Criteria(); |
| 38 | + $criteria->addFilter(new EqualsFilter('productNumber', $productNumber)); |
| 39 | + $criteria->setLimit(1); |
| 40 | + |
| 41 | + /** @var SalesChannelProductEntity|null $product */ |
| 42 | + $product = $this->productRepository->search($criteria, $context->getContext())->first(); |
| 43 | + |
| 44 | + if (!$product) { |
| 45 | + $this->addFlash('danger', "Invalid product number: $productNumber"); |
| 46 | + |
| 47 | + return $this->redirectToRoute('frontend.home.page'); |
| 48 | + } |
| 49 | + |
| 50 | + $lineItem = (new LineItem($product->getId(), LineItem::PRODUCT_LINE_ITEM_TYPE, $product->getId(), 1)) |
| 51 | + ->setStackable(true) |
| 52 | + ->setRemovable(true); |
| 53 | + $cart = $this->cartService->getCart($context->getToken(), $context); |
| 54 | + $this->cartService->add($cart, $lineItem, $context); |
| 55 | + $this->addFlash('success', "Product {$product->getName()} was added to the cart successfully."); |
| 56 | + |
| 57 | + return $this->redirectToRefererOrToHomePage($referer); |
| 58 | + } |
| 59 | + |
| 60 | + private function redirectToRefererOrToHomePage(?string $referer): RedirectResponse |
| 61 | + { |
| 62 | + if ($referer && filter_var($referer, FILTER_VALIDATE_URL)) { |
| 63 | + return new RedirectResponse($referer); |
| 64 | + } |
| 65 | + |
| 66 | + return $this->redirectToRoute('frontend.home.page'); |
| 67 | + } |
| 68 | +} |
0 commit comments