<?php
namespace App\Controller;
use App\Model\DataObject\Sample;
use App\Model\DataObject\VirtualProduct;
use App\Services\CartService;
use App\Services\CheckoutService;
use App\Services\StockService;
use Elements\Bundle\TrackingBundle\Tracking\Tracker\GoogleAnalytics4;
use Elements\Bundle\TrackingBundle\Tracking\TrackingManager;
use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
use Pimcore\Bundle\EcommerceFrameworkBundle\Model\AbstractProduct;
use Pimcore\Bundle\EcommerceFrameworkBundle\Model\CheckoutableInterface;
use Pimcore\Log\ApplicationLogger;
use Pimcore\Model\DataObject;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class CartController extends AbstractController
{
protected TrackingManager $trackingManager;
public function __construct(ApplicationLogger $applicationLogger, protected CartService $cartService, Factory $factory)
{
parent::__construct($applicationLogger);
$this->trackingManager = $factory->getTrackingManager();
}
/**
* @Route("/{_locale}/cart", name="cart_list")
*/
public function list(Factory $factory): Response
{
$cart = $this->cartService->getCart();
$this->trackingManager->trackCartView($cart);
return $this->render('cart/list.html.twig', [
'cart' => $cart
]);
}
/**
* @Route("/{_locale}/cart/update", name="cart_update")
* @param Request $request
* @return Response
*/
public function update(Request $request): Response
{
$success = false;
$productId = $request->request->get('productId');
$product = DataObject::getById($productId);
if ($product instanceof CheckoutableInterface) {
$this->cartService->updateCartItem($product, $request->request->get('amount', 1));
$success = true;
}
$response = [
'success' => $success,
'count' => $this->cartService->getCartCount(),
'cartContent' => $this->renderView('cart/items.html.twig', [
'cart' => $this->cartService->getCart()
])
];
return $this->json($response, 200, [
'X-Robots-Tag' => 'noindex'
]);
}
/**
* @Route("/{_locale}/cart/reorder/{order}", name="cart_reorder")
*/
public function reorder(DataObject\OnlineShopOrder $order)
{
foreach($order->getItems() as $item) {
$product = $item->getProduct();
if ($product instanceof Sample && $product->isActive()) {
$this->cartService->addToCart($product, $item->getAmount(), $item->getComment());
}
}
$this->cartService->limitItemCountToMax();
return $this->redirectToRoute('cart_list');
}
/**
* @Route("/{_locale}/cart/add", name="cart_add")
*/
public function addToCart(Request $request, StockService $stockService): \Symfony\Component\HttpFoundation\JsonResponse
{
$id = $request->get('id', 0);
$product = DataObject::getById($id);
$success = true;
if ($product instanceof CheckoutableInterface) {
try {
$comment = [
'detailUrl' => $request->get('detailUrl', null)
];
$this->cartService->addToCart($product, (int) $request->get('amount', 1), json_encode($comment));
} catch (\Exception $e) {
$success = false;
}
//TODO: get event to view
$trackingData = [];
foreach ($this->trackingManager->getActiveTrackers() as $tracker) {
if ($tracker instanceof GoogleAnalytics4) {
$trackingData = $tracker->createGA4ProductInfoArray($product, 'add_to_cart', (int)$request->get('amount', 1));
}
}
} else {
$success = false;
}
$response = [
'success' => $success,
'count' => $this->cartService->getCartCount(),
'cartListContent' => $this->renderView('cart/ajax.html.twig', [
'cart' => $this->cartService->getCart()
]),
'detailContent' => $this->renderView('cart/detail/cart-detail-content.html.twig', [
'product' => $product,
'contactButton' => true,
'contactAreaId' => 'contact-form',
'cartButton' => $product !== null,
]),
'trackingData' => $trackingData
];
return $this->json($response, 200, [
'X-Robots-Tag' => 'noindex'
]);
}
/**
* @Route("/{_locale}/cart/delete/{itemKey}", name="cart_delete")
*/
public function delete(string $itemKey) {
$item = $this->cartService->getCart()->getItem($itemKey);
$this->cartService->removeCartItem($itemKey);
$this->trackingManager->trackCartProductActionRemove($this->cartService->getCart(), $item?->getProduct(), $item?->getCount());
$this->trackingManager->forwardTrackedCodesAsFlashMessage();
return $this->redirectToRoute('cart_list');
}
/**
* @Route("/{_locale}/cart/ajax", name="cart_ajax")
*/
public function ajax(): \Symfony\Component\HttpFoundation\JsonResponse
{
return $this->json([
'success' => true,
'count' => $this->cartService->getCartCount(),
'cartListContent' => $this->renderView('cart/ajax.html.twig', [
'cart' => $this->cartService->getCart()
])
], 200, [
'X-Robots-Tag' => 'noindex'
]);
}
}