Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Add ajax page update, seo urls, price slider
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Zhavoronkov authored and Nikita Zhavoronkov committed Oct 24, 2017
1 parent ed7f29f commit 370a056
Show file tree
Hide file tree
Showing 31 changed files with 1,477 additions and 191 deletions.
41 changes: 41 additions & 0 deletions Block/AjaxScript.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
namespace Niks\LayeredNavigation\Block;

use Magento\Framework\View\Element\Template;

/**
* Class AjaxScript
* @package Niks_LayeredNavigation
*/
class AjaxScript extends Template
{

/**
* Get JSON config
*
* @return string
*/
public function getAjaxConfig()
{
$config = [
'disabled' => !$this->getIsAjax(),
'filtersContainer' => '#layered-filter-block',
'productsContainer' => '.' . \Niks\LayeredNavigation\Plugin\CategoryViewBlock::PRODUCT_LIST_WRAPPER
];
return json_encode($config);
}

/**
* Get ajax option
*
* @return string
*/
protected function getIsAjax()
{
return $this->_scopeConfig->getValue(
'niks_layered_navigation/general/ajax',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$this->_storeManager->getStore()->getId()
);
}
}
10 changes: 5 additions & 5 deletions Block/LayeredNavigation/RenderLayered.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class RenderLayered extends CoreRender
*/
public function buildUrl($attributeCode, $optionId)
{
$value = $this->filter->getValueAsArray();
$value[] = $optionId;
$value = implode('_', $value);
$query = [$attributeCode => $value];
return $this->_urlBuilder->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true, '_query' => $query]);
return $this->_urlBuilder->getFilterUrl(
$this->filter->getRequestVar(),
$optionId,
[]
);
}
}
53 changes: 53 additions & 0 deletions Block/LayeredNavigation/SliderRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Niks\LayeredNavigation\Block\LayeredNavigation;

use Magento\Framework\View\Element\Template;

/**
* Class RenderLayered Render Swatches at Layered Navigation
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class SliderRenderer extends Template
{
/**
* Path to template file.
*
* @var string
*/
protected $_template = 'Niks_LayeredNavigation::slider.phtml';

public function getFrom()
{
$currentValues = $this->getFilter()->getCurrentValue();
if (isset($currentValues[0])) {
return $currentValues[0];
}
return $this->getFilter()->getMin();
}

public function getTo()
{
$currentValues = $this->getFilter()->getCurrentValue();
if (isset($currentValues[1])) {
return $currentValues[1];
}
return $this->getFilter()->getMax();
}

public function getPriceRangeUrlTemplate()
{
return $this->_urlBuilder->getFilterUrl(
$this->getFilter()->getRequestVar(),
'{{from}}-{{to}}',
[],
true
);
}

public function getCurrencySymbol()
{
return $this->_storeManager->getStore()->getCurrentCurrency()->getCurrencySymbol();
}
}
84 changes: 84 additions & 0 deletions Controller/Router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
namespace Niks\LayeredNavigation\Controller;

use Magento\Framework\UrlInterface;
use Magento\UrlRewrite\Model\UrlFinderInterface;
use Niks\LayeredNavigation\Model\Url\Builder;
use Niks\LayeredNavigation\Model\Url\Hydrator;

