Skip to content

Commit 80f61e6

Browse files
author
Eric Bohanon
committed
MAGETWO-89246: Fix out of stock filter for products resolution
1 parent a29a6f6 commit 80f61e6

File tree

27 files changed

+845
-754
lines changed

27 files changed

+845
-754
lines changed

app/code/Magento/Bundle/Model/ResourceModel/Option/Collection.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
3131
*/
3232
private $productIds = [];
3333

34+
/**
35+
* @var bool
36+
*/
37+
private $hasProductJoin = false;
38+
3439
/**
3540
* Init model and resource model
3641
*
@@ -49,6 +54,7 @@ protected function _construct()
4954
*/
5055
public function joinValues($storeId)
5156
{
57+
$this->joinProductEntityTable();
5258
$this->getSelect()->joinLeft(
5359
['option_value_default' => $this->getTable('catalog_product_bundle_option_value')],
5460
implode(
@@ -101,17 +107,48 @@ public function setProductIdFilter($productId)
101107
{
102108
$this->productIds[] = $productId;
103109

110+
return $this;
111+
}
112+
113+
private function joinProductEntityTable()
114+
{
115+
if ($this->hasProductJoin || empty($this->productIds)) {
116+
return;
117+
}
118+
119+
if (count($this->productIds) === 1) {
120+
$comparedField = reset($this->productIds);
121+
$whereClause = "cpe.entity_id = ?";
122+
} else {
123+
$comparedField = $this->productIds;
124+
$whereClause = "cpe.entity_id IN (?)";
125+
}
126+
104127
$productTable = $this->getTable('catalog_product_entity');
105128
$linkField = $this->getConnection()->getAutoIncrementField($productTable);
106129
$this->getSelect()->join(
107130
['cpe' => $productTable],
108131
'cpe.'.$linkField.' = main_table.parent_id',
109132
[]
110133
)->where(
111-
"cpe.entity_id = (?)",
112-
$this->productIds
134+
$whereClause,
135+
$comparedField
113136
);
114137

138+
$this->hasProductJoin = true;
139+
}
140+
141+
/**
142+
* Add base select based off product id buffer.
143+
*
144+
* @return $this
145+
*/
146+
protected function _beforeLoad()
147+
{
148+
$this->joinProductEntityTable();
149+
150+
parent::_beforeLoad();
151+
115152
return $this;
116153
}
117154

@@ -123,6 +160,7 @@ public function setProductIdFilter($productId)
123160
protected function _afterLoad()
124161
{
125162
$this->productIds = [];
163+
$this->hasProductJoin = false;
126164

127165
return parent::_afterLoad();
128166
}

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/Product.php

Lines changed: 14 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@
88
namespace Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider;
99

1010
use Magento\Catalog\Model\Product\Visibility;
11-
use Magento\Catalog\Model\ResourceModel\CategoryProduct;
12-
use Magento\Framework\Api\SearchCriteriaBuilder;
1311
use Magento\Framework\Api\SearchCriteriaInterface;
1412
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
15-
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
16-
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
1713
use Magento\Catalog\Api\Data\ProductSearchResultsInterfaceFactory;
1814
use Magento\Framework\Api\SearchResultsInterface;
15+
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessorInterface;
1916

2017
/**
2118
* Product field data provider, used for GraphQL resolver processing.
@@ -27,79 +24,42 @@ class Product
2724
*/
2825
private $collectionFactory;
2926

30-
/**
31-
* @var JoinProcessorInterface
32-
*/
33-
private $joinProcessor;
34-
35-
/**
36-
* @var CollectionProcessorInterface
37-
*/
38-
private $collectionProcessor;
39-
4027
/**
4128
* @var ProductSearchResultsInterfaceFactory
4229
*/
4330
private $searchResultsFactory;
4431

4532
/**
46-
* @var CategoryProduct
47-
*/
48-
private $categoryProduct;
49-
50-
/**
51-
* @var SearchCriteriaBuilder
52-
*/
53-
private $searchCriteriaBuilder;
54-
55-
/**
56-
* @var \Magento\Catalog\Model\Layer\Resolver
57-
*/
58-
private $layerResolver;
59-
60-
/**
61-
* @var \Magento\Catalog\Api\Data\ProductSearchResultsInterface
33+
* @var CollectionProcessorInterface
6234
*/
63-
private $searchResult;
35+
private $collectionProcessor;
6436

6537
/**
66-
* @var \Magento\Catalog\Model\ProductRepository
38+
* @var Visibility
6739
*/
68-
private $productRepository;
40+
private $visibility;
6941

7042
/**
71-
* @var Visibility
43+
* @var
7244
*/
73-
private $visibility;
45+
private $searchProcessor;
7446

7547
/**
7648
* @param CollectionFactory $collectionFactory
77-
* @param JoinProcessorInterface $joinProcessor
78-
* @param CollectionProcessorInterface $collectionProcessor
7949
* @param ProductSearchResultsInterfaceFactory $searchResultsFactory
80-
* @param CategoryProduct $categoryProduct
81-
* @param SearchCriteriaBuilder $searchCriteriaBuilder
50+
* @param Visibility $visibility
51+
* @param CollectionProcessorInterface $collectionProcessor
8252
*/
8353
public function __construct(
8454
CollectionFactory $collectionFactory,
85-
JoinProcessorInterface $joinProcessor,
86-
CollectionProcessorInterface $collectionProcessor,
8755
ProductSearchResultsInterfaceFactory $searchResultsFactory,
88-
CategoryProduct $categoryProduct,
89-
SearchCriteriaBuilder $searchCriteriaBuilder,
90-
\Magento\Catalog\Model\Layer\Resolver $layerResolver,
91-
\Magento\Catalog\Model\ProductRepository $productRepository,
92-
Visibility $visibility
56+
Visibility $visibility,
57+
CollectionProcessorInterface $collectionProcessor
9358
) {
9459
$this->collectionFactory = $collectionFactory;
95-
$this->joinProcessor = $joinProcessor;
96-
$this->collectionProcessor = $collectionProcessor;
9760
$this->searchResultsFactory = $searchResultsFactory;
98-
$this->categoryProduct = $categoryProduct;
99-
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
10061
$this->visibility = $visibility;
101-
$this->layerResolver = $layerResolver;
102-
$this->productRepository = $productRepository;
62+
$this->collectionProcessor = $collectionProcessor;
10363
}
10464

10565
/**
@@ -119,29 +79,17 @@ public function getList(
11979
): SearchResultsInterface {
12080
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
12181
$collection = $this->collectionFactory->create();
122-
$this->joinProcessor->process($collection);
12382

124-
foreach ($attributes as $attributeCode) {
125-
$collection->addAttributeToSelect($attributeCode);
126-
}
127-
$collection->addAttributeToSelect('special_price');
128-
$collection->addAttributeToSelect('special_price_from');
129-
$collection->addAttributeToSelect('special_price_to');
130-
$collection->addAttributeToSelect('tax_class_id');
131-
$collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
132-
$collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
83+
$this->collectionProcessor->process($collection, $searchCriteria, $attributes);
13384

13485
if (!$isChildSearch) {
13586
$visibilityIds
13687
= $isSearch ? $this->visibility->getVisibleInSearchIds() : $this->visibility->getVisibleInCatalogIds();
13788
$collection->setVisibility($visibilityIds);
13889
}
139-
140-
$this->collectionProcessor->process($searchCriteria, $collection);
141-
$collection->addWebsiteNamesToResult();
14290
$collection->load();
14391

144-
// Methods that perform extra fetches
92+
// Methods that perform extra fetches post-load
14593
$collection->addCategoryIds();
14694
$collection->addMediaGalleryData();
14795
$collection->addOptionsToResult();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessor;
9+
10+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
11+
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessorInterface;
12+
use Magento\Framework\Api\SearchCriteriaInterface;
13+
14+
/**
15+
* Adds passed in attributes to product collection results
16+
*
17+
* {@inheritdoc}
18+
*/
19+
class AttributeProcessor implements CollectionProcessorInterface
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function process(
25+
Collection $collection,
26+
SearchCriteriaInterface $searchCriteria,
27+
array $attributeNames
28+
): Collection {
29+
foreach ($attributeNames as $name) {
30+
$collection->addAttributeToSelect($name);
31+
}
32+
33+
return $collection;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessor;
9+
10+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
11+
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessorInterface;
12+
use Magento\Framework\Api\SearchCriteriaInterface;
13+
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
14+
15+
/**
16+
* Add necessary joins for extensible entities.
17+
*
18+
* {@inheritdoc}
19+
*/
20+
class ExtensibleEntityProcessor implements CollectionProcessorInterface
21+
{
22+
/**
23+
* @var JoinProcessorInterface
24+
*/
25+
private $joinProcessor;
26+
27+
/**
28+
* @param JoinProcessorInterface $joinProcessor
29+
*/
30+
public function __construct(JoinProcessorInterface $joinProcessor)
31+
{
32+
$this->joinProcessor = $joinProcessor;
33+
}
34+
35+
public function process(
36+
Collection $collection,
37+
SearchCriteriaInterface $searchCriteria,
38+
array $attributeNames
39+
): Collection {
40+
$this->joinProcessor->process($collection);
41+
42+
return $collection;
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessor;
9+
10+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
11+
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessorInterface;
12+
use Magento\Framework\Api\SearchCriteriaInterface;
13+
14+
/**
15+
* Add attributes required for every GraphQL product resolution process.
16+
*
17+
* {@inheritdoc}
18+
*/
19+
class RequiredColumnsProcessor implements CollectionProcessorInterface
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function process(
25+
Collection $collection,
26+
SearchCriteriaInterface $searchCriteria,
27+
array $attributeNames
28+
): Collection {
29+
$collection->addAttributeToSelect('special_price');
30+
$collection->addAttributeToSelect('special_price_from');
31+
$collection->addAttributeToSelect('special_price_to');
32+
$collection->addAttributeToSelect('tax_class_id');
33+
$collection->addWebsiteNamesToResult();
34+
35+
return $collection;
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessor;
9+
10+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
11+
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessorInterface;
12+
use Magento\Framework\Api\SearchCriteriaInterface;
13+
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface as SearchCriteriaApplier;
14+
15+
/**
16+
* Apply search criteria data to passed in collection.
17+
*
18+
* {@inheritdoc}
19+
*/
20+
class SearchCriteriaProcessor implements CollectionProcessorInterface
21+
{
22+
/**
23+
* @var SearchCriteriaApplier
24+
*/
25+
private $searchCriteriaApplier;
26+
27+
/**
28+
* @param SearchCriteriaApplier $searchCriteriaApplier
29+
*/
30+
public function __construct(SearchCriteriaApplier $searchCriteriaApplier)
31+
{
32+
$this->searchCriteriaApplier = $searchCriteriaApplier;
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function process(
39+
Collection $collection,
40+
SearchCriteriaInterface $searchCriteria,
41+
array $attributeNames
42+
): Collection {
43+
$this->searchCriteriaApplier->process($searchCriteria, $collection);
44+
45+
return $collection;
46+
}
47+
}

0 commit comments

Comments
 (0)