Skip to content

Commit

Permalink
optimize loading old usages
Browse files Browse the repository at this point in the history
  • Loading branch information
superdav42 committed Sep 4, 2024
1 parent 0efa79c commit 07be069
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions Block/Catalog/Product/Usage.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Usage extends AbstractProduct
protected CollectionFactory $orderCollectionFactory;
protected SortOrderBuilder $sortOrderBuilder;
private DataHelper $config;
private array $previousCategories;

public function __construct(
Context $context,
Expand Down Expand Up @@ -527,6 +528,10 @@ public function getPreviousSelectHtml(): string
*/
public function getPreviousCategories(): array
{
if (isset($this->previousCategories)) {
return $this->previousCategories;
}

$previousCategories = [];
try {
$items = $this->checkoutSession->getQuote()->getAllVisibleItems();
Expand All @@ -544,8 +549,10 @@ public function getPreviousCategories(): array
$ordersCollection = $this->orderCollectionFactory->create()
->addFieldToSelect('*')
->addFieldToFilter('customer_id', $this->getCustomerId())
->addFieldToFilter('created_at', ['from' => '2023-05-01 00:00:00'])
->setPageSize(20 - count($previousCategories))
->setOrder('created_at', 'desc');
$tried = 0;
/**
* @var Order $order
* @var OrderItemInterface $item
Expand All @@ -568,10 +575,11 @@ public function getPreviousCategories(): array
} catch (Exception $e) {
// Probably usage was deleted, skip.
continue;
$tried++;
}

if (count($previousCategories) >= 10) {
break;
if (count($previousCategories) >= 10 || $tried > 20) {
break 2;
}
}
}
Expand All @@ -580,7 +588,8 @@ public function getPreviousCategories(): array
$this->_logger->error(__('Unable to fetch previous categories.'));
$this->_logger->error($e->getMessage());
}
return $previousCategories;
$this->previousCategories = $previousCategories;
return $this->previousCategories;
}

/**
Expand Down

0 comments on commit 07be069

Please sign in to comment.