@@ -66,6 +66,11 @@ class Product extends AbstractResource
66
66
*/
67
67
protected $ defaultAttributes ;
68
68
69
+ /**
70
+ * @var array
71
+ */
72
+ protected $ availableCategoryIdsCache = [];
73
+
69
74
/**
70
75
* @param \Magento\Eav\Model\Entity\Context $context
71
76
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
@@ -465,18 +470,22 @@ public function getAvailableInCategories($object)
465
470
{
466
471
// is_parent=1 ensures that we'll get only category IDs those are direct parents of the product, instead of
467
472
// fetching all parent IDs, including those are higher on the tree
468
- $ select = $ this ->getConnection ()->select ()->distinct ()->from (
469
- $ this ->getTable ('catalog_category_product_index ' ),
470
- ['category_id ' ]
471
- )->where (
472
- 'product_id = ? AND is_parent = 1 ' ,
473
- (int )$ object ->getEntityId ()
474
- )->where (
475
- 'visibility != ? ' ,
476
- \Magento \Catalog \Model \Product \Visibility::VISIBILITY_NOT_VISIBLE
477
- );
478
-
479
- return $ this ->getConnection ()->fetchCol ($ select );
473
+ $ entityId = (int )$ object ->getEntityId ();
474
+ if (!isset ($ this ->availableCategoryIdsCache [$ entityId ])) {
475
+ $ this ->availableCategoryIdsCache [$ entityId ] = $ this ->getConnection ()->fetchCol (
476
+ $ this ->getConnection ()->select ()->distinct ()->from (
477
+ $ this ->getTable ('catalog_category_product_index ' ),
478
+ ['category_id ' ]
479
+ )->where (
480
+ 'product_id = ? AND is_parent = 1 ' ,
481
+ $ entityId
482
+ )->where (
483
+ 'visibility != ? ' ,
484
+ \Magento \Catalog \Model \Product \Visibility::VISIBILITY_NOT_VISIBLE
485
+ )
486
+ );
487
+ }
488
+ return $ this ->availableCategoryIdsCache [$ entityId ];
480
489
}
481
490
482
491
/**
0 commit comments