-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] Listen to ProcessOrderCheckStock event
Relates: #138
- Loading branch information
Showing
4 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
Classes/Domain/DoctrineRepository/Product/BeVariantRepository.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Extcode\CartProducts\Domain\DoctrineRepository\Product; | ||
|
||
/* | ||
* This file is part of the package extcode/cart-products. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
use TYPO3\CMS\Core\Database\ConnectionPool; | ||
|
||
class BeVariantRepository | ||
{ | ||
public function __construct( | ||
private readonly ConnectionPool $connectionPool | ||
) {} | ||
|
||
public function getStock(int $uid): int | ||
{ | ||
$productConnection = $this->connectionPool->getConnectionForTable('tx_cartproducts_domain_model_product_bevariant'); | ||
|
||
$productQueryBuilder = $productConnection->createQueryBuilder(); | ||
|
||
return $productQueryBuilder | ||
->select('stock') | ||
->from('tx_cartproducts_domain_model_product_bevariant') | ||
->where( | ||
$productQueryBuilder->expr()->eq('uid', $productQueryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)) | ||
)->executeQuery()->fetchOne(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
Classes/Domain/DoctrineRepository/Product/ProductRepository.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Extcode\CartProducts\Domain\DoctrineRepository\Product; | ||
|
||
/* | ||
* This file is part of the package extcode/cart-products. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
use TYPO3\CMS\Core\Database\ConnectionPool; | ||
|
||
class ProductRepository | ||
{ | ||
public function __construct( | ||
private readonly ConnectionPool $connectionPool | ||
) {} | ||
|
||
public function getStock(int $uid): int | ||
{ | ||
$productConnection = $this->connectionPool->getConnectionForTable('tx_cartproducts_domain_model_product_product'); | ||
|
||
$productQueryBuilder = $productConnection->createQueryBuilder(); | ||
|
||
return $productQueryBuilder | ||
->select('stock') | ||
->from('tx_cartproducts_domain_model_product_product') | ||
->where( | ||
$productQueryBuilder->expr()->eq('uid', $productQueryBuilder->createNamedParameter($uid, \PDO::PARAM_INT)) | ||
)->executeQuery()->fetchOne(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Extcode\CartProducts\EventListener\Order\Stock; | ||
|
||
/* | ||
* This file is part of the package extcode/cart-products. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
use Extcode\Cart\Domain\Model\Cart\BeVariant as CartProductBeVariant; | ||
use Extcode\Cart\Domain\Model\Cart\Product as CartProduct; | ||
use Extcode\Cart\Event\ProcessOrderCheckStockEvent; | ||
use Extcode\CartProducts\Domain\DoctrineRepository\Product\BeVariantRepository; | ||
use Extcode\CartProducts\Domain\DoctrineRepository\Product\ProductRepository; | ||
use TYPO3\CMS\Core\Messaging\FlashMessage; | ||
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
use TYPO3\CMS\Extbase\Utility\LocalizationUtility; | ||
|
||
class CheckStock | ||
{ | ||
private ProcessOrderCheckStockEvent $event; | ||
|
||
public function __construct( | ||
private readonly ProductRepository $productRepository, | ||
private readonly BeVariantRepository $beVariantRepository, | ||
) {} | ||
|
||
public function __invoke(ProcessOrderCheckStockEvent $event): void | ||
{ | ||
$this->event = $event; | ||
$cart = $event->getCart(); | ||
$cartProducts = $cart->getProducts(); | ||
|
||
foreach ($cartProducts as $cartProduct) { | ||
if ($cartProduct->getProductType() !== 'CartProducts' || $cartProduct->isHandleStock() === false) { | ||
continue; | ||
} | ||
|
||
if ($cartProduct->isHandleStockInVariants()) { | ||
foreach ($cartProduct->getBeVariants() as $cartProductBeVariant) { | ||
$this->checkStockForBackendVariant($cartProductBeVariant, $cartProduct); | ||
} | ||
} else { | ||
$this->checkStockForProduct($cartProduct); | ||
} | ||
} | ||
} | ||
|
||
private function checkStockForProduct(CartProduct $cartProduct): void | ||
{ | ||
$quantityInStock = $this->productRepository->getStock($cartProduct->getProductId()); | ||
|
||
if ($quantityInStock < $cartProduct->getQuantity()) { | ||
$this->falseAvailability($this->event, $cartProduct->getTitle(), $cartProduct->getSku(), $quantityInStock); | ||
} | ||
} | ||
|
||
public function checkStockForBackendVariant(CartProductBeVariant $cartProductBeVariant, CartProduct $cartProduct): void | ||
{ | ||
$quantityInStock = $this->beVariantRepository->getStock($cartProductBeVariant->getId()); | ||
|
||
if ($quantityInStock < $cartProductBeVariant->getQuantity()) { | ||
$this->falseAvailability($this->event, $cartProduct->getTitle(), $cartProductBeVariant->getSku(), $quantityInStock); | ||
} | ||
} | ||
|
||
private function falseAvailability( | ||
ProcessOrderCheckStockEvent $event, | ||
string $title, | ||
string $sku, | ||
int $quantityInStock | ||
): void { | ||
$event->setNotEveryProductAvailable(); | ||
$event->addInsufficientStockMessage( | ||
GeneralUtility::makeInstance( | ||
FlashMessage::class, | ||
LocalizationUtility::translate( | ||
'tx_cart.error.stock_handling.order', | ||
'cart', | ||
[$title, $sku, $quantityInStock] | ||
), | ||
'', | ||
ContextualFeedbackSeverity::ERROR | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters