Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 88 additions & 75 deletions Classes/Controller/Cart/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Extcode\Cart\Event\CheckProductAvailabilityEvent;
use Extcode\Cart\Event\RetrieveProductsFromRequestEvent;
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Core\Messaging\AbstractMessage;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;

Expand All @@ -33,7 +33,6 @@ public function addAction(): ResponseInterface
$this->restoreSession();

$event = new RetrieveProductsFromRequestEvent($this->request, $this->cart);

$this->eventDispatcher->dispatch($event);

$errors = $event->getErrors();
Expand All @@ -49,46 +48,8 @@ public function addAction(): ResponseInterface
}
}

$messageBody = '';
$messageTitle = '';
$severity = ContextualFeedbackSeverity::OK->value;

if (!empty($errors)) {
foreach ($errors as $error) {
if (!($error instanceof AbstractMessage)) {
continue;
}
$message = $error->jsonSerialize();
if ($message['severity'] >= $severity) {
$severity = $message['severity'];
$messageBody = $message['message'];
$messageTitle = $message['title'];
}
}

$pageType = $GLOBALS['TYPO3_REQUEST']->getAttribute('routing')->getPageType();
if ($pageType === self::AJAX_CART_TYPE_NUM) {
$response = [
'status' => '412',
'count' => $this->cart->getCount(),
'net' => $this->cart->getNet(),
'gross' => $this->cart->getGross(),
'messageBody' => $messageBody,
'messageTitle' => $messageTitle,
'severity' => $severity,
];

return $this->jsonResponse(json_encode($response));
}

$this->addFlashMessage(
$messageBody,
$messageTitle,
$severity,
true
);

return $this->redirect('show', 'Cart\Cart');
if (empty($errors) === false) {
return $this->responseForAddActionWithErrors($errors);
}

$quantity = $this->addProductsToCart($cartProducts);
Expand All @@ -97,39 +58,7 @@ public function addAction(): ResponseInterface

$this->sessionHandler->writeCart($this->settings['cart']['pid'], $this->cart);

$messageBody = LocalizationUtility::translate(
'tx_cart.success.stock_handling.add.' . ($quantity == 1 ? 'one' : 'more'),
'Cart',
[$quantity]
);

$pageType = $GLOBALS['TYPO3_REQUEST']->getAttribute('routing')->getPageType();
if ($pageType === self::AJAX_CART_TYPE_NUM) {
$productsChanged = $this->getChangedProducts($cartProducts);

$response = [
'status' => '200',
'added' => $quantity,
'count' => $this->cart->getCount(),
'net' => $this->cart->getNet(),
'gross' => $this->cart->getGross(),
'productsChanged' => $productsChanged,
'messageBody' => $messageBody,
'messageTitle' => $messageTitle,
'severity' => $severity,
];

return $this->jsonResponse(json_encode($response));
}

$this->addFlashMessage(
$messageBody,
$messageTitle,
$severity,
true
);

return $this->redirect('show', 'Cart\Cart');
return $this->responseForAddAction($cartProducts, $quantity);
}

public function removeAction(): ResponseInterface
Expand Down Expand Up @@ -167,6 +96,7 @@ protected function getChangedProducts(array $products): array
$productsChanged[$product->getId()] = $productChanged->toArray();
}
}

return $productsChanged;
}

Expand All @@ -180,6 +110,89 @@ protected function addProductsToCart(array $products): int
$this->cart->addProduct($product);
}
}

return $quantity;
}

/**
* @param Product[] $cartProducts
*/
private function responseForAddAction(array $cartProducts, int $quantity): ResponseInterface
{
$messageBody = LocalizationUtility::translate(
'tx_cart.success.stock_handling.add.' . ($quantity == 1 ? 'one' : 'more'),
'Cart',
[$quantity]
);

$pageType = $GLOBALS['TYPO3_REQUEST']->getAttribute('routing')->getPageType();
if ($pageType === self::AJAX_CART_TYPE_NUM) {
$response = [
'status' => '200',
'added' => $quantity,
'count' => $this->cart->getCount(),
'net' => $this->cart->getNet(),
'gross' => $this->cart->getGross(),
'productsChanged' => $this->getChangedProducts($cartProducts),
'messageBody' => $messageBody,
'messageTitle' => '',
'severity' => ContextualFeedbackSeverity::OK->value,
];

return $this->jsonResponse(json_encode($response));
}

$this->addFlashMessage(
$messageBody
);

return $this->redirect('show', 'Cart\Cart');
}

/**
* @param FlashMessage[] $errors
*/
private function responseForAddActionWithErrors(array $errors): ResponseInterface
{
$errorWithHighestSeverity = $this->getErrorWithHighestSeverity($errors);

$pageType = $GLOBALS['TYPO3_REQUEST']->getAttribute('routing')->getPageType();
if ($pageType === self::AJAX_CART_TYPE_NUM) {
$response = [
'status' => '412',
'count' => $this->cart->getCount(),
'net' => $this->cart->getNet(),
'gross' => $this->cart->getGross(),
'messageBody' => $errorWithHighestSeverity->getMessage(),
'messageTitle' => $errorWithHighestSeverity->getTitle(),
'severity' => $errorWithHighestSeverity->getSeverity(),
];

return $this->jsonResponse(json_encode($response));
}

$this->addFlashMessage(
$errorWithHighestSeverity->getMessage(),
$errorWithHighestSeverity->getTitle(),
$errorWithHighestSeverity->getSeverity(),
);

return $this->redirect('show', 'Cart\Cart');
}

/**
* @param FlashMessage[] $errors
*/
private function getErrorWithHighestSeverity(array $errors): FlashMessage
{
$errorToReturn = array_shift($errors);

foreach ($errors as $error) {
if ($error->getSeverity()->value >= $errorToReturn->getSeverity()->value) {
$errorToReturn = $error;
}
}

return $errorToReturn;
}
}
3 changes: 3 additions & 0 deletions Classes/Event/RetrieveProductsFromRequestEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public function addError(FlashMessage $error): void
$this->errors[] = $error;
}

/**
* @return FlashMessage[]
*/
public function getErrors(): array
{
return $this->errors;
Expand Down
2 changes: 1 addition & 1 deletion Documentation/guides.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
interlink-shortcode="extcode/cart"
/>
<project title="Cart"
release="11.3.0"
release="11.3.1"
version="11.3"
copyright="2018 - 2025"
/>
Expand Down
Loading