Skip to content

Commit 13b0913

Browse files
committed
PWA-1326: Implement the schema changes for Configurable Options Selection
- refactoring swatches
1 parent c9a6164 commit 13b0913

File tree

4 files changed

+28
-18
lines changed

4 files changed

+28
-18
lines changed

app/code/Magento/ConfigurableProductGraphQl/Model/Formatter/OptionValue.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Magento\CatalogInventory\Model\StockRegistry;
1212
use Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute;
1313
use Magento\ConfigurableProductGraphQl\Model\Options\SelectionUidFormatter;
14-
use Magento\SwatchesGraphQl\Model\Resolver\Product\Options\DataProvider\SwatchDataProvider;
1514

1615
/**
1716
* Formatter for configurable product option values
@@ -23,28 +22,20 @@ class OptionValue
2322
*/
2423
private $selectionUidFormatter;
2524

26-
/**
27-
* @var SwatchDataProvider
28-
*/
29-
private $swatchDataProvider;
30-
3125
/**
3226
* @var StockRegistry
3327
*/
3428
private $stockRegistry;
3529

3630
/**
3731
* @param SelectionUidFormatter $selectionUidFormatter
38-
* @param SwatchDataProvider $swatchDataProvider
3932
* @param StockRegistry $stockRegistry
4033
*/
4134
public function __construct(
4235
SelectionUidFormatter $selectionUidFormatter,
43-
SwatchDataProvider $swatchDataProvider,
4436
StockRegistry $stockRegistry
4537
) {
4638
$this->selectionUidFormatter = $selectionUidFormatter;
47-
$this->swatchDataProvider = $swatchDataProvider;
4839
$this->stockRegistry = $stockRegistry;
4940
}
5041

@@ -69,7 +60,7 @@ public function format(array $optionValue, Attribute $attribute, array $optionId
6960
'is_available' => $this->getIsAvailable($optionIds[$valueIndex] ?? []),
7061
'is_use_default' => (bool)$attribute->getIsUseDefault(),
7162
'label' => $optionValue['label'],
72-
'swatch' => $this->swatchDataProvider->getData($optionValue['value_index'])
63+
'value_index' => $optionValue['value_index']
7364
];
7465
}
7566

app/code/Magento/ConfigurableProductGraphQl/Model/Options/DataProvider/Variant.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Magento\Catalog\Api\Data\ProductInterface;
1111
use Magento\CatalogInventory\Model\ResourceModel\Stock\StatusFactory;
1212
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
13-
use Magento\Framework\DataObject;
1413
use Magento\Framework\Exception\LocalizedException;
1514

1615
/**
@@ -44,8 +43,8 @@ public function __construct(
4443
* Load available child products by parent
4544
*
4645
* @param ProductInterface $product
47-
* @return DataObject[]
48-
* @throws LocalizedException
46+
* @return ProductInterface[]
47+
* @throws \Magento\Framework\Exception\LocalizedException
4948
*/
5049
public function getSalableVariantsByParent(ProductInterface $product): array
5150
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,5 @@ type ColorSwatchData implements SwatchDataInterface {
4949
}
5050

5151
type ConfigurableProductOptionValue {
52-
swatch: SwatchDataInterface
52+
swatch: SwatchDataInterface @resolver(class: "Magento\\SwatchesGraphQl\\Model\\Resolver\\Product\\Options\\SwatchData")
5353
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Swatches/ProductSwatchDataTest.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,29 @@ public function testVisualSwatchDataValues()
8484
$color = '#000000';
8585
$query = <<<QUERY
8686
{
87-
products(filter: {sku: {eq: "$productSku"}}) {
87+
products(filter: { sku: { eq: "$productSku" } }) {
8888
items {
89-
... on ConfigurableProduct{
90-
configurable_options{
89+
... on ConfigurableProduct {
90+
configurable_options {
9191
values {
92-
swatch_data{
92+
swatch_data {
9393
value
9494
... on ImageSwatchData {
9595
thumbnail
9696
}
9797
}
9898
}
9999
}
100+
configurable_product_options_selection {
101+
configurable_options {
102+
values {
103+
label
104+
swatch {
105+
value
106+
}
107+
}
108+
}
109+
}
100110
}
101111
}
102112
}
@@ -123,5 +133,15 @@ public function testVisualSwatchDataValues()
123133
$option['values'][1]['swatch_data']['thumbnail'],
124134
$this->swatchMediaHelper->getSwatchAttributeImage(Swatch::SWATCH_THUMBNAIL_NAME, $imageName)
125135
);
136+
137+
$configurableProductOptionsSelection =
138+
$product['configurable_product_options_selection']['configurable_options'][0];
139+
140+
$this->assertArrayHasKey('values', $configurableProductOptionsSelection);
141+
$this->assertEquals($color, $configurableProductOptionsSelection['values'][0]['swatch']['value']);
142+
$this->assertStringContainsString(
143+
$configurableProductOptionsSelection['values'][1]['swatch']['value'],
144+
$this->swatchMediaHelper->getSwatchAttributeImage(Swatch::SWATCH_IMAGE_NAME, $imageName)
145+
);
126146
}
127147
}

0 commit comments

Comments
 (0)