Skip to content

Commit 29552f4

Browse files
author
Prabhu Ram
committed
Merge remote-tracking branch 'origin/MC-29945-cart-image' into MC-21228
2 parents 1a71334 + 928a1fa commit 29552f4

File tree

5 files changed

+196
-6
lines changed

5 files changed

+196
-6
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Cart;
9+
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Framework\Api\SearchCriteriaBuilder;
12+
use Magento\Quote\Model\Quote;
13+
14+
/**
15+
* Fetch Product models corresponding to a cart's items
16+
*/
17+
class GetCartProducts
18+
{
19+
/**
20+
* @var ProductRepositoryInterface
21+
*/
22+
private $productRepository;
23+
24+
/**
25+
* @var SearchCriteriaBuilder
26+
*/
27+
private $searchCriteriaBuilder;
28+
29+
/**
30+
* @param ProductRepositoryInterface $productRepository
31+
* @param SearchCriteriaBuilder $searchCriteriaBuilder
32+
*/
33+
public function __construct(
34+
ProductRepositoryInterface $productRepository,
35+
SearchCriteriaBuilder $searchCriteriaBuilder
36+
) {
37+
$this->productRepository = $productRepository;
38+
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
39+
}
40+
41+
/**
42+
* Get product models based on items in cart
43+
*
44+
* @param Quote $cart
45+
* @return ProductInterface[]
46+
*/
47+
public function execute(Quote $cart): array
48+
{
49+
$cartItems = $cart->getAllVisibleItems();
50+
if (empty($cartItems)) {
51+
return [];
52+
}
53+
$cartItemIds = \array_map(
54+
function ($item) {
55+
return $item->getProduct()->getId();
56+
},
57+
$cartItems
58+
);
59+
60+
$searchCriteria = $this->searchCriteriaBuilder->addFilter('entity_id', $cartItemIds, 'in')->create();
61+
$products = $this->productRepository->getList($searchCriteria)->getItems();
62+
63+
return $products;
64+
}
65+
}

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

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,31 @@
1010
use Magento\Framework\Exception\LocalizedException;
1111
use Magento\Framework\GraphQl\Config\Element\Field;
1212
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
13+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1314
use Magento\Framework\GraphQl\Query\ResolverInterface;
1415
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
use Magento\Quote\Model\Quote;
1517
use Magento\Quote\Model\Quote\Item as QuoteItem;
18+
use Magento\QuoteGraphQl\Model\Cart\GetCartProducts;
1619

1720
/**
1821
* @inheritdoc
1922
*/
2023
class CartItems implements ResolverInterface
2124
{
25+
/**
26+
* @var GetCartProducts
27+
*/
28+
private $getCartProducts;
29+
30+
/**
31+
* @param GetCartProducts $getCartProducts
32+
*/
33+
public function __construct(GetCartProducts $getCartProducts)
34+
{
35+
$this->getCartProducts = $getCartProducts;
36+
}
37+
2238
/**
2339
* @inheritdoc
2440
*/
@@ -36,12 +52,19 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
3652
$itemsData[] = new GraphQlInputException(__($error->getText()));
3753
}
3854
}
39-
foreach ($cart->getAllVisibleItems() as $cartItem) {
40-
/**
41-
* @var QuoteItem $cartItem
42-
*/
43-
$productData = $cartItem->getProduct()->getData();
44-
$productData['model'] = $cartItem->getProduct();
55+
56+
$cartProductsData = $this->getCartProductsData($cart);
57+
$cartItems = $cart->getAllVisibleItems();
58+
/** @var QuoteItem $cartItem */
59+
foreach ($cartItems as $cartItem) {
60+
$productId = $cartItem->getProduct()->getId();
61+
if (!isset($cartProductsData[$productId])) {
62+
$itemsData[] = new GraphQlNoSuchEntityException(
63+
__("The product that was requested doesn't exist. Verify the product and try again.")
64+
);
65+
continue;
66+
}
67+
$productData = $cartProductsData[$productId];
4568

4669
$itemsData[] = [
4770
'id' => $cartItem->getItemId(),
@@ -52,4 +75,22 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
5275
}
5376
return $itemsData;
5477
}
78+
79+
/**
80+
* Get product data for cart items
81+
*
82+
* @param Quote $cart
83+
* @return array
84+
*/
85+
private function getCartProductsData(Quote $cart): array
86+
{
87+
$products = $this->getCartProducts->execute($cart);
88+
$productsData = [];
89+
foreach ($products as $product) {
90+
$productsData[$product->getId()] = $product->getData();
91+
$productsData[$product->getId()]['model'] = $product;
92+
}
93+
94+
return $productsData;
95+
}
5596
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/AddSimpleProductToCartTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,54 @@ public function testAddSimpleProductToCart()
8181
self::assertEquals('USD', $rowTotalIncludingTax['currency']);
8282
}
8383

84+
/**
85+
* @magentoApiDataFixture Magento/Catalog/_files/product_with_image_no_options.php
86+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
87+
*/
88+
public function testAddProductToCartWithImage()
89+
{
90+
$sku = 'simple-2';
91+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
92+
93+
$query = <<<QUERY
94+
mutation {
95+
addSimpleProductsToCart(input: {
96+
cart_id: "$maskedQuoteId",
97+
cart_items: [{data: {sku: "$sku", quantity: 1}}]
98+
}) {
99+
cart {
100+
items {
101+
id
102+
prices{
103+
price {
104+
value
105+
}
106+
}
107+
quantity
108+
product {
109+
sku
110+
name
111+
image {
112+
label
113+
url
114+
}
115+
}
116+
}
117+
}
118+
}
119+
}
120+
QUERY;
121+
122+
$response = $this->graphQlMutation($query);
123+
$this->assertArrayHasKey('cart', $response['addSimpleProductsToCart']);
124+
$this->assertCount(1, $response['addSimpleProductsToCart']['cart']['items']);
125+
$cartItem = $response['addSimpleProductsToCart']['cart']['items'][0];
126+
$this->assertEquals('11', $cartItem['prices']['price']['value']);
127+
$this->assertEquals($sku, $cartItem['product']['sku']);
128+
$expectedImageRegex = '/^https?:\/\/.+magento_image(_[0-9]+)?.jpg$/';
129+
$this->assertRegExp($expectedImageRegex, $cartItem['product']['image']['url']);
130+
}
131+
84132
/**
85133
* Add disabled product to cart
86134
*
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
require __DIR__ . '/product_image.php';
8+
require __DIR__ . '/product_simple_without_custom_options.php';
9+
10+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
11+
$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
12+
$product = $productRepository->get('simple-2');
13+
14+
/** @var $product \Magento\Catalog\Model\Product */
15+
$product->setStoreId(0)
16+
->setImage('/m/a/magento_image.jpg')
17+
->setSmallImage('/m/a/magento_image.jpg')
18+
->setThumbnail('/m/a/magento_image.jpg')
19+
->setData('media_gallery', ['images' => [
20+
[
21+
'file' => '/m/a/magento_image.jpg',
22+
'position' => 1,
23+
'label' => 'Image Alt Text',
24+
'disabled' => 0,
25+
'media_type' => 'image'
26+
],
27+
]])
28+
->save();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
require __DIR__ . '/product_image_rollback.php';
8+
require __DIR__ . '/product_simple_without_custom_options_rollback.php';

0 commit comments

Comments
 (0)