|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\ConfigurableProduct\Plugin\CatalogWidget\Block\Product; |
| 10 | + |
| 11 | +use Magento\Catalog\Model\Product\Visibility; |
| 12 | +use Magento\Catalog\Model\ResourceModel\Product\Collection; |
| 13 | +use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; |
| 14 | +use Magento\CatalogWidget\Block\Product\ProductsList; |
| 15 | +use Magento\Framework\App\ResourceConnection; |
| 16 | +use Magento\Framework\Exception\LocalizedException; |
| 17 | + |
| 18 | +class ProductsListPlugin |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var CollectionFactory |
| 22 | + */ |
| 23 | + protected CollectionFactory $productCollectionFactory; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var Visibility |
| 27 | + */ |
| 28 | + protected Visibility $catalogProductVisibility; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var ResourceConnection |
| 32 | + */ |
| 33 | + protected ResourceConnection $resource; |
| 34 | + |
| 35 | + /** |
| 36 | + * @param CollectionFactory $productCollectionFactory |
| 37 | + * @param Visibility $catalogProductVisibility |
| 38 | + * @param ResourceConnection $resource |
| 39 | + */ |
| 40 | + public function __construct( |
| 41 | + CollectionFactory $productCollectionFactory, |
| 42 | + Visibility $catalogProductVisibility, |
| 43 | + ResourceConnection $resource |
| 44 | + ) { |
| 45 | + $this->productCollectionFactory = $productCollectionFactory; |
| 46 | + $this->catalogProductVisibility = $catalogProductVisibility; |
| 47 | + $this->resource = $resource; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Adds configurable products to the item list if child products are already part of the collection |
| 52 | + * |
| 53 | + * @param ProductsList $subject |
| 54 | + * @param Collection $result |
| 55 | + * @return Collection |
| 56 | + * @throws LocalizedException |
| 57 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 58 | + */ |
| 59 | + public function afterCreateCollection(ProductsList $subject, Collection $result): Collection |
| 60 | + { |
| 61 | + if ($result->count()) { |
| 62 | + $connection = $this->resource->getConnection(); |
| 63 | + $productIds = $connection->fetchCol( |
| 64 | + $connection |
| 65 | + ->select() |
| 66 | + ->from(['e' => $this->resource->getTableName('catalog_product_entity')], ['link_table.parent_id']) |
| 67 | + ->joinInner( |
| 68 | + ['link_table' => $this->resource->getTableName('catalog_product_super_link')], |
| 69 | + 'link_table.product_id = e.entity_id', |
| 70 | + [] |
| 71 | + ) |
| 72 | + ->where('link_table.product_id IN (?)', $result->getAllIds()) |
| 73 | + ); |
| 74 | + |
| 75 | + $configurableProductCollection = $this->productCollectionFactory->create(); |
| 76 | + $configurableProductCollection->setVisibility($this->catalogProductVisibility->getVisibleInCatalogIds()); |
| 77 | + $configurableProductCollection->addIdFilter($productIds); |
| 78 | + |
| 79 | + foreach ($configurableProductCollection->getItems() as $item) { |
| 80 | + $result->addItem($item); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return $result; |
| 85 | + } |
| 86 | +} |
0 commit comments