Skip to content

Commit 2d2b3a1

Browse files
committed
Fixed issue where discounts have not been returned for paginated cart items
1 parent d0b7e7b commit 2d2b3a1

File tree

4 files changed

+102
-110
lines changed

4 files changed

+102
-110
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*
6+
* NOTICE: All information contained herein is, and remains
7+
* the property of Adobe and its suppliers, if any. The intellectual
8+
* and technical concepts contained herein are proprietary to Adobe
9+
* and its suppliers and are protected by all applicable intellectual
10+
* property laws, including trade secret and copyright laws.
11+
* Dissemination of this information or reproduction of this material
12+
* is strictly forbidden unless prior written permission is obtained from
13+
* Adobe.
14+
*/
15+
declare(strict_types=1);
16+
17+
namespace Magento\QuoteGraphQl\Model\CartItem;
18+
19+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
20+
use Magento\Framework\GraphQl\Query\Uid;
21+
use Magento\Quote\Api\Data\CartItemInterface;
22+
23+
class GetItemsData
24+
{
25+
/**
26+
* @param Uid $uidEncoder
27+
*/
28+
public function __construct(
29+
private readonly Uid $uidEncoder,
30+
) {
31+
}
32+
33+
/**
34+
* Retrieve cart items data
35+
*
36+
* @param CartItemInterface[] $cartItems
37+
* @return array
38+
*/
39+
public function execute(array $cartItems): array
40+
{
41+
$itemsData = [];
42+
foreach ($cartItems as $cartItem) {
43+
$product = $cartItem->getProduct();
44+
if ($product === null) {
45+
$itemsData[] = new GraphQlNoSuchEntityException(
46+
__("The product that was requested doesn't exist. Verify the product and try again.")
47+
);
48+
continue;
49+
}
50+
$productData = $product->getData();
51+
$productData['model'] = $product;
52+
$productData['uid'] = $this->uidEncoder->encode((string) $product->getId());
53+
54+
$itemsData[] = [
55+
'id' => $cartItem->getItemId(),
56+
'uid' => $this->uidEncoder->encode((string) $cartItem->getItemId()),
57+
'quantity' => $cartItem->getQty(),
58+
'product' => $productData,
59+
'model' => $cartItem,
60+
];
61+
}
62+
return $itemsData;
63+
}
64+
}

app/code/Magento/QuoteGraphQl/Model/CartItem/GetPaginatedCartItems.php

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
namespace Magento\QuoteGraphQl\Model\CartItem;
1818

19-
use Magento\Catalog\Api\Data\ProductInterface;
20-
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
2119
use Magento\Quote\Model\Quote;
2220
use Magento\Quote\Model\ResourceModel\Quote\Item\CollectionFactory as ItemCollectionFactory;
2321

