/** * @param \Magento\CatalogRule\Model\Rule $rule * @param array $productIds * @return array */ public function afterGetMatchingProductIds(\Magento\CatalogRule\Model\Rule $rule, array $productIds) { $configurableProductIds = $this->configurableProductsProvider->getIds(array_keys($productIds)); foreach ($configurableProductIds as $productId) { $subProductsIds = $this->configurable->getChildrenIds($productId)[0]; $parentValidationResult = $productIds[$productId]; foreach ($subProductsIds as $subProductsId) { $productIds[$subProductsId] = $this->getSubProductValidationResult($rule->getId(), $subProductsId, $parentValidationResult); } unset($productIds[$productId]); } return $productIds; }
/** * {@inheritdoc} */ public function addChild($sku, $childSku) { $product = $this->productRepository->get($sku); $child = $this->productRepository->get($childSku); $childrenIds = array_values($this->configurableType->getChildrenIds($product->getId())[0]); if (in_array($child->getId(), $childrenIds)) { throw new StateException(__('Product has been already attached')); } $childrenIds[] = $child->getId(); $product->setAssociatedProductIds($childrenIds); $product->save(); return true; }
/** * @param \Magento\CatalogRule\Model\Rule $rule * @param array $productIds * @return array * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterGetMatchingProductIds(\Magento\CatalogRule\Model\Rule $rule, array $productIds) { $configurableProductIds = $this->configurableProductsProvider->getIds(array_keys($productIds)); foreach ($configurableProductIds as $productId) { if (!isset($this->childrenProducts[$productId])) { $this->childrenProducts[$productId] = $this->configurable->getChildrenIds($productId)[0]; } $subProductIds = $this->childrenProducts[$productId]; $parentValidationResult = isset($productIds[$productId]) ? array_filter($productIds[$productId]) : []; foreach ($subProductIds as $subProductId) { $childValidationResult = isset($productIds[$subProductId]) ? array_filter($productIds[$subProductId]) : []; $productIds[$subProductId] = $parentValidationResult + $childValidationResult; } unset($productIds[$productId]); } return $productIds; }
/** * Retrieve Required children ids * Return grouped array, ex array( * group => array(ids) * ) * * @param array|int $parentId * @param bool $required * @return array */ public function getChildrenIds($parentId, $required = true) { return $this->_catalogProductTypeConfigurable->getChildrenIds($parentId, $required); }