Skip to content

Commit

Permalink
Improve product and taxon repository requests to list objects
Browse files Browse the repository at this point in the history
  • Loading branch information
maximehuran committed Apr 9, 2024
1 parent 49df1f8 commit 72762e7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/Provider/ProductUrlProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ public function __construct(

public function getItems(string $locale): array
{
$products = $this->productRepository->findBy(['enabled' => true]);
$products = $this->productRepository->createListQueryBuilder($locale)
->andWhere('o.enabled = :enabled')
->setParameter('enabled', true)
->getQuery()
->getResult()
;

$items = [];
/** @var ProductInterface $product */
foreach ($products as $product) {
$product->setCurrentLocale($locale); // To have translated name and slug with given locale
$items[] = [
'name' => $product->getName(),
'value' => $this->router->generate('sylius_shop_product_show', ['slug' => $product->getSlug(), '_locale' => $locale]),
Expand Down
10 changes: 8 additions & 2 deletions src/Provider/TaxonUrlProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,21 @@ public function __construct(

public function getItems(string $locale): array
{
$taxons = $this->taxonRepository->findBy(['enabled' => true]);
$taxons = $this->taxonRepository->createListQueryBuilder()
->andWhere('translation.locale = :locale')
->andWhere('o.enabled = :enabled')
->setParameter('locale', $locale)
->setParameter('enabled', true)
->getQuery()
->getResult()
;

$items = [];
/** @var TaxonInterface $taxon */
foreach ($taxons as $taxon) {
if ($taxon->isRoot()) {
continue;
}
$taxon->setCurrentLocale($locale); // To have translated name and slug with given locale
$items[] = [
'name' => $this->getTaxonFullName($taxon),
'value' => $this->router->generate('sylius_shop_product_index', ['slug' => $taxon->getSlug(), '_locale' => $locale]),
Expand Down

0 comments on commit 72762e7

Please sign in to comment.