@@ -27,35 +25,13 @@
2725
class GetPaginatedCartItems
2826
{
2927
/**
30-
* @param ProductCollectionFactory $productCollectionFactory
3128
* @param ItemCollectionFactory $itemCollectionFactory
3229
*/
3330
public function __construct(
34-
private readonly ProductCollectionFactory $productCollectionFactory,
3531
private readonly ItemCollectionFactory $itemCollectionFactory
3632
) {
3733
}
3834

39-
/**
40-
* Get product models based on items in cart
41-
*
42-
* @param array $cartProductsIds
43-
* @return ProductInterface[]
44-
*/
45-
private function getCartProduct(array $cartProductsIds): array
46-
{
47-
if (empty($cartProductsIds)) {
48-
return [];
49-
}
50-
/** @var \Magento\Framework\Data\Collection $productCollection */
51-
$productCollection = $this->productCollectionFactory->create()
52-
->addAttributeToSelect('*')
53-
->addIdFilter($cartProductsIds)
54-
->setFlag('has_stock_status_filter', true);
55-
56-
return $productCollection->getItems();
57-
}
58-
5935
/**
6036
* Get visible cart items and product data for cart items
6137
*
@@ -68,9 +44,11 @@ private function getCartProduct(array $cartProductsIds): array
6844
*/
6945
public function execute(Quote $cart, int $pageSize, int $offset, string $orderBy, string $order): array
7046
{
71-
$result = [];
7247
if (!$cart->getId()) {
73-
return $result;
48+
return [
49+
'total' => 0,
50+
'items' => []
51+
];
7452
}
7553
/** @var \Magento\Framework\Data\Collection $itemCollection */
7654
$itemCollection = $this->itemCollectionFactory->create()
@@ -81,20 +59,19 @@ public function execute(Quote $cart, int $pageSize, int $offset, string $orderBy
8159
->setPageSize($pageSize);
8260

8361
$items = [];
84-
$cartProductsIds = [];
8562
$itemDeletedCount = 0;
8663
/** @var \Magento\Quote\Model\Quote\Item $item */
8764
foreach ($itemCollection->getItems() as $item) {
8865
if (!$item->isDeleted()) {
8966
$items[] = $item;
90-
$cartProductsIds[] = $item->getProduct()->getId();
9167
} else {
9268
$itemDeletedCount++;
9369
}
9470
}
95-
$result['total'] = $itemCollection->getSize() - $itemDeletedCount;
96-
$result['items'] = $items;
97-
$result['products'] = $this->getCartProduct($cartProductsIds);
98-
return $result;
71+
72+
return [
73+
'total' => $itemCollection->getSize() - $itemDeletedCount,
74+
'items' => $items
75+
];
9976
}
10077
}

app/code/Magento/QuoteGraphQl/Model/Resolver/CartItems.php

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
use Magento\Framework\GraphQl\Query\ResolverInterface;
1414
use Magento\Framework\GraphQl\Query\Uid;
1515
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
use Magento\Quote\Api\Data\CartInterface;
1617
use Magento\Quote\Model\Quote\Item as QuoteItem;
18+
use Magento\QuoteGraphQl\Model\CartItem\GetItemsData;
1719

1820
/**
1921
* @inheritdoc
@@ -22,9 +24,11 @@ class CartItems implements ResolverInterface
2224
{
2325
/**
2426
* @param Uid $uidEncoder
27+
* @param GetItemsData $getItemsData
2528
*/
2629
public function __construct(
2730
private readonly Uid $uidEncoder,
31+
private readonly GetItemsData $getItemsData
2832
) {
2933
}
3034

@@ -36,32 +40,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
3640
if (!isset($value['model'])) {
3741
throw new LocalizedException(__('"model" value should be specified'));
3842
}
43+
/** @var CartInterface $cart */
3944
$cart = $value['model'];
40-
41-
$itemsData = [];
4245
$cartItems = $cart->getAllVisibleItems();
4346

44-
/** @var QuoteItem $cartItem */
45-
foreach ($cartItems as $cartItem) {
46-
$product = $cartItem->getProduct();
47-
if ($product === null) {
48-
$itemsData[] = new GraphQlNoSuchEntityException(
49-
__("The product that was requested doesn't exist. Verify the product and try again.")
50-
);
51-
continue;
52-
}
53-
$productData = $product->getData();
54-
$productData['model'] = $product;
55-
$productData['uid'] = $this->uidEncoder->encode((string) $product->getId());
56-
57-
$itemsData[] = [
58-
'id' => $cartItem->getItemId(),
59-
'uid' => $this->uidEncoder->encode((string) $cartItem->getItemId()),
60-
'quantity' => $cartItem->getQty(),
61-
'product' => $productData,
62-
'model' => $cartItem,
63-
];
64-
}
65-
return $itemsData;
47+
return $this->getItemsData->execute($cartItems);
6648
}
6749
}

app/code/Magento/QuoteGraphQl/Model/Resolver/CartItemsPaginated.php

Lines changed: 23 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,31 @@
1616

1717
namespace Magento\QuoteGraphQl\Model\Resolver;
1818

19-
use Magento\Catalog\Model\Product;
2019
use Magento\Framework\Exception\LocalizedException;
2120
use Magento\Framework\GraphQl\Config\Element\Field;
2221
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
23-
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
2422
use Magento\Framework\GraphQl\Query\ResolverInterface;
2523
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
24+
use Magento\Quote\Api\Data\CartItemInterface;
2625
use Magento\Quote\Model\Quote;
26+
use Magento\QuoteGraphQl\Model\CartItem\GetItemsData;
2727
use Magento\QuoteGraphQl\Model\CartItem\GetPaginatedCartItems;
28-
use Magento\Framework\GraphQl\Query\Uid;
2928

