diff --git a/vendor/magento/module-catalog-widget/Block/Product/ProductsList.php b/vendor/magento/module-catalog-widget/Block/Product/ProductsList.php index 4ca4bc1e2dc..fe3329ec281 100644 --- a/vendor/magento/module-catalog-widget/Block/Product/ProductsList.php +++ b/vendor/magento/module-catalog-widget/Block/Product/ProductsList.php @@ -1,7 +1,7 @@ productCollectionFactory = $productCollectionFactory; $this->catalogProductVisibility = $catalogProductVisibility; @@ -171,6 +188,9 @@ class ProductsList extends AbstractProduct implements BlockInterface, IdentityIn $this->urlEncoder = $urlEncoder ?: ObjectManager::getInstance()->get(EncoderInterface::class); $this->categoryRepository = $categoryRepository ?? ObjectManager::getInstance() ->get(CategoryRepositoryInterface::class); + $this->optionsData = $optionsData ?: ObjectManager::getInstance()->get(OptionsData::class); + $this->categoryConditionProcessor = $categoryConditionProcessor ?: ObjectManager::getInstance() + ->get(CategoryConditionProcessor::class); parent::__construct( $context, $data @@ -301,11 +321,23 @@ class ProductsList extends AbstractProduct implements BlockInterface, IdentityIn 'action' => $url, 'data' => [ 'product' => $product->getEntityId(), + 'options' => $this->optionsData->getOptionsData($product), ActionInterface::PARAM_NAME_URL_ENCODED => $this->urlEncoder->encode($url), ] ]; } + /** + * Return product options + * + * @param Product $product + * @return array + */ + public function getOptionsData(Product $product): array + { + return $this->optionsData->getOptionsData($product); + } + /** * @inheritdoc */ @@ -350,6 +382,7 @@ class ProductsList extends AbstractProduct implements BlockInterface, IdentityIn */ $collection = $this->_addProductAttributesAndPrices($collection) ->addStoreFilter() + ->addAttributeToFilter(Product::STATUS, ProductStatus::STATUS_ENABLED) ->addAttributeToSort('entity_id', 'desc') ->setPageSize($this->getPageSize()) ->setCurPage($this->getRequest()->getParam($this->getData('page_var_name'), 1)); @@ -367,34 +400,6 @@ class ProductsList extends AbstractProduct implements BlockInterface, IdentityIn return $collection; } - /** - * Update conditions if the category is an anchor category - * - * @param array $condition - * @return array - */ - private function updateAnchorCategoryConditions(array $condition): array - { - if (array_key_exists('value', $condition)) { - $categoryId = $condition['value']; - - try { - $category = $this->categoryRepository->get($categoryId, $this->_storeManager->getStore()->getId()); - } catch (NoSuchEntityException $e) { - return $condition; - } - - $children = $category->getIsAnchor() ? $category->getChildren(true) : []; - if ($children) { - $children = explode(',', $children); - $condition['operator'] = "()"; - $condition['value'] = array_merge([$categoryId], $children); - } - } - - return $condition; - } - /** * Get conditions * @@ -417,7 +422,10 @@ class ProductsList extends AbstractProduct implements BlockInterface, IdentityIn } if ($condition['attribute'] == 'category_ids') { - $conditions[$key] = $this->updateAnchorCategoryConditions($condition); + $conditions[$key] = $this->categoryConditionProcessor->process( + $condition, + $this->_storeManager->getStore()->getId() + ); } } } @@ -600,4 +608,12 @@ class ProductsList extends AbstractProduct implements BlockInterface, IdentityIn { return $this->conditionsHelper->decode(htmlspecialchars_decode($encodedConditions)); } + + /** + * @inheritdoc + */ + protected function _afterToHtml($html) + { + return trim($html); + } } diff --git a/vendor/magento/module-catalog-widget/Model/Rule/Condition/Product/CategoryConditionProcessor.php b/vendor/magento/module-catalog-widget/Model/Rule/Condition/Product/CategoryConditionProcessor.php new file mode 100644 index 00000000000..ab47a672f99 --- /dev/null +++ b/vendor/magento/module-catalog-widget/Model/Rule/Condition/Product/CategoryConditionProcessor.php @@ -0,0 +1,82 @@ +getCategoriesWithChildren( + !is_array($condition['value']) ? $this->toArray((string) $condition['value']) : $condition['value'], + $storeId + ); + } + return $condition; + } + + /** + * Get category IDs including children of anchor categories + * + * @param array $categoryIds + * @param int|null $storeId + * @return array + */ + private function getCategoriesWithChildren(array $categoryIds, ?int $storeId = null): array + { + $allCategoryIds = []; + foreach ($categoryIds as $categoryId) { + try { + $category = $this->categoryRepository->get($categoryId, $storeId); + } catch (NoSuchEntityException $e) { + continue; + } + + $allCategoryIds[] = $categoryId; + $children = $category->getIsAnchor() ? $category->getChildren(true) : ''; + if ($children) { + array_push($allCategoryIds, ...$this->toArray((string) $children)); + } + } + + return $allCategoryIds; + } + + /** + * Convert comma or semicolon separated string to array + * + * @param string $value + * @return array + */ + private function toArray(string $value): array + { + return $value ? preg_split('#\s*[,;]\s*#', $value, -1, PREG_SPLIT_NO_EMPTY) : []; + } +} diff --git a/vendor/magento/module-catalog-widget/view/frontend/templates/product/widget/content/grid.phtml b/vendor/magento/module-catalog-widget/view/frontend/templates/product/widget/content/grid.phtml index 78797f295c1..ed12241d623 100644 --- a/vendor/magento/module-catalog-widget/view/frontend/templates/product/widget/content/grid.phtml +++ b/vendor/magento/module-catalog-widget/view/frontend/templates/product/widget/content/grid.phtml @@ -74,6 +74,12 @@ use Magento\Wishlist\Helper\Data; isSaleable()): ?> getAddToCartPostParams($_item); ?>