@@ -62,6 +62,7 @@ class Usage extends AbstractProduct
62
62
protected CollectionFactory $ orderCollectionFactory ;
63
63
protected SortOrderBuilder $ sortOrderBuilder ;
64
64
private DataHelper $ config ;
65
+ private array $ previousCategories ;
65
66
66
67
public function __construct (
67
68
Context $ context ,
@@ -527,6 +528,10 @@ public function getPreviousSelectHtml(): string
527
528
*/
528
529
public function getPreviousCategories (): array
529
530
{
531
+ if (isset ($ this ->previousCategories )) {
532
+ return $ this ->previousCategories ;
533
+ }
534
+
530
535
$ previousCategories = [];
531
536
try {
532
537
$ items = $ this ->checkoutSession ->getQuote ()->getAllVisibleItems ();
@@ -544,8 +549,10 @@ public function getPreviousCategories(): array
544
549
$ ordersCollection = $ this ->orderCollectionFactory ->create ()
545
550
->addFieldToSelect ('* ' )
546
551
->addFieldToFilter ('customer_id ' , $ this ->getCustomerId ())
552
+ ->addFieldToFilter ('created_at ' , ['from ' => '2023-05-01 00:00:00 ' ])
547
553
->setPageSize (20 - count ($ previousCategories ))
548
554
->setOrder ('created_at ' , 'desc ' );
555
+ $ tried = 0 ;
549
556
/**
550
557
* @var Order $order
551
558
* @var OrderItemInterface $item
@@ -568,10 +575,11 @@ public function getPreviousCategories(): array
568
575
} catch (Exception $ e ) {
569
576
// Probably usage was deleted, skip.
570
577
continue ;
578
+ $ tried ++;
571
579
}
572
580
573
- if (count ($ previousCategories ) >= 10 ) {
574
- break ;
581
+ if (count ($ previousCategories ) >= 10 || $ tried > 20 ) {
582
+ break 2 ;
575
583
}
576
584
}
577
585
}
@@ -580,7 +588,8 @@ public function getPreviousCategories(): array
580
588
$ this ->_logger ->error (__ ('Unable to fetch previous categories. ' ));
581
589
$ this ->_logger ->error ($ e ->getMessage ());
582
590
}
583
- return $ previousCategories ;
591
+ $ this ->previousCategories = $ previousCategories ;
592
+ return $ this ->previousCategories ;
584
593
}
585
594
586
595
/**
0 commit comments