Skip to content

Commit

Permalink
Merge pull request Sylius#9621 from bartoszpietrzak1994/taxon-with-ch…
Browse files Browse the repository at this point in the history
…ildren-taxons-behavior-in-listing

Taxon with children taxons behavior in listing
  • Loading branch information
pamil authored Aug 15, 2018
2 parents 1829d35 + 8102395 commit 2760de2
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 5 deletions.
4 changes: 4 additions & 0 deletions app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@ liip_imagine:
web_path:
web_root: "%kernel.project_dir%/web"
cache_prefix: "media/cache"

sylius_shop:
product_grid:
include_all_descendants: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@viewing_products
Feature: Viewing products from taxon children
In order to browse products from general taxons
As a Visitor
I want to be able to view products from taxon children

Background:
Given the store has currency "Euro"
And the store operates on a channel named "Poland"
And the store classifies its products as "T-Shirts"
And the "T-Shirts" taxon has children taxon "Men" and "Women"
And the "Men" taxon has children taxon "XL" and "XXL"
And the store has a product "T-Shirt Banana" available in "Poland" channel
And this product belongs to "T-Shirts"
And the store has a product "T-Shirt Banana For Men" available in "Poland" channel
And this product belongs to "Men"
And the store has a product "T-Shirt Banana For Men XXL" available in "Poland" channel
And this product belongs to "XXL"

@ui
Scenario: Viewing products from taxon children
When I browse products from taxon "T-Shirts"
Then I should see the product "T-Shirt Banana"
And I should see the product "T-Shirt Banana For Men"
And I should see the product "T-Shirt Banana For Men XXL"
26 changes: 22 additions & 4 deletions src/Sylius/Bundle/CoreBundle/Doctrine/ORM/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,35 @@ public function createShopListQueryBuilder(
ChannelInterface $channel,
TaxonInterface $taxon,
string $locale,
array $sorting = []
array $sorting = [],
bool $includeAllDescendants = false
): QueryBuilder {
$queryBuilder = $this->createQueryBuilder('o')
->addSelect('translation')
->innerJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale')
->innerJoin('o.productTaxons', 'productTaxon')
->andWhere('productTaxon.taxon = :taxon')
->innerJoin('o.productTaxons', 'productTaxon');

if ($includeAllDescendants) {
$queryBuilder
->innerJoin('productTaxon.taxon', 'taxon')
->andWhere('taxon.left >= :taxonLeft')
->andWhere('taxon.right <= :taxonRight')
->andWhere('taxon.root = :taxonRoot')
->setParameter('taxonLeft', $taxon->getLeft())
->setParameter('taxonRight', $taxon->getRight())
->setParameter('taxonRoot', $taxon->getRoot())
;
} else {
$queryBuilder
->andWhere('productTaxon.taxon = :taxon')
->setParameter('taxon', $taxon)
;
}

$queryBuilder
->andWhere(':channel MEMBER OF o.channels')
->andWhere('o.enabled = true')
->setParameter('locale', $locale)
->setParameter('taxon', $taxon)
->setParameter('channel', $channel)
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()
->end()
->arrayNode('product_grid')
->addDefaultsIfNotSet()
->children()
->booleanNode('include_all_descendants')
->defaultFalse()
->end()
->end()
->end()
->end()
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public function load(array $config, ContainerBuilder $container): void
$loader->load(sprintf('services/integrations/locale/%s.xml', $config['locale_switcher']));

$container->setParameter('sylius_shop.firewall_context_name', $config['firewall_context_name']);
$container->setParameter(
'sylius_shop.product_grid.include_all_descendants',
$config['product_grid']['include_all_descendants']
);
$this->configureCheckoutResolverIfNeeded($config['checkout_resolver'], $container);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ sylius_grid:
taxon: "expr:notFoundOnNull(service('sylius.repository.taxon').findOneBySlug($slug, service('sylius.context.locale').getLocaleCode()))"
locale: "expr:service('sylius.context.locale').getLocaleCode()"
sorting: "expr:service('request_stack').getCurrentRequest().get('sorting', [])"
includeAllDescendants: "expr:parameter('sylius_shop.product_grid.include_all_descendants')"
sorting:
position: asc
limits: [9, 18, 27]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,17 @@ public function createListQueryBuilder(string $locale, $taxonId = null): QueryBu
* @param TaxonInterface $taxon
* @param string $locale
* @param array $sorting
* @param bool $includeAllDescendants
*
* @return QueryBuilder
*/
public function createShopListQueryBuilder(ChannelInterface $channel, TaxonInterface $taxon, string $locale, array $sorting = []): QueryBuilder;
public function createShopListQueryBuilder(
ChannelInterface $channel,
TaxonInterface $taxon,
string $locale,
array $sorting = [],
bool $includeAllDescendants = false
): QueryBuilder;

/**
* @param ChannelInterface $channel
Expand Down

0 comments on commit 2760de2

Please sign in to comment.