Skip to content

Commit 07be069

Browse files
committed
optimize loading old usages
1 parent 0efa79c commit 07be069

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

Block/Catalog/Product/Usage.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class Usage extends AbstractProduct
6262
protected CollectionFactory $orderCollectionFactory;
6363
protected SortOrderBuilder $sortOrderBuilder;
6464
private DataHelper $config;
65+
private array $previousCategories;
6566

6667
public function __construct(
6768
Context $context,
@@ -527,6 +528,10 @@ public function getPreviousSelectHtml(): string
527528
*/
528529
public function getPreviousCategories(): array
529530
{
531+
if (isset($this->previousCategories)) {
532+
return $this->previousCategories;
533+
}
534+
530535
$previousCategories = [];
531536
try {
532537
$items = $this->checkoutSession->getQuote()->getAllVisibleItems();
@@ -544,8 +549,10 @@ public function getPreviousCategories(): array
544549
$ordersCollection = $this->orderCollectionFactory->create()
545550
->addFieldToSelect('*')
546551
->addFieldToFilter('customer_id', $this->getCustomerId())
552+
->addFieldToFilter('created_at', ['from' => '2023-05-01 00:00:00'])
547553
->setPageSize(20 - count($previousCategories))
548554
->setOrder('created_at', 'desc');
555+
$tried = 0;
549556
/**
550557
* @var Order $order
551558
* @var OrderItemInterface $item
@@ -568,10 +575,11 @@ public function getPreviousCategories(): array
568575
} catch (Exception $e) {
569576
// Probably usage was deleted, skip.
570577
continue;
578+
$tried++;
571579
}
572580

573-
if (count($previousCategories) >= 10) {
574-
break;
581+
if (count($previousCategories) >= 10 || $tried > 20) {
582+
break 2;
575583
}
576584
}
577585
}
@@ -580,7 +588,8 @@ public function getPreviousCategories(): array
580588
$this->_logger->error(__('Unable to fetch previous categories.'));
581589
$this->_logger->error($e->getMessage());
582590
}
583-
return $previousCategories;
591+
$this->previousCategories = $previousCategories;
592+
return $this->previousCategories;
584593
}
585594

586595
/**

0 commit comments

Comments
 (0)