|
3 | 3 | * Copyright © Magento, Inc. All rights reserved. |
4 | 4 | * See COPYING.txt for license details. |
5 | 5 | */ |
6 | | - |
7 | 6 | declare(strict_types=1); |
8 | 7 |
|
9 | 8 | namespace Magento\ConfigurableProductGraphQl\Model\Options; |
10 | 9 |
|
11 | 10 | use Magento\Catalog\Api\Data\ProductInterface; |
| 11 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
12 | 12 | use Magento\ConfigurableProduct\Helper\Data; |
13 | | -use Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute; |
14 | | -use Magento\ConfigurableProductGraphQl\Model\Formatter\Option; |
| 13 | +use Magento\ConfigurableProductGraphQl\Model\Options\DataProvider\Variant; |
| 14 | +use Magento\Framework\Exception\NoSuchEntityException; |
15 | 15 |
|
16 | 16 | /** |
17 | 17 | * Retrieve metadata for configurable option selection. |
18 | 18 | */ |
19 | | -class ConfigurableOptionsMetadata |
| 19 | +class Metadata |
20 | 20 | { |
21 | 21 | /** |
22 | 22 | * @var Data |
23 | 23 | */ |
24 | 24 | private $configurableProductHelper; |
25 | 25 |
|
26 | 26 | /** |
27 | | - * @var Option |
| 27 | + * @var SelectionUidFormatter |
| 28 | + */ |
| 29 | + private $selectionUidFormatter; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var ProductRepositoryInterface |
28 | 33 | */ |
29 | | - private $configurableOptionsFormatter; |
| 34 | + private $productRepository; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var Variant |
| 38 | + */ |
| 39 | + private $variant; |
30 | 40 |
|
31 | 41 | /** |
32 | 42 | * @param Data $configurableProductHelper |
33 | | - * @param Option $configurableOptionsFormatter |
| 43 | + * @param SelectionUidFormatter $selectionUidFormatter |
| 44 | + * @param ProductRepositoryInterface $productRepository |
| 45 | + * @param Variant $variant |
34 | 46 | */ |
35 | 47 | public function __construct( |
36 | 48 | Data $configurableProductHelper, |
37 | | - Option $configurableOptionsFormatter |
| 49 | + SelectionUidFormatter $selectionUidFormatter, |
| 50 | + ProductRepositoryInterface $productRepository, |
| 51 | + Variant $variant |
38 | 52 | ) { |
39 | 53 | $this->configurableProductHelper = $configurableProductHelper; |
40 | | - $this->configurableOptionsFormatter = $configurableOptionsFormatter; |
| 54 | + $this->selectionUidFormatter = $selectionUidFormatter; |
| 55 | + $this->productRepository = $productRepository; |
| 56 | + $this->variant = $variant; |
41 | 57 | } |
42 | 58 |
|
43 | 59 | /** |
44 | | - * Load available selections from configurable options and variant. |
| 60 | + * Load available selections from configurable options. |
45 | 61 | * |
46 | 62 | * @param ProductInterface $product |
47 | | - * @param array $options |
48 | | - * @param array $selectedOptions |
| 63 | + * @param array $selectedOptionsUid |
49 | 64 | * @return array |
| 65 | + * @throws NoSuchEntityException |
| 66 | + * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
50 | 67 | */ |
51 | | - public function getAvailableSelections(ProductInterface $product, array $options, array $selectedOptions): array |
52 | | - { |
53 | | - $attributes = $this->getAttributes($product); |
54 | | - $availableSelections = []; |
| 68 | + public function getAvailableSelections( |
| 69 | + ProductInterface $product, |
| 70 | + array $selectedOptionsUid |
| 71 | + ): array { |
| 72 | + $options = $this->configurableProductHelper->getOptions($product, $this->getAllowProducts($product)); |
| 73 | + $selectedOptions = $this->selectionUidFormatter->extract($selectedOptionsUid); |
| 74 | + $attributeCodes = $this->getAttributeCodes($product); |
| 75 | + $availableSelections = $availableProducts = $variantData = []; |
55 | 76 |
|
56 | | - foreach ($options as $attributeId => $option) { |
57 | | - if ($attributeId === 'index' || isset($selectedOptions[$attributeId])) { |
58 | | - continue; |
| 77 | + if (isset($options['index']) && $options['index']) { |
| 78 | + foreach ($options['index'] as $productId => $productOptions) { |
| 79 | + if (!empty($selectedOptions) && !$this->hasProductRequiredOptions($selectedOptions, $productOptions)) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + $availableProducts[] = $productId; |
| 84 | + foreach ($productOptions as $attributeId => $optionIndex) { |
| 85 | + $uid = $this->selectionUidFormatter->encode($attributeId, (int)$optionIndex); |
| 86 | + |
| 87 | + if (isset($availableSelections[$attributeId]['option_value_uids']) |
| 88 | + && in_array($uid, $availableSelections[$attributeId]['option_value_uids']) |
| 89 | + ) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + $availableSelections[$attributeId]['option_value_uids'][] = $uid; |
| 93 | + $availableSelections[$attributeId]['attribute_code'] = $attributeCodes[$attributeId]; |
| 94 | + } |
| 95 | + |
| 96 | + if ($this->hasSelectionProduct($selectedOptions, $productOptions)) { |
| 97 | + $variantProduct = $this->productRepository->getById($productId); |
| 98 | + $variantData = $variantProduct->getData(); |
| 99 | + $variantData['model'] = $variantProduct; |
| 100 | + } |
59 | 101 | } |
| 102 | + } |
| 103 | + |
| 104 | + return [ |
| 105 | + 'options_available_for_selection' => $availableSelections, |
| 106 | + 'variant' => $variantData, |
| 107 | + 'availableSelectionProducts' => array_unique($availableProducts), |
| 108 | + 'product' => $product |
| 109 | + ]; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Get allowed products. |
| 114 | + * |
| 115 | + * @param ProductInterface $product |
| 116 | + * @return ProductInterface[] |
| 117 | + */ |
| 118 | + public function getAllowProducts(ProductInterface $product): array |
| 119 | + { |
| 120 | + return $this->variant->getSalableVariantsByParent($product) ?? []; |
| 121 | + } |
60 | 122 |
|
61 | | - $availableSelections[] = $this->configurableOptionsFormatter->format( |
62 | | - $attributes[$attributeId], |
63 | | - $options[$attributeId] ?? [] |
64 | | - ); |
| 123 | + /** |
| 124 | + * Check if a product has the selected options. |
| 125 | + * |
| 126 | + * @param array $requiredOptions |
| 127 | + * @param array $productOptions |
| 128 | + * @return bool |
| 129 | + */ |
| 130 | + private function hasProductRequiredOptions($requiredOptions, $productOptions): bool |
| 131 | + { |
| 132 | + $result = true; |
| 133 | + foreach ($requiredOptions as $attributeId => $optionIndex) { |
| 134 | + if (!isset($productOptions[$attributeId]) || !$productOptions[$attributeId] |
| 135 | + || $optionIndex != $productOptions[$attributeId] |
| 136 | + ) { |
| 137 | + $result = false; |
| 138 | + break; |
| 139 | + } |
65 | 140 | } |
66 | 141 |
|
67 | | - return $availableSelections; |
| 142 | + return $result; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Check if selected options match a product. |
| 147 | + * |
| 148 | + * @param array $requiredOptions |
| 149 | + * @param array $productOptions |
| 150 | + * @return bool |
| 151 | + */ |
| 152 | + private function hasSelectionProduct($requiredOptions, $productOptions): bool |
| 153 | + { |
| 154 | + return $this->hasProductRequiredOptions($productOptions, $requiredOptions); |
68 | 155 | } |
69 | 156 |
|
70 | 157 | /** |
71 | | - * Retrieve configurable attributes for the product |
| 158 | + * Retrieve attribute codes |
72 | 159 | * |
73 | 160 | * @param ProductInterface $product |
74 | | - * @return Attribute[] |
| 161 | + * @return string[] |
75 | 162 | */ |
76 | | - private function getAttributes(ProductInterface $product): array |
| 163 | + private function getAttributeCodes(ProductInterface $product): array |
77 | 164 | { |
78 | 165 | $allowedAttributes = $this->configurableProductHelper->getAllowAttributes($product); |
79 | | - $attributes = []; |
| 166 | + $attributeCodes = []; |
80 | 167 | foreach ($allowedAttributes as $attribute) { |
81 | | - $attributes[$attribute->getAttributeId()] = $attribute; |
| 168 | + $attributeCodes[$attribute->getAttributeId()] = $attribute->getProductAttribute()->getAttributeCode(); |
82 | 169 | } |
83 | 170 |
|
84 | | - return $attributes; |
| 171 | + return $attributeCodes; |
85 | 172 | } |
86 | 173 | } |
0 commit comments