diff --git a/vendor/magento/module-catalog-rule/Model/Indexer/IndexBuilder.php b/vendor/magento/module-catalog-rule/Model/Indexer/IndexBuilder.php index 7c570e2..e197574 100644 --- a/vendor/magento/module-catalog-rule/Model/Indexer/IndexBuilder.php +++ b/vendor/magento/module-catalog-rule/Model/Indexer/IndexBuilder.php @@ -9,6 +9,7 @@ namespace Magento\CatalogRule\Model\Indexer; use Magento\Catalog\Model\Product; use Magento\Catalog\Model\ProductFactory; use Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher; +use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory; use Magento\CatalogRule\Model\Indexer\IndexBuilder\ProductLoader; use Magento\CatalogRule\Model\Indexer\IndexerTableSwapperInterface as TableSwapper; use Magento\CatalogRule\Model\ResourceModel\Rule\Collection as RuleCollection; @@ -168,6 +169,11 @@ class IndexBuilder private $productLoader; /** + * @var ProductCollectionFactory + */ + private $productCollectionFactory; + + /** * @param RuleCollectionFactory $ruleCollectionFactory * @param PriceCurrencyInterface $priceCurrency * @param ResourceConnection $resource @@ -188,6 +194,7 @@ class IndexBuilder * @param ProductLoader|null $productLoader * @param TableSwapper|null $tableSwapper * @param TimezoneInterface|null $localeDate + * @param ProductCollectionFactory|null $productCollectionFactory * @SuppressWarnings(PHPMD.ExcessiveParameterList) * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ @@ -211,7 +218,8 @@ class IndexBuilder ActiveTableSwitcher $activeTableSwitcher = null, ProductLoader $productLoader = null, TableSwapper $tableSwapper = null, - TimezoneInterface $localeDate = null + TimezoneInterface $localeDate = null, + ProductCollectionFactory $productCollectionFactory = null ) { $this->resource = $resource; $this->connection = $resource->getConnection(); @@ -253,6 +261,8 @@ class IndexBuilder ObjectManager::getInstance()->get(TableSwapper::class); $this->localeDate = $localeDate ?? ObjectManager::getInstance()->get(TimezoneInterface::class); + $this->productCollectionFactory = $productCollectionFactory ?? + ObjectManager::getInstance()->get(ProductCollectionFactory::class); } /** @@ -497,13 +507,20 @@ class IndexBuilder */ private function applyRules(RuleCollection $ruleCollection, Product $product): void { + /** @var \Magento\CatalogRule\Model\Rule $rule */ foreach ($ruleCollection as $rule) { - if (!$rule->validate($product)) { - continue; - } - + $productCollection = $this->productCollectionFactory->create(); + $productCollection->addIdFilter($product->getId()); + $rule->getConditions()->collectValidatedAttributes($productCollection); + $validationResult = []; $websiteIds = array_intersect($product->getWebsiteIds(), $rule->getWebsiteIds()); - $this->assignProductToRule($rule, $product->getId(), $websiteIds); + foreach ($websiteIds as $websiteId) { + $defaultGroupId = $this->storeManager->getWebsite($websiteId)->getDefaultGroupId(); + $defaultStoreId = $this->storeManager->getGroup($defaultGroupId)->getDefaultStoreId(); + $product->setStoreId($defaultStoreId); + $validationResult[$websiteId] = $rule->validate($product); + } + $this->assignProductToRule($rule, $product->getId(), array_keys(array_filter($validationResult))); } $this->cleanProductPriceIndex([$product->getId()]); diff --git a/vendor/magento/module-catalog-rule-configurable/Plugin/CatalogRule/Model/Rule/Validation.php b/vendor/magento/module-catalog-rule-configurable/Plugin/CatalogRule/Model/Rule/Validation.php index a4be621..194eb53 100644 --- a/vendor/magento/module-catalog-rule-configurable/Plugin/CatalogRule/Model/Rule/Validation.php +++ b/vendor/magento/module-catalog-rule-configurable/Plugin/CatalogRule/Model/Rule/Validation.php @@ -6,6 +6,7 @@ */ namespace Magento\CatalogRuleConfigurable\Plugin\CatalogRule\Model\Rule; +use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\ConfigurableProduct\Model\Product\Type\Configurable; use Magento\CatalogRule\Model\Rule; use Magento\Framework\DataObject; @@ -22,11 +23,18 @@ class Validation private $configurable; /** + * @var ProductRepositoryInterface + */ + private $productRepository; + + /** * @param Configurable $configurableType + * @param ProductRepositoryInterface $productRepository */ - public function __construct(Configurable $configurableType) + public function __construct(Configurable $configurableType, ProductRepositoryInterface $productRepository) { $this->configurable = $configurableType; + $this->productRepository = $productRepository; } /** @@ -41,10 +49,19 @@ class Validation { if (!$validateResult && ($configurableProducts = $this->configurable->getParentIdsByChild($product->getId()))) { foreach ($configurableProducts as $configurableProductId) { - $validateResult = $rule->getConditions()->validateByEntityId($configurableProductId); - // If any of configurable product is valid for current rule, then their sub-product must be valid too - if ($validateResult) { - break; + try { + $configurableProduct = $this->productRepository->getById( + $configurableProductId, + false, + $product->getStoreId() + ); + $validateResult = $rule->getConditions()->validate($configurableProduct); + //If any of configurable product is valid for current rule, then their sub-product must be valid too + if ($validateResult) { + break; + } + } catch (\Exception $e) { + continue; } } } diff --git a/vendor/magento/module-configurable-product/Model/Plugin/ProductIdentitiesExtender.php b/vendor/magento/module-configurable-product/Model/Plugin/ProductIdentitiesExtender.php index ef0ada5..6d209e0 100644 --- a/vendor/magento/module-configurable-product/Model/Plugin/ProductIdentitiesExtender.php +++ b/vendor/magento/module-configurable-product/Model/Plugin/ProductIdentitiesExtender.php @@ -7,9 +7,11 @@ declare(strict_types=1); namespace Magento\ConfigurableProduct\Model\Plugin; +use Magento\Catalog\Model\ResourceModel\Product\Website\Link as ProductWebsiteLink; use Magento\ConfigurableProduct\Model\Product\Type\Configurable as ConfigurableType; use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Catalog\Model\Product; +use Magento\Store\Model\Store; /** * Extender of product identities for child of configurable products @@ -27,6 +29,11 @@ class ProductIdentitiesExtender private $productRepository; /** + * @var ProductWebsiteLink + */ + private $productWebsiteLink; + + /** * @var array */ private $cacheParentIdsByChild = []; @@ -34,11 +41,16 @@ class ProductIdentitiesExtender /** * @param ConfigurableType $configurableType * @param ProductRepositoryInterface $productRepository + * @param ProductWebsiteLink $productWebsiteLink */ - public function __construct(ConfigurableType $configurableType, ProductRepositoryInterface $productRepository) - { + public function __construct( + ConfigurableType $configurableType, + ProductRepositoryInterface $productRepository, + ProductWebsiteLink $productWebsiteLink + ) { $this->configurableType = $configurableType; $this->productRepository = $productRepository; + $this->productWebsiteLink = $productWebsiteLink; } /** @@ -54,10 +66,19 @@ class ProductIdentitiesExtender if ($subject->getTypeId() !== ConfigurableType::TYPE_CODE) { return $identities; } + + $store = $subject->getStore(); $parentProductsIdentities = []; foreach ($this->getParentIdsByChild($subject->getId()) as $parentId) { - $parentProduct = $this->productRepository->getById($parentId); - $parentProductsIdentities[] = $parentProduct->getIdentities(); + $addParentIdentities = true; + if (Store::DEFAULT_STORE_ID !== (int) $store->getId()) { + $parentWebsiteIds = $this->productWebsiteLink->getWebsiteIdsByProductId($parentId); + $addParentIdentities = in_array($store->getWebsiteId(), $parentWebsiteIds); + } + if ($addParentIdentities) { + $parentProduct = $this->productRepository->getById($parentId); + $parentProductsIdentities[] = $parentProduct->getIdentities(); + } } $identities = array_merge($identities, ...$parentProductsIdentities);