Skip to content

Commit

Permalink
Feature new category view (#140)
Browse files Browse the repository at this point in the history
* Category view changes

* Rename the new setting

* Fix category link if child is selected
  • Loading branch information
ah-net committed Jan 16, 2024
1 parent 4456e64 commit dc1bf8c
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 68 deletions.
51 changes: 51 additions & 0 deletions Block/LayeredNavigation/RenderLayered/DefaultRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Tweakwise\Magento2Tweakwise\Model\Seo\FilterHelper;
use Magento\Framework\View\Element\Template;
use Magento\Framework\Serialize\Serializer\Json;
use Tweakwise\Magento2TweakwiseExport\Model\Helper;

class DefaultRenderer extends Template
{
Expand Down Expand Up @@ -46,6 +47,8 @@ class DefaultRenderer extends Template
*/
protected $navigationConfig;

protected $helper;

/**
* Constructor
*
Expand All @@ -54,6 +57,7 @@ class DefaultRenderer extends Template
* @param NavigationConfig $navigationConfig
* @param FilterHelper $filterHelper
* @param Json $jsonSerializer
* @param Helper $helper
* @param array $data
*/
public function __construct(
Expand All @@ -62,13 +66,15 @@ public function __construct(
NavigationConfig $navigationConfig,
FilterHelper $filterHelper,
Json $jsonSerializer,
Helper $helper,
array $data = []
) {
parent::__construct($context, $data);
$this->config = $config;
$this->filterHelper = $filterHelper;
$this->jsonSerializer = $jsonSerializer;
$this->navigationConfig = $navigationConfig;
$this->helper = $helper;
}

/**
Expand Down Expand Up @@ -108,8 +114,16 @@ public function getCategoryUrl(Item $item): string
public function getItems()
{
$items = $this->filter->getItems();
$type = $this->filter->getFacet()->getFacetSettings()->getSelectionType();
$maxItems = $this->getMaxItemsShown();

if ($this->config->isCategoryViewDefault() && $type == 'link') {
$result = $this->findCurrentCategory($items);
if (!empty($result)) {
$items = $result;
}
}

/** @var Item $item */
foreach ($items as $index => $item) {
$defaultShow = $index >= $maxItems;
Expand All @@ -119,6 +133,35 @@ public function getItems()
return $items;
}

/**
* @param $items
* @return array
*/
private function findCurrentCategory($items) {
$storeId = $this->filter->getStoreId();
$currentCategory = $this->filter->getLayer()->getCurrentCategory();
$tweakwiseCategoryId = $this->helper->getTweakwiseId($storeId, $currentCategory->getId());

foreach ($items as $index => $item) {
if ($item->getAttribute()->getValue('attributeid') == $tweakwiseCategoryId) {
if (!empty($item->getChildren())) {
return $item->getChildren();
} else {
//current category is the lowest level. Return all items on the same level
return $items;
}
} elseif (!empty($item->getChildren())) {
//check if children are the current category
$result = $this->findCurrentCategory($item->getChildren());
if (!empty($result)) {
return $result;
}
}
}

return [];
}

/**
* @return string
*/
Expand Down Expand Up @@ -232,4 +275,12 @@ public function getUrlKey()
{
return $this->getFacetSettings()->getUrlKey();
}

/**
* @return bool
*/
public function hasDefaultCategoryView()
{
return $this->config->isCategoryViewDefault();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public function setItem(Item $item)
*/
public function hasChildren()
{
//link view should never show children. To be backwards compatible this only happens if the default linkrenderer is enabled.
if ($this->hasDefaultCategoryView()) {
return false;
}

return $this->item->hasChildren();
}

Expand Down
4 changes: 4 additions & 0 deletions Block/LayeredNavigation/RenderLayered/SliderRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Magento\Framework\Pricing\Helper\Data as PriceHelper;
use Magento\Framework\View\Element\Template;
use Magento\Framework\Serialize\Serializer\Json;
use Tweakwise\Magento2TweakwiseExport\Model\Helper;

class SliderRenderer extends DefaultRenderer
{
Expand Down Expand Up @@ -42,6 +43,7 @@ class SliderRenderer extends DefaultRenderer
* @param FilterHelper $filterHelper
* @param Template\Context $context
* @param Json $jsonSerializer
* @param Helper $helper
* @param array $data
*/
public function __construct(
Expand All @@ -52,6 +54,7 @@ public function __construct(
FilterHelper $filterHelper,
Template\Context $context,
Json $jsonSerializer,
Helper $helper,
array $data = []
) {
parent::__construct(
Expand All @@ -60,6 +63,7 @@ public function __construct(
$navigationConfig,
$filterHelper,
$jsonSerializer,
$helper,
$data
);
$this->priceHelper = $priceHelper;
Expand Down
46 changes: 36 additions & 10 deletions Model/Client/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\StoreManager;
use Tweakwise\Magento2Tweakwise\Model\Config;

class Request
{
Expand Down Expand Up @@ -42,16 +43,22 @@ class Request
*/
protected $helper;

/**
* @var Config
*/
protected $config;
/**
* Request constructor.
*
* @param Helper $helper
* @param StoreManager $storeManager
* @param Config $config
*/
public function __construct(Helper $helper, StoreManager $storeManager)
public function __construct(Helper $helper, StoreManager $storeManager, Config $config)
{
$this->helper = $helper;
$this->storeManager = $storeManager;
$this->config = $config;
}

/**
Expand Down Expand Up @@ -179,8 +186,34 @@ public function addCategoryFilter($category)
$ids[] = $category;
return $this->addCategoryPathFilter($ids);
}
/** @var Category $category */
$parentIsRoot = in_array(

if($this->config->isCategoryViewDefault()) {
/** @var Category $category */
$parentIsRoot = $category;
while ($this->isCategoryRoot($parentIsRoot) === false) {
$ids[] = (int)$parentIsRoot->getParentId();
$parentIsRoot = $parentIsRoot->getParentCategory();
}
$ids[] = (int)$parentIsRoot->getParentId();

$ids = array_reverse($ids);
} else {
/** @var Category $category */
$parentIsRoot = $this->isCategoryRoot($category);

if (!$parentIsRoot) {
// Parent category is added so that category menu is retained on the deepest category level
$ids[] = (int) $category->getParentId();
}
}

$ids[] = (int) $category->getId();

return $this->addCategoryPathFilter($ids);
}

private function isCategoryRoot($category) {
return in_array(
(int) $category->getParentId(),
[
0,
Expand All @@ -189,13 +222,6 @@ public function addCategoryFilter($category)
],
true
);
if (!$parentIsRoot) {
// Parent category is added so that category menu is retained on the deepest category level
$ids[] = (int) $category->getParentId();
}
$ids[] = (int) $category->getId();

return $this->addCategoryPathFilter($ids);
}

/**
Expand Down
11 changes: 0 additions & 11 deletions Model/Client/Request/FacetAttributeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,6 @@ class FacetAttributeRequest extends Request
*/
protected $hiddenParameters = [];

/**
* Request constructor.
*
* @param Helper $helper
* @param StoreManager $storeManager
*/
public function __construct(Helper $helper, StoreManager $storeManager)
{
parent::__construct($helper, $storeManager);
}

/**
* {@inheritdoc}
*/
Expand Down
15 changes: 0 additions & 15 deletions Model/Client/Request/ProductSearchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,4 @@ class ProductSearchRequest extends ProductNavigationRequest implements SearchReq
* {@inheritDoc}
*/
protected $path = 'navigation-search';

/**
* ProductSearchRequest constructor.
* @param Helper $helper
* @param StoreManager $storeManager
* @param Config $config
*/
public function __construct(
Helper $helper,
StoreManager $storeManager,
Config $config
) {
parent::__construct($helper, $storeManager);
$this->config = $config;
}
}
5 changes: 3 additions & 2 deletions Model/Client/Request/Recommendations/FeaturedRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Tweakwise\Magento2Tweakwise\Model\Client\Request;
use Tweakwise\Magento2Tweakwise\Model\Client\Response\RecommendationsResponse;
use Tweakwise\Magento2TweakwiseExport\Model\Helper;
use Tweakwise\Magento2Tweakwise\Model\Config;

class FeaturedRequest extends Request
{
Expand All @@ -29,10 +30,10 @@ class FeaturedRequest extends Request

protected $registery;

public function __construct(Helper $helper, StoreManager $storeManager, Registry $registry)
public function __construct(Helper $helper, StoreManager $storeManager, Registry $registry, Config $config)
{
$this->registery = $registry;
parent::__construct($helper, $storeManager);
parent::__construct($helper, $storeManager, $config);
}

/**
Expand Down
15 changes: 0 additions & 15 deletions Model/Client/Request/Suggestions/ProductSuggestionsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,6 @@ class ProductSuggestionsRequest extends Request implements SearchRequestInterfac
*/
protected $path = 'suggestions/products';

/**
* ProductSuggestionRequest constructor.
* @param Helper $helper
* @param StoreManager $storeManager
* @param Config $config
*/
public function __construct(
Helper $helper,
StoreManager $storeManager,
Config $config
) {
parent::__construct($helper, $storeManager);
$this->config = $config;
}

/**
* @return string
*/
Expand Down
15 changes: 0 additions & 15 deletions Model/Client/Request/Suggestions/SuggestionsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,6 @@ class SuggestionsRequest extends Request implements SearchRequestInterface
*/
protected $path = 'suggestions';

/**
* SuggestionRequest constructor.
* @param Helper $helper
* @param StoreManager $storeManager
* @param Config $config
*/
public function __construct(
Helper $helper,
StoreManager $storeManager,
Config $config
) {
parent::__construct($helper, $storeManager);
$this->config = $config;
}

/**
* @return string
*/
Expand Down
9 changes: 9 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -599,4 +599,13 @@ public function getSalt() : string
}
return $salt;
}

/**
* @param Store|null $store
* @return int
*/
public function isCategoryViewDefault(Store $store = null)
{
return $this->getStoreConfig('tweakwise/layered/default_category_view', $store);
}
}

0 comments on commit dc1bf8c

Please sign in to comment.