|
7 | 7 |
|
8 | 8 | namespace Magento\CatalogRuleConfigurable\Plugin\CatalogRule\Model\Rule; |
9 | 9 |
|
| 10 | +use Magento\CatalogRule\Model\Rule; |
10 | 11 | use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable as ConfigurableProductsResourceModel; |
11 | 12 | use Magento\Framework\App\ResourceConnection; |
12 | 13 |
|
@@ -50,37 +51,104 @@ public function __construct( |
50 | 51 | /** |
51 | 52 | * Match configurable child products if configurable product matches the condition |
52 | 53 | * |
53 | | - * @param \Magento\CatalogRule\Model\Rule $rule |
| 54 | + * @param Rule $rule |
54 | 55 | * @param \Closure $proceed |
55 | 56 | * @return array |
56 | 57 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
57 | | - * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
58 | 58 | */ |
59 | 59 | public function aroundGetMatchingProductIds( |
60 | | - \Magento\CatalogRule\Model\Rule $rule, |
| 60 | + Rule $rule, |
61 | 61 | \Closure $proceed |
62 | 62 | ): array { |
63 | 63 | $productsFilter = $rule->getProductsFilter() ? (array) $rule->getProductsFilter() : []; |
64 | | - if ($productsFilter) { |
65 | | - $parentIds = $this->configurable->getParentIdsByChild($productsFilter); |
66 | | - if ($parentIds) { |
67 | | - $rule->setProductsFilter( |
68 | | - array_unique( |
69 | | - array_merge( |
70 | | - $productsFilter, |
71 | | - $parentIds |
72 | | - ) |
73 | | - ) |
74 | | - ); |
75 | | - } |
76 | | - } |
| 64 | + |
| 65 | + $this->updateProductsFilter($rule, $productsFilter); |
77 | 66 |
|
78 | 67 | $productIds = $proceed(); |
79 | 68 |
|
80 | 69 | if (self::$allConfigurableProductIds === null) { |
81 | 70 | self::$allConfigurableProductIds = $this->loadAllConfigurableProductIds(); |
82 | 71 | } |
83 | 72 |
|
| 73 | + return $this->processConfigurableProducts($productIds, $productsFilter); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Update products filter to include parent IDs and remove duplicate children |
| 78 | + * |
| 79 | + * @param Rule $rule |
| 80 | + * @param array $productsFilter |
| 81 | + * @return void |
| 82 | + */ |
| 83 | + private function updateProductsFilter( |
| 84 | + Rule $rule, |
| 85 | + array $productsFilter |
| 86 | + ): void { |
| 87 | + if (!$productsFilter) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + $parentIds = $this->configurable->getParentIdsByChild($productsFilter); |
| 92 | + if (!$parentIds) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + $childrenToRemove = $this->getChildrenToRemove($productsFilter, $parentIds); |
| 97 | + $filteredProducts = array_diff($productsFilter, $childrenToRemove); |
| 98 | + |
| 99 | + $rule->setProductsFilter( |
| 100 | + array_unique( |
| 101 | + array_merge( |
| 102 | + $filteredProducts, |
| 103 | + $parentIds |
| 104 | + ) |
| 105 | + ) |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Get children to remove from filter to avoid redundancy |
| 111 | + * |
| 112 | + * @param array $productsFilter |
| 113 | + * @param array $parentIds |
| 114 | + * @return array |
| 115 | + */ |
| 116 | + private function getChildrenToRemove(array $productsFilter, array $parentIds): array |
| 117 | + { |
| 118 | + $childrenToRemove = []; |
| 119 | + |
| 120 | + foreach ($parentIds as $parentId) { |
| 121 | + if (in_array($parentId, $productsFilter)) { |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + if (!isset(self::$childrenProducts[$parentId])) { |
| 126 | + self::$childrenProducts[$parentId] = $this->configurable->getChildrenIds($parentId)[0]; |
| 127 | + } |
| 128 | + $children = self::$childrenProducts[$parentId]; |
| 129 | + |
| 130 | + $childrenInFilter = array_intersect($productsFilter, $children); |
| 131 | + if (count($childrenInFilter) > 1) { |
| 132 | + $childrenInFilterArray = array_values($childrenInFilter); |
| 133 | + $childrenCount = count($childrenInFilterArray); |
| 134 | + for ($i = 1; $i < $childrenCount; $i++) { |
| 135 | + $childrenToRemove[] = $childrenInFilterArray[$i]; |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return $childrenToRemove; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Process configurable products and apply parent validation to children |
| 145 | + * |
| 146 | + * @param array $productIds |
| 147 | + * @param array $productsFilter |
| 148 | + * @return array |
| 149 | + */ |
| 150 | + private function processConfigurableProducts(array $productIds, array $productsFilter): array |
| 151 | + { |
84 | 152 | foreach (array_keys($productIds) as $productId) { |
85 | 153 | if (isset(self::$allConfigurableProductIds[$productId])) { |
86 | 154 | if (!isset(self::$childrenProducts[$productId])) { |
|
0 commit comments