17
17
use Magento \Quote \Api \Data \CartInterface ;
18
18
use Magento \Quote \Api \Data \CartItemInterface ;
19
19
use Magento \Quote \Model \Quote \Item ;
20
+ use Psr \Log \LoggerInterface ;
20
21
21
22
class CartQuantityValidator implements CartQuantityValidatorInterface
22
23
{
@@ -32,12 +33,14 @@ class CartQuantityValidator implements CartQuantityValidatorInterface
32
33
* @param StockRegistryInterface $stockRegistry
33
34
* @param Config $config
34
35
* @param ProductRepositoryInterface $productRepository
36
+ * @param LoggerInterface $logger
35
37
*/
36
38
public function __construct (
37
39
private readonly CartItemRepositoryInterface $ cartItemRepository ,
38
- private readonly StockRegistryInterface $ stockRegistry ,
39
- private readonly Config $ config ,
40
- private readonly ProductRepositoryInterface $ productRepository
40
+ private readonly StockRegistryInterface $ stockRegistry ,
41
+ private readonly Config $ config ,
42
+ private readonly ProductRepositoryInterface $ productRepository ,
43
+ private readonly LoggerInterface $ logger
41
44
) {
42
45
}
43
46
@@ -69,8 +72,8 @@ public function validateFinalCartQuantities(CartInterface $customerCart, CartInt
69
72
$ sku = $ this ->getSkuFromItem ($ customerCartItem );
70
73
$ product = $ this ->getProduct ((int ) $ customerCartItem ->getProduct ()->getId ());
71
74
$ isAvailable = $ customerCartItem ->getChildren ()
72
- ? $ this ->validateCompositeProductQty ($ guestCartItem , $ customerCartItem )
73
- : $ this ->validateProductQty ($ product , $ sku , $ guestCartItem ->getQty (), $ customerCartItem ->getQty ());
75
+ ? $ this ->isCompositeProductQtyValid ($ guestCartItem , $ customerCartItem )
76
+ : $ this ->isProductQtyValid ($ product , $ sku , $ guestCartItem ->getQty (), $ customerCartItem ->getQty ());
74
77
75
78
if ($ this ->config ->getCartMergePreference () === Config::CART_PREFERENCE_GUEST ) {
76
79
$ this ->safeDeleteCartItem ((int ) $ customerCart ->getId (), (int ) $ customerCartItem ->getItemId ());
@@ -126,7 +129,7 @@ private function getCurrentCartItemQty(float $guestQty, float $customerQty): flo
126
129
* @param float $customerQty
127
130
* @return bool
128
131
*/
129
- private function validateProductQty (
132
+ private function isProductQtyValid (
130
133
ProductInterface $ product ,
131
134
string $ sku ,
132
135
float $ guestQty ,
@@ -151,7 +154,7 @@ private function validateProductQty(
151
154
* @return bool
152
155
* @throws NoSuchEntityException
153
156
*/
154
- private function validateCompositeProductQty (
157
+ private function isCompositeProductQtyValid (
155
158
Item $ guestCartItem ,
156
159
Item $ customerCartItem
157
160
): bool {
@@ -164,7 +167,7 @@ private function validateCompositeProductQty(
164
167
$ customerItemQty = $ customerCartItem ->getQty () * $ customerChildItem ->getQty ();
165
168
$ guestItemQty = $ guestCartItem ->getQty () * $ guestChildItems [$ sku ]->getQty ();
166
169
167
- if (!$ this ->validateProductQty ($ childProduct , $ sku , $ guestItemQty , $ customerItemQty )) {
170
+ if (!$ this ->isProductQtyValid ($ childProduct , $ sku , $ guestItemQty , $ customerItemQty )) {
168
171
return false ;
169
172
}
170
173
}
@@ -210,8 +213,8 @@ private function safeDeleteCartItem(int $cartId, int $itemId): void
210
213
{
211
214
try {
212
215
$ this ->cartItemRepository ->deleteById ($ cartId , $ itemId );
213
- } catch (NoSuchEntityException |CouldNotSaveException $ e ) { // phpcs:ignore
214
- // Optionally log the error.
216
+ } catch (NoSuchEntityException |CouldNotSaveException $ e ) {
217
+ $ this -> logger -> error ( $ e );
215
218
}
216
219
}
217
220
}
0 commit comments