-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6690 from magento-honey-badgers/PWA-1326
[honey] PWA-1326: Implement the schema changes for Configurable Options Selection
- Loading branch information
Showing
16 changed files
with
910 additions
and
78 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
app/code/Magento/ConfigurableProductGraphQl/Model/Formatter/Option.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Magento\ConfigurableProductGraphQl\Model\Formatter; | ||
|
||
use Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute; | ||
use Magento\Framework\GraphQl\Query\Uid; | ||
|
||
/** | ||
* Formatter for configurable product options | ||
*/ | ||
class Option | ||
{ | ||
/** | ||
* @var Uid | ||
*/ | ||
private $idEncoder; | ||
|
||
/** | ||
* @var OptionValue | ||
*/ | ||
private $valueFormatter; | ||
|
||
/** | ||
* @param Uid $idEncoder | ||
* @param OptionValue $valueFormatter | ||
*/ | ||
public function __construct( | ||
Uid $idEncoder, | ||
OptionValue $valueFormatter | ||
) { | ||
$this->idEncoder = $idEncoder; | ||
$this->valueFormatter = $valueFormatter; | ||
} | ||
|
||
/** | ||
* Format configurable product options according to the GraphQL schema | ||
* | ||
* @param Attribute $attribute | ||
* @param array $optionIds | ||
* @return array|null | ||
*/ | ||
public function format(Attribute $attribute, array $optionIds): ?array | ||
{ | ||
$optionValues = []; | ||
|
||
foreach ($attribute->getOptions() as $option) { | ||
$optionValues[] = $this->valueFormatter->format($option, $attribute, $optionIds); | ||
} | ||
|
||
return [ | ||
'uid' => $this->idEncoder->encode($attribute->getProductSuperAttributeId()), | ||
'attribute_code' => $attribute->getProductAttribute()->getAttributeCode(), | ||
'label' => $attribute->getLabel(), | ||
'values' => $optionValues, | ||
]; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
app/code/Magento/ConfigurableProductGraphQl/Model/Formatter/OptionValue.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Magento\ConfigurableProductGraphQl\Model\Formatter; | ||
|
||
use Magento\CatalogInventory\Model\StockRegistry; | ||
use Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute; | ||
use Magento\ConfigurableProductGraphQl\Model\Options\SelectionUidFormatter; | ||
|
||
/** | ||
* Formatter for configurable product option values | ||
*/ | ||
class OptionValue | ||
{ | ||
/** | ||
* @var SelectionUidFormatter | ||
*/ | ||
private $selectionUidFormatter; | ||
|
||
/** | ||
* @var StockRegistry | ||
*/ | ||
private $stockRegistry; | ||
|
||
/** | ||
* @param SelectionUidFormatter $selectionUidFormatter | ||
* @param StockRegistry $stockRegistry | ||
*/ | ||
public function __construct( | ||
SelectionUidFormatter $selectionUidFormatter, | ||
StockRegistry $stockRegistry | ||
) { | ||
$this->selectionUidFormatter = $selectionUidFormatter; | ||
$this->stockRegistry = $stockRegistry; | ||
} | ||
|
||
/** | ||
* Format configurable product option values according to the GraphQL schema | ||
* | ||
* @param array $optionValue | ||
* @param Attribute $attribute | ||
* @param array $optionIds | ||
* @return array | ||
*/ | ||
public function format(array $optionValue, Attribute $attribute, array $optionIds): array | ||
{ | ||
$valueIndex = (int)$optionValue['value_index']; | ||
$attributeId = (int)$attribute->getAttributeId(); | ||
|
||
return [ | ||
'uid' => $this->selectionUidFormatter->encode( | ||
$attributeId, | ||
$valueIndex | ||
), | ||
'is_available' => $this->getIsAvailable($optionIds[$valueIndex] ?? []), | ||
'is_use_default' => (bool)$attribute->getIsUseDefault(), | ||
'label' => $optionValue['label'], | ||
'value_index' => $optionValue['value_index'] | ||
]; | ||
} | ||
|
||
/** | ||
* Get is variants available | ||
* | ||
* @param array $variantIds | ||
* @return bool | ||
*/ | ||
private function getIsAvailable(array $variantIds): bool | ||
{ | ||
foreach ($variantIds as $variantId) { | ||
if ($this->stockRegistry->getProductStockStatus($variantId)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
app/code/Magento/ConfigurableProductGraphQl/Model/Formatter/Variant.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Magento\ConfigurableProductGraphQl\Model\Formatter; | ||
|
||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
|
||
/** | ||
* Formatter for configurable product variant | ||
*/ | ||
class Variant | ||
{ | ||
/** | ||
* Format selected variant of configurable product based on selected options | ||
* | ||
* @param array $options | ||
* @param array $selectedOptions | ||
* @param array $variants | ||
* @return array|null | ||
* @throws GraphQlInputException | ||
*/ | ||
public function format(array $options, array $selectedOptions, array $variants): ?array | ||
{ | ||
$variant = null; | ||
$productIds = array_keys($variants); | ||
|
||
foreach ($selectedOptions as $attributeId => $selectedValue) { | ||
if (!isset($options[$attributeId][$selectedValue])) { | ||
throw new GraphQlInputException(__('configurableOptionValueUids values are incorrect')); | ||
} | ||
|
||
$productIds = array_intersect($productIds, $options[$attributeId][$selectedValue]); | ||
} | ||
|
||
if (count($productIds) === 1) { | ||
$variantProduct = $variants[array_pop($productIds)]; | ||
$variant = $variantProduct->getData(); | ||
$variant['url_path'] = $variantProduct->getProductUrl(); | ||
$variant['model'] = $variantProduct; | ||
} | ||
|
||
return $variant; | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
app/code/Magento/ConfigurableProductGraphQl/Model/Options/ConfigurableOptionsMetadata.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Magento\ConfigurableProductGraphQl\Model\Options; | ||
|
||
use Magento\Catalog\Api\Data\ProductInterface; | ||
use Magento\ConfigurableProduct\Helper\Data; | ||
use Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute; | ||
use Magento\ConfigurableProductGraphQl\Model\Formatter\Option; | ||
|
||
/** | ||
* Retrieve metadata for configurable option selection. | ||
*/ | ||
class ConfigurableOptionsMetadata | ||
{ | ||
/** | ||
* @var Data | ||
*/ | ||
private $configurableProductHelper; | ||
|
||
/** | ||
* @var Option | ||
*/ | ||
private $configurableOptionsFormatter; | ||
|
||
/** | ||
* @param Data $configurableProductHelper | ||
* @param Option $configurableOptionsFormatter | ||
*/ | ||
public function __construct( | ||
Data $configurableProductHelper, | ||
Option $configurableOptionsFormatter | ||
) { | ||
$this->configurableProductHelper = $configurableProductHelper; | ||
$this->configurableOptionsFormatter = $configurableOptionsFormatter; | ||
} | ||
|
||
/** | ||
* Load available selections from configurable options and variant. | ||
* | ||
* @param ProductInterface $product | ||
* @param array $options | ||
* @param array $selectedOptions | ||
* @return array | ||
*/ | ||
public function getAvailableSelections(ProductInterface $product, array $options, array $selectedOptions): array | ||
{ | ||
$attributes = $this->getAttributes($product); | ||
$availableSelections = []; | ||
|
||
foreach ($options as $attributeId => $option) { | ||
if ($attributeId === 'index' || isset($selectedOptions[$attributeId])) { | ||
continue; | ||
} | ||
|
||
$availableSelections[] = $this->configurableOptionsFormatter->format( | ||
$attributes[$attributeId], | ||
$options[$attributeId] ?? [] | ||
); | ||
} | ||
|
||
return $availableSelections; | ||
} | ||
|
||
/** | ||
* Retrieve configurable attributes for the product | ||
* | ||
* @param ProductInterface $product | ||
* @return Attribute[] | ||
*/ | ||
private function getAttributes(ProductInterface $product): array | ||
{ | ||
$allowedAttributes = $this->configurableProductHelper->getAllowAttributes($product); | ||
$attributes = []; | ||
foreach ($allowedAttributes as $attribute) { | ||
$attributes[$attribute->getAttributeId()] = $attribute; | ||
} | ||
|
||
return $attributes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.