Skip to content

Commit 045eeeb

Browse files
authored
Merge branch '2.4-develop' into functional-mainline-ce-new
2 parents d0e0816 + c26aca7 commit 045eeeb

File tree

27 files changed

+733
-261
lines changed

27 files changed

+733
-261
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
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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;
18+
19+
use Magento\Framework\Exception\LocalizedException;
20+
use Magento\Quote\Model\Quote;
21+
22+
class GetDiscounts
23+
{
24+
/**
25+
* Get Discount Values
26+
*
27+
* @param Quote $quote
28+
* @param array $discounts
29+
* @return array|null
30+
* @throws LocalizedException
31+
*/
32+
public function execute(Quote $quote, array $discounts): ?array
33+
{
34+
if (empty($discounts)) {
35+
return null;
36+
}
37+
38+
$discountValues = [];
39+
foreach ($discounts as $value) {
40+
$discountData = $value->getDiscountData();
41+
$discountValues[] = [
42+
'label' => $value->getRuleLabel() ?: __('Discount'),
43+
'applied_to' => $discountData->getAppliedTo(),
44+
'amount' => [
45+
'value' => $discountData->getAmount(),
46+
'currency' => $quote->getQuoteCurrencyCode()
47+
],
48+
'discount_model' => $value,
49+
'quote_model' => $quote
50+
];
51+
}
52+
53+
return $discountValues;
54+
}
55+
}

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

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,26 @@
1515
use Magento\Quote\Model\Cart\Totals;
1616
use Magento\Quote\Model\Quote\Item;
1717
use Magento\QuoteGraphQl\Model\Cart\TotalsCollector;
18+
use Magento\QuoteGraphQl\Model\GetDiscounts;
1819

1920
/**
2021
* @inheritdoc
2122
*/
2223
class CartItemPrices implements ResolverInterface, ResetAfterRequestInterface
2324
{
24-
/**
25-
* @var TotalsCollector
26-
*/
27-
private $totalsCollector;
28-
2925
/**
3026
* @var Totals|null
3127
*/
3228
private $totals;
3329

3430
/**
35-
* CartItemPrices constructor
36-
*
3731
* @param TotalsCollector $totalsCollector
32+
* @param GetDiscounts $getDiscounts
3833
*/
3934
public function __construct(
40-
TotalsCollector $totalsCollector
35+
private readonly TotalsCollector $totalsCollector,
36+
private readonly GetDiscounts $getDiscounts
4137
) {
42-
$this->totalsCollector = $totalsCollector;
4338
}
4439

4540
/**
@@ -69,10 +64,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6964

7065
/** calculate bundle product discount */
7166
if ($cartItem->getProductType() == 'bundle') {
72-
$discountValues = $this->getDiscountValues($cartItem, $currencyCode);
67+
$discounts = $cartItem->getExtensionAttributes()->getDiscounts() ?? [];
7368
$discountAmount = 0;
74-
foreach ((array) $discountValues as $discountValue) {
75-
$discountAmount += $discountValue['amount']['value'];
69+
foreach ($discounts as $discount) {
70+
$discountAmount += $discount->getDiscountData()->getAmount();
7671
}
7772
} else {
7873
$discountAmount = $cartItem->getDiscountAmount();
@@ -99,36 +94,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
9994
'currency' => $currencyCode,
10095
'value' => $discountAmount,
10196
],
102-
'discounts' => $this->getDiscountValues($cartItem, $currencyCode)
97+
'discounts' => $this->getDiscounts->execute(
98+
$cartItem->getQuote(),
99+
$cartItem->getExtensionAttributes()->getDiscounts() ?? []
100+
)
103101
];
104102
}
105-
106-
/**
107-
* Get Discount Values
108-
*
109-
* @param Item $cartItem
110-
* @param string $currencyCode
111-
* @return array|null
112-
*/
113-
private function getDiscountValues($cartItem, $currencyCode)
114-
{
115-
$itemDiscounts = $cartItem->getExtensionAttributes()->getDiscounts();
116-
if ($itemDiscounts) {
117-
$discountValues = [];
118-
foreach ($itemDiscounts as $value) {
119-
$discount = [];
120-
$amount = [];
121-
/* @var \Magento\SalesRule\Api\Data\DiscountDataInterface $discountData */
122-
$discountData = $value->getDiscountData();
123-
$discountAmount = $discountData->getAmount();
124-
$discount['label'] = $value->getRuleLabel() ?: __('Discount');
125-
$amount['value'] = $discountAmount;
126-
$amount['currency'] = $currencyCode;
127-
$discount['amount'] = $amount;
128-
$discountValues[] = $discount;
129-
}
130-
return $discountValues;
131-
}
132-
return null;
133-
}
134103
}

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
}

0 commit comments

Comments
 (0)