3029
/**
3130
* @inheritdoc
3231
*/
3332
class CartItemsPaginated implements ResolverInterface
3433
{
3534
private const SORT_ORDER_BY = 'item_id';
36-
3735
private const SORT_ORDER = 'ASC';
3836

3937
/**
4038
* @param GetPaginatedCartItems $pagination
41-
* @param Uid $uidEncoder
39+
* @param GetItemsData $getItemsData
4240
*/
4341
public function __construct(
4442
private readonly GetPaginatedCartItems $pagination,
45-
private readonly Uid $uidEncoder
43+
private readonly GetItemsData $getItemsData
4644
) {
4745
}
4846

@@ -69,41 +67,29 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6967
$orderBy = mb_strtolower($args['sort']['field']);
7068
}
7169

72-
$itemsData = [];
73-
try {
74-
$paginatedCartItems = $this->pagination->execute($cart, $pageSize, (int) $offset, $orderBy, $order);
75-
$cartProductsData = $this->getCartProductsData($paginatedCartItems['products']);
70+
$allVisibleItems = $cart->getAllVisibleItems();
71+
$paginatedCartItems = $this->pagination->execute($cart, $pageSize, (int) $offset, $orderBy, $order);
7672

77-
foreach ($paginatedCartItems['items'] as $cartItem) {
78-
$productId = $cartItem->getProduct()->getId();
79-
if (!isset($cartProductsData[$productId])) {
80-
$itemsData[] = new GraphQlNoSuchEntityException(
81-
__("The product that was requested doesn't exist. Verify the product and try again.")
82-
);
83-
continue;
73+
$cartItems = [];
74+
/** @var CartItemInterface $cartItem */
75+
foreach ($paginatedCartItems['items'] as $cartItem) {
76+
foreach ($allVisibleItems as $item) {
77+
if ($cartItem->getId() == $item->getId()) {
78+
$cartItems[] = $item;
8479
}
85-
$cartItem->setQuote($cart);
86-
$itemsData[] = [
87-
'id' => $cartItem->getItemId(),
88-
'uid' => $this->uidEncoder->encode((string) $cartItem->getItemId()),
89-
'quantity' => $cartItem->getQty(),
90-
'product' => $cartProductsData[$productId],
91-
'model' => $cartItem,
92-
];
9380
}
94-
95-
return [
96-
'items' => $itemsData,
97-
'total_count' => $paginatedCartItems['total'],
98-
'page_info' => [
99-
'page_size' => $pageSize,
100-
'current_page' => $currentPage,
101-
'total_pages' => (int) ceil($paginatedCartItems['total'] / $pageSize)
102-
],
103-
];
104-
} catch (\Exception $e) {
105-
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
10681
}
82+
$itemsData = $this->getItemsData->execute($cartItems);
83+
84+
return [
85+
'items' => $itemsData,
86+
'total_count' => $paginatedCartItems['total'],
87+
'page_info' => [
88+
'page_size' => $pageSize,
89+
'current_page' => $currentPage,
90+
'total_pages' => (int) ceil($paginatedCartItems['total'] / $pageSize)
91+
],
92+
];
10793
}
10894

10995
/**
@@ -121,21 +107,4 @@ private function validate(array $args)
121107
throw new GraphQlInputException(__('pageSize value must be greater than 0.'));
122108
}
123109
}
124-
125-
/**
126-
* Get product data for cart items
127-
*
128-
* @param Product[] $products
129-
* @return array
130-
*/
131-
private function getCartProductsData(array $products): array
132-
{
133-
$productsData = [];
134-
foreach ($products as $product) {
135-
$productsData[$product->getId()] = $product->getData();
136-
$productsData[$product->getId()]['model'] = $product;
137-
$productsData[$product->getId()]['uid'] = $this->uidEncoder->encode((string) $product->getId());
138-
}
139-
return $productsData;
140-
}
141110
}

0 commit comments

Comments
 (0)