/**
* Class Router
* @package Niks_LayeredNavigation
*/
class Router extends \Magento\UrlRewrite\Controller\Router implements \Magento\Framework\App\RouterInterface
{
/** @var \Niks\LayeredNavigation\Model\Url\Hydrator */
protected $urlHydrator;

/** @var \Magento\Framework\Registry */
protected $registry;

/**
* @param \Magento\Framework\App\ActionFactory $actionFactory
* @param \Magento\Framework\UrlInterface $url
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\App\ResponseInterface $response
* @param UrlFinderInterface $urlFinder
* @param Hydrator $urlHydrator
* @param \Magento\Framework\Registry $registry
*/
public function __construct(
\Magento\Framework\App\ActionFactory $actionFactory,
\Magento\Framework\UrlInterface $url,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\ResponseInterface $response,
UrlFinderInterface $urlFinder,
Hydrator $urlHydrator,
\Magento\Framework\Registry $registry
)
{
$this->urlHydrator = $urlHydrator;
$this->registry = $registry;
parent::__construct($actionFactory, $url, $storeManager, $response, $urlFinder);
}

/**
* Match corresponding navigation URL and modify request
*
* @param \Magento\Framework\App\RequestInterface $request
* @return \Magento\Framework\App\ActionInterface|null
*/
public function match(\Magento\Framework\App\RequestInterface $request)
{
$parentMatch = parent::match($request);
if ($parentMatch !== null) {
$request->setAlias(
Builder::REWRITE_NAVIGATION_PATH_ALIAS,
ltrim($request->getOriginalPathInfo(), '/')
);
return $parentMatch;
}

$filterString = '/' . $this->urlHydrator->getFilterString($request->getPathInfo());
$originalPath = preg_replace('%' . $filterString . '(?!.*' . $filterString . '.*)%', '', $request->getPathInfo());

$rewrite = $this->getRewrite($originalPath, $this->storeManager->getStore()->getId());
if ($rewrite === null) {
return null;
}
if ($rewrite->getRedirectType()) {
return $this->processRedirect($request, $rewrite);
}

$this->registry->register('current_category_id', $rewrite->getEntityId());
$filterParams = $this->urlHydrator->extract($request->getPathInfo());
if (empty($filterParams)) {
return null;
}
$request->setParam('navigation_filters', $filterParams);
$request->setAlias(UrlInterface::REWRITE_REQUEST_PATH_ALIAS, ltrim($request->getPathInfo(), '/'));
$request->setAlias(Builder::REWRITE_NAVIGATION_PATH_ALIAS, $rewrite->getRequestPath());
$request->setPathInfo('/' . $rewrite->getTargetPath());
return $this->actionFactory->create(\Magento\Framework\App\Action\Forward::class);
}
}
64 changes: 14 additions & 50 deletions Model/Layer/Filter/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
class Attribute extends CoreAttribute
{
/**
* @var \Magento\Framework\App\RequestInterface
* @var \Magento\Framework\Filter\StripTags
*/
protected $_request;
private $tagFilter;

/**
* @var \Magento\Framework\Filter\StripTags
* @var \\Niks\LayeredNavigation\Model\Url\Builder
*/
private $tagFilter;
protected $urlBuilder;

/**
* @var \Magento\CatalogSearch\Model\Layer\Category\ItemCollectionProvider
Expand All @@ -36,6 +36,7 @@ public function __construct(
\Magento\Catalog\Model\Layer $layer,
\Magento\Catalog\Model\Layer\Filter\Item\DataBuilder $itemDataBuilder,
\Magento\Framework\Filter\StripTags $tagFilter,
\Niks\LayeredNavigation\Model\Url\Builder $urlBuilder,
\Magento\CatalogSearch\Model\Layer\Category\ItemCollectionProvider $collectionProvider,
array $data = []
) {
Expand All @@ -48,17 +49,10 @@ public function __construct(
$data
);
$this->tagFilter = $tagFilter;
$this->urlBuilder = $urlBuilder;
$this->collectionProvider = $collectionProvider;
}

/**
* @return \Magento\Framework\App\RequestInterface
*/
protected function _getRequest()
{
return $this->_request;
}

/**
* Apply attribute option filter to product collection
*
Expand All @@ -68,8 +62,8 @@ protected function _getRequest()
*/
public function apply(\Magento\Framework\App\RequestInterface $request)
{
$this->_request = $request;
if (empty($request->getParam($this->_requestVar))) {
$values = $this->urlBuilder->getValuesFromUrl($this->_requestVar);
if (!$values) {
return $this;
}

Expand All @@ -78,8 +72,7 @@ public function apply(\Magento\Framework\App\RequestInterface $request)
->getProductCollection();
$this->applyToCollection($productCollection);

$attributeValues = $this->getValueAsArray();
foreach ($attributeValues as $value) {
foreach ($values as $value) {
$label = $this->getOptionText($value);
$this->getLayer()
->getState()
Expand All @@ -88,21 +81,6 @@ public function apply(\Magento\Framework\App\RequestInterface $request)
return $this;
}

/**
* Get filter values
*
* @return array
*/
public function getValueAsArray()
{
$paramValue = $this->_getRequest()->getParam($this->_requestVar);
if (!$paramValue) {
return [];
}
$requestValue = $this->_getRequest()->getParam($this->_requestVar);
return explode('_', $requestValue);
}

/**
* Apply current filter to collection
*
Expand All @@ -111,28 +89,13 @@ public function getValueAsArray()
public function applyToCollection($collection)
{
$attribute = $this->getAttributeModel();
$attributeValue = $this->getValueAsArray();
$attributeValue = $this->urlBuilder->getValuesFromUrl($this->_requestVar);
if (empty($attributeValue)) {
return $this;
}
$collection->addFieldToFilter($attribute->getAttributeCode(), array('in' => $attributeValue));
}

/**
* Get filter value for reset current filter state
*
* @param string $value
* @return string
*/
public function getResetOptionValue($value)
{
$attributeValues = $this->getValueAsArray();
$key = array_search($value, $attributeValues);
unset($attributeValues[$key]);
return implode('_', $attributeValues);
}


/**
* Get data array for building attribute filter items
*
Expand All @@ -141,7 +104,8 @@ public function getResetOptionValue($value)
*/
protected function _getItemsData()
{
if (!$this->_getRequest()->getParam($this->_requestVar)) {
$values = $this->urlBuilder->getValuesFromUrl($this->_requestVar);
if (!$values) {
return parent::_getItemsData();
}

Expand Down Expand Up @@ -176,9 +140,9 @@ protected function _getItemsData()

$options = $attribute->getFrontend()
->getSelectOptions();
$usedOptions = $this->getValueAsArray();

foreach ($options as $option) {
if (empty($option['value']) || in_array($option['value'], $usedOptions)) {
if (empty($option['value']) || in_array($option['value'], $values)) {
continue;
}
// Check filter type
Expand Down
Loading

0 comments on commit 370a056

Please sign in to comment.