2525use Magento \Store \Model \Indexer \WebsiteDimensionProvider ;
2626use Magento \Store \Model \Store ;
2727use Magento \Catalog \Model \ResourceModel \Category ;
28+ use Zend_Db_Expr ;
29+ use Magento \Catalog \Model \ResourceModel \Product \Gallery ;
2830
2931/**
3032 * Product collection
@@ -43,70 +45,54 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
4345 /**
4446 * Alias for index table
4547 */
46- const INDEX_TABLE_ALIAS = 'price_index ' ;
48+ public const INDEX_TABLE_ALIAS = 'price_index ' ;
4749
4850 /**
4951 * Alias for main table
5052 */
51- const MAIN_TABLE_ALIAS = 'e ' ;
53+ public const MAIN_TABLE_ALIAS = 'e ' ;
5254
5355 /**
5456 * @var string
5557 */
5658 protected $ _idFieldName = 'entity_id ' ;
5759
5860 /**
59- * Catalog Product Flat is enabled cache per store
60- *
6161 * @var array
6262 */
6363 protected $ _flatEnabled = [];
6464
6565 /**
66- * Product websites table name
67- *
6866 * @var string
6967 */
7068 protected $ _productWebsiteTable ;
7169
7270 /**
73- * Product categories table name
74- *
7571 * @var string
7672 */
7773 protected $ _productCategoryTable ;
7874
7975 /**
80- * Is add URL rewrites to collection flag
81- *
8276 * @var bool
8377 */
8478 protected $ _addUrlRewrite = false ;
8579
8680 /**
87- * Add URL rewrite for category
88- *
8981 * @var int
9082 */
9183 protected $ _urlRewriteCategory = '' ;
9284
9385 /**
94- * Is add final price to product collection flag
95- *
9686 * @var bool
9787 */
9888 protected $ _addFinalPrice = false ;
9989
10090 /**
101- * Cache for all ids
102- *
10391 * @var array
10492 */
10593 protected $ _allIdsCache = null ;
10694
10795 /**
108- * Is add tax percents to product collection flag
109- *
11096 * @var bool
11197 */
11298 protected $ _addTaxPercents = false ;
@@ -136,43 +122,31 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
136122 protected $ _priceDataFieldFilters = [];
137123
138124 /**
139- * Price expression sql
140- *
141125 * @var string|null
142126 */
143127 protected $ _priceExpression ;
144128
145129 /**
146- * Additional price expression sql part
147- *
148130 * @var string|null
149131 */
150132 protected $ _additionalPriceExpression ;
151133
152134 /**
153- * Max prise (statistics data)
154- *
155135 * @var float
156136 */
157137 protected $ _maxPrice ;
158138
159139 /**
160- * Min prise (statistics data)
161- *
162140 * @var float
163141 */
164142 protected $ _minPrice ;
165143
166144 /**
167- * Prise standard deviation (statistics data)
168- *
169145 * @var float
170146 */
171147 protected $ _priceStandardDeviation ;
172148
173149 /**
174- * Prises count (statistics data)
175- *
176150 * @var int
177151 */
178152 protected $ _pricesCount = null ;
@@ -206,8 +180,6 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
206180 protected $ _scopeConfig ;
207181
208182 /**
209- * Customer session
210- *
211183 * @var \Magento\Customer\Model\Session
212184 */
213185 protected $ _customerSession ;
@@ -218,15 +190,11 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
218190 protected $ _localeDate ;
219191
220192 /**
221- * Catalog url
222- *
223193 * @var \Magento\Catalog\Model\ResourceModel\Url
224194 */
225195 protected $ _catalogUrl ;
226196
227197 /**
228- * Product option factory
229- *
230198 * @var \Magento\Catalog\Model\Product\OptionFactory
231199 */
232200 protected $ _productOptionFactory ;
@@ -249,8 +217,6 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
249217 protected $ _groupManagement ;
250218
251219 /**
252- * Need to add websites to result flag
253- *
254220 * @var bool
255221 */
256222 protected $ needToAddWebsiteNamesToResult ;
@@ -339,8 +305,13 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
339305 * @param PriceTableResolver|null $priceTableResolver
340306 * @param DimensionFactory|null $dimensionFactory
341307 * @param Category|null $categoryResourceModel
308+ * @param DbStorage|null $urlFinder
309+ * @param GalleryReadHandler|null $productGalleryReadHandler
310+ * @param Gallery|null $mediaGalleryResource
342311 *
343312 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
313+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
314+ * @SuppressWarnings(PHPMD.NPathComplexity)
344315 */
345316 public function __construct (
346317 \Magento \Framework \Data \Collection \EntityFactory $ entityFactory ,
@@ -368,7 +339,10 @@ public function __construct(
368339 TableMaintainer $ tableMaintainer = null ,
369340 PriceTableResolver $ priceTableResolver = null ,
370341 DimensionFactory $ dimensionFactory = null ,
371- Category $ categoryResourceModel = null
342+ Category $ categoryResourceModel = null ,
343+ DbStorage $ urlFinder = null ,
344+ GalleryReadHandler $ productGalleryReadHandler = null ,
345+ Gallery $ mediaGalleryResource = null
372346 ) {
373347 $ this ->moduleManager = $ moduleManager ;
374348 $ this ->_catalogProductFlatState = $ catalogProductFlatState ;
@@ -398,25 +372,19 @@ public function __construct(
398372 $ storeManager ,
399373 $ connection
400374 );
401- $ this ->tableMaintainer = $ tableMaintainer ?: ObjectManager::getInstance ()->get (TableMaintainer::class);
402- $ this ->priceTableResolver = $ priceTableResolver ?: ObjectManager::getInstance ()->get (PriceTableResolver::class);
375+ $ this ->tableMaintainer = $ tableMaintainer ?: ObjectManager::getInstance ()
376+ ->get (TableMaintainer::class);
377+ $ this ->priceTableResolver = $ priceTableResolver ?: ObjectManager::getInstance ()
378+ ->get (PriceTableResolver::class);
403379 $ this ->dimensionFactory = $ dimensionFactory
404380 ?: ObjectManager::getInstance ()->get (DimensionFactory::class);
405381 $ this ->categoryResourceModel = $ categoryResourceModel ?: ObjectManager::getInstance ()
406382 ->get (Category::class);
407- }
408-
409- /**
410- * Retrieve urlFinder
411- *
412- * @return GalleryReadHandler
413- */
414- private function getUrlFinder ()
415- {
416- if ($ this ->urlFinder === null ) {
417- $ this ->urlFinder = ObjectManager::getInstance ()->get (DbStorage::class);
418- }
419- return $ this ->urlFinder ;
383+ $ this ->urlFinder = $ urlFinder ?: ObjectManager::getInstance ()->get (DbStorage::class);
384+ $ this ->productGalleryReadHandler = $ productGalleryReadHandler ?: ObjectManager::getInstance ()
385+ ->get (GalleryReadHandler::class);
386+ $ this ->mediaGalleryResource = $ mediaGalleryResource ?: ObjectManager::getInstance ()
387+ ->get (Gallery::class);
420388 }
421389
422390 /**
@@ -643,7 +611,7 @@ protected function _initSelect()
643611 [self ::MAIN_TABLE_ALIAS => $ this ->getEntity ()->getFlatTableName ()],
644612 null
645613 )->columns (
646- ['status ' => new \ Zend_Db_Expr (ProductStatus::STATUS_ENABLED )]
614+ ['status ' => new Zend_Db_Expr (ProductStatus::STATUS_ENABLED )]
647615 );
648616 $ this ->addAttributeToSelect ($ this ->getResource ()->getDefaultAttributes ());
649617 if ($ this ->_catalogProductFlatState ->getFlatIndexerHelper ()->isAddChildData ()) {
@@ -791,7 +759,7 @@ public function addIdFilter($productId, $exclude = false)
791759 return $ this ;
792760 }
793761 if (is_array ($ productId )) {
794- if (! empty ( $ productId) ) {
762+ if ($ productId ) {
795763 if ($ exclude ) {
796764 $ condition = ['nin ' => $ productId ];
797765 } else {
@@ -1028,7 +996,7 @@ public function getMaxAttributeValue($attribute)
1028996 $ select ->join (
1029997 [$ tableAlias => $ attribute ->getBackend ()->getTable ()],
1030998 $ condition ,
1031- [$ fieldAlias => new \ Zend_Db_Expr ('MAX( ' . $ tableAlias . '.value) ' )]
999+ [$ fieldAlias => new Zend_Db_Expr ('MAX( ' . $ tableAlias . '.value) ' )]
10321000 )->group (
10331001 'e.entity_type_id '
10341002 );
@@ -1065,8 +1033,8 @@ public function getAttributeValueCountByRange($attribute, $range)
10651033 [$ tableAlias => $ attribute ->getBackend ()->getTable ()],
10661034 $ condition ,
10671035 [
1068- 'count_ ' . $ attributeCode => new \ Zend_Db_Expr ('COUNT(DISTINCT e.entity_id) ' ),
1069- 'range_ ' . $ attributeCode => new \ Zend_Db_Expr ('CEIL(( ' . $ tableAlias . '.value+0.01)/ ' . $ range . ') ' )
1036+ 'count_ ' . $ attributeCode => new Zend_Db_Expr ('COUNT(DISTINCT e.entity_id) ' ),
1037+ 'range_ ' . $ attributeCode => new Zend_Db_Expr ('CEIL(( ' . $ tableAlias . '.value+0.01)/ ' . $ range . ') ' )
10701038 ]
10711039 )->group (
10721040 'range_ ' . $ attributeCode
@@ -1104,8 +1072,8 @@ public function getAttributeValueCount($attribute)
11041072 [$ tableAlias => $ attribute ->getBackend ()->getTable ()],
11051073 $ condition ,
11061074 [
1107- 'count_ ' . $ attributeCode => new \ Zend_Db_Expr ('COUNT(DISTINCT e.entity_id) ' ),
1108- 'value_ ' . $ attributeCode => new \ Zend_Db_Expr ($ tableAlias . '.value ' )
1075+ 'count_ ' . $ attributeCode => new Zend_Db_Expr ('COUNT(DISTINCT e.entity_id) ' ),
1076+ 'value_ ' . $ attributeCode => new Zend_Db_Expr ($ tableAlias . '.value ' )
11091077 ]
11101078 )->group (
11111079 'value_ ' . $ attributeCode
@@ -1311,7 +1279,7 @@ public function getProductCountSelect()
13111279 'count_table.product_id = e.entity_id ' ,
13121280 [
13131281 'count_table.category_id ' ,
1314- 'product_count ' => new \ Zend_Db_Expr ('COUNT(DISTINCT count_table.product_id) ' )
1282+ 'product_count ' => new Zend_Db_Expr ('COUNT(DISTINCT count_table.product_id) ' )
13151283 ]
13161284 )->where (
13171285 'count_table.store_id = ? ' ,
@@ -1490,7 +1458,7 @@ protected function _addUrlRewrite()
14901458 $ filter ['metadata ' ]['category_id ' ] = $ this ->_urlRewriteCategory ;
14911459 }
14921460
1493- $ rewrites = $ this ->getUrlFinder () ->findAllByData ($ filter );
1461+ $ rewrites = $ this ->urlFinder ->findAllByData ($ filter );
14941462 foreach ($ rewrites as $ rewrite ) {
14951463 if ($ item = $ this ->getItemById ($ rewrite ->getEntityId ())) {
14961464 $ item ->setData ('request_path ' , $ rewrite ->getRequestPath ());
@@ -2090,28 +2058,25 @@ protected function _applyZeroStoreProductLimitations()
20902058 {
20912059 $ filters = $ this ->_productLimitationFilters ;
20922060 $ categories = $ this ->getChildrenCategories ((int )$ filters ['category_id ' ]);
2061+ $ joinCond = 'cat_pro.product_id = e.entity_id ' ;
20932062
2094- $ conditions = [
2095- 'cat_pro.product_id=e.entity_id ' ,
2096- $ this ->getConnection ()->quoteInto (
2097- 'cat_pro.category_id IN (?) ' ,
2098- $ categories
2099- ),
2100- ];
2101- $ joinCond = join (' AND ' , $ conditions );
2102-
2103- $ fromPart = $ this ->getSelect ()->getPart (\Magento \Framework \DB \Select::FROM );
2063+ $ fromPart = $ this ->getSelect ()->getPart (Select::FROM );
21042064 if (isset ($ fromPart ['cat_pro ' ])) {
21052065 $ fromPart ['cat_pro ' ]['joinCondition ' ] = $ joinCond ;
2106- $ this ->getSelect ()->setPart (\ Magento \ Framework \ DB \ Select::FROM , $ fromPart );
2066+ $ this ->getSelect ()->setPart (Select::FROM , $ fromPart );
21072067 } else {
2068+ $ conditions = [
2069+ $ joinCond ,
2070+ $ this ->getConnection ()->quoteInto ('cat_pro.category_id IN(?) ' , $ categories , 'int ' ),
2071+ ];
2072+ $ joinCond = join (' AND ' , $ conditions );
21082073 $ this ->getSelect ()->join (
21092074 ['cat_pro ' => $ this ->getTable ('catalog_category_product ' )],
21102075 $ joinCond ,
2111- ['cat_index_position ' => ' position ' ]
2112- );
2076+ ['cat_index_position ' => new Zend_Db_Expr ( ' MIN(cat_pro. position) ' ) ]
2077+ )-> group ( ' e.entity_id ' ) ;
21132078 }
2114- $ this ->_joinFields ['position ' ] = ['table ' => 'cat_pro ' , 'field ' => 'position ' ];
2079+ $ this ->_joinFields ['position ' ] = ['table ' => 'cat_pro ' , 'field ' => 'min_position ' ];
21152080
21162081 return $ this ;
21172082 }
@@ -2370,7 +2335,7 @@ public function addMediaGalleryData()
23702335 $ items = $ this ->getItems ();
23712336 $ linkField = $ this ->getProductEntityMetadata ()->getLinkField ();
23722337
2373- $ select = $ this ->getMediaGalleryResource ()
2338+ $ select = $ this ->mediaGalleryResource
23742339 ->createBatchBaseSelect (
23752340 $ this ->getStoreId (),
23762341 $ this ->getAttribute ('media_gallery ' )->getAttributeId ()
@@ -2392,7 +2357,7 @@ function ($item) use ($linkField) {
23922357 }
23932358
23942359 foreach ($ items as $ item ) {
2395- $ this ->getGalleryReadHandler ()
2360+ $ this ->productGalleryReadHandler
23962361 ->addMediaDataToProduct (
23972362 $ item ,
23982363 $ mediaGalleries [$ item ->getOrigData ($ linkField )] ?? []
@@ -2414,35 +2379,6 @@ public function getProductEntityMetadata()
24142379 return $ this ->metadataPool ->getMetadata (ProductInterface::class);
24152380 }
24162381
2417- /**
2418- * Retrieve GalleryReadHandler
2419- *
2420- * @return GalleryReadHandler
2421- * @deprecated 101.0.1
2422- */
2423- private function getGalleryReadHandler ()
2424- {
2425- if ($ this ->productGalleryReadHandler === null ) {
2426- $ this ->productGalleryReadHandler = ObjectManager::getInstance ()->get (GalleryReadHandler::class);
2427- }
2428- return $ this ->productGalleryReadHandler ;
2429- }
2430-
2431- /**
2432- * Retrieve Media gallery resource.
2433- *
2434- * @deprecated 101.0.1
2435- *
2436- * @return \Magento\Catalog\Model\ResourceModel\Product\Gallery
2437- */
2438- private function getMediaGalleryResource ()
2439- {
2440- if (null === $ this ->mediaGalleryResource ) {
2441- $ this ->mediaGalleryResource = ObjectManager::getInstance ()->get (Gallery::class);
2442- }
2443- return $ this ->mediaGalleryResource ;
2444- }
2445-
24462382 /**
24472383 * Clear collection
24482384 *
@@ -2551,7 +2487,7 @@ private function addIsSaleableAttributeToFilter($condition): self
25512487 foreach ($ columns as $ columnEntry ) {
25522488 list ($ correlationName , $ column , $ alias ) = $ columnEntry ;
25532489 if ($ alias == 'is_saleable ' ) {
2554- if ($ column instanceof \ Zend_Db_Expr) {
2490+ if ($ column instanceof Zend_Db_Expr) {
25552491 $ field = $ column ;
25562492 } else {
25572493 $ connection = $ this ->getSelect ()->getConnection ();
0 commit comments