Skip to content

Commit 7f82e2d

Browse files
committed
Merge remote-tracking branch 'shikha/28573_wrong_thumbnail' into 28573_wrong_thumbnail
2 parents 6e0c28c + cf70a95 commit 7f82e2d

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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\ConfigurableProductGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Query\ResolverInterface;
13+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
use Magento\Catalog\Api\ProductRepositoryInterface;
15+
16+
/**
17+
* Fetches the Product data according to the GraphQL schema
18+
*/
19+
class ProductResolver implements ResolverInterface
20+
{
21+
/**
22+
* @var ProductRepositoryInterface
23+
*/
24+
private $productRepository;
25+
26+
/**
27+
* @param ProductRepositoryInterface $productRepository
28+
*/
29+
public function __construct(ProductRepositoryInterface $productRepository)
30+
{
31+
$this->productRepository = $productRepository;
32+
}
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
public function resolve(
38+
Field $field,
39+
$context,
40+
ResolveInfo $info,
41+
array $value = null,
42+
array $args = null
43+
) {
44+
if (!isset($value['model'])) {
45+
throw new LocalizedException(__('"model" value should be specified'));
46+
}
47+
48+
$cartItem = $value['model'];
49+
$product = $this->productRepository->get($cartItem->getSku());
50+
$productData = $product->toArray();
51+
$productData['model'] = $product;
52+
return $productData;
53+
}
54+
}

app/code/Magento/ConfigurableProductGraphQl/Model/Resolver/SelectionMediaGallery.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\ConfigurableProductGraphQl\Model\Resolver;
79

810
use Magento\Framework\GraphQl\Config\Element\Field;

app/code/Magento/ConfigurableProductGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ input ConfigurableProductCartItemInput {
6464
type ConfigurableCartItem implements CartItemInterface {
6565
customizable_options: [SelectedCustomizableOption] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\CustomizableOptions")
6666
configurable_options: [SelectedConfigurableOption!]! @resolver(class: "Magento\\ConfigurableProductGraphQl\\Model\\Resolver\\ConfigurableCartItemOptions")
67+
configured_variant: ProductInterface @doc(description: "Product details of the cart item") @resolver(class: "\\Magento\\ConfigurableProductGraphQl\\Model\\Resolver\\ProductResolver")
6768
}
6869

6970
type SelectedConfigurableOption {

dev/tests/api-functional/testsuite/Magento/GraphQl/ConfigurableProduct/AddConfigurableProductToCartTest.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,42 @@ public function testAddConfigurableProductToCartWithCustomOption()
416416
$this->assertResponseFields($item['customizable_options'], $expectedOptions['customizable_options']);
417417
}
418418

419+
/**
420+
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/configurable_product_with_child_products_with_images.php
421+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
422+
*/
423+
public function testAddConfigurableProductWithImageToCart()
424+
{
425+
$searchResponse = $this->graphQlQuery($this->getFetchProductQuery('configurable'));
426+
$product = current($searchResponse['products']['items']);
427+
428+
$quantity = 1;
429+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
430+
$parentSku = $product['sku'];
431+
$sku = 'simple_20';
432+
$attributeId = (int) $product['configurable_options'][0]['attribute_id'];
433+
$optionId = $product['configurable_options'][0]['values'][1]['value_index'];
434+
435+
$query = $this->graphQlQueryForVariant(
436+
$maskedQuoteId,
437+
$parentSku,
438+
$sku,
439+
$quantity
440+
);
441+
442+
$response = $this->graphQlMutation($query);
443+
444+
$cartItem = current($response['addConfigurableProductsToCart']['cart']['items']);
445+
self::assertEquals($quantity, $cartItem['quantity']);
446+
self::assertEquals($parentSku, $cartItem['product']['sku']);
447+
self::assertArrayHasKey('configured_variant', $cartItem);
448+
449+
$variant = $cartItem['configured_variant'];
450+
$expectedThumbnailUrl = "magento_thumbnail.jpg";
451+
$variantImage = basename($variant['thumbnail']['url']);
452+
$this->assertEquals($expectedThumbnailUrl, $variantImage);
453+
}
454+
419455
/**
420456
* @param string $maskedQuoteId
421457
* @param string $parentSku
@@ -458,6 +494,52 @@ private function getQuery(string $maskedQuoteId, string $parentSku, string $sku,
458494
}
459495
}
460496
}
497+
QUERY;
498+
}
499+
500+
/**
501+
* @param string $maskedQuoteId
502+
* @param string $parentSku
503+
* @param string $sku
504+
* @param int $quantity
505+
* @return cart items with variants details
506+
*/
507+
private function graphQlQueryForVariant(string $maskedQuoteId, string $parentSku, string $sku, int $quantity): string
508+
{
509+
return <<<QUERY
510+
mutation {
511+
addConfigurableProductsToCart(
512+
input:{
513+
cart_id:"{$maskedQuoteId}"
514+
cart_items:{
515+
parent_sku: "{$parentSku}"
516+
data:{
517+
sku:"{$sku}"
518+
quantity:{$quantity}
519+
}
520+
}
521+
}
522+
) {
523+
cart {
524+
items {
525+
id
526+
quantity
527+
product {
528+
sku
529+
}
530+
... on ConfigurableCartItem {
531+
configured_variant {
532+
sku
533+
thumbnail {
534+
label
535+
url
536+
}
537+
}
538+
}
539+
}
540+
}
541+
}
542+
}
461543
QUERY;
462544
}
463545

0 commit comments

Comments
 (0)