RenderLayered.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Mageplaza
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Mageplaza.com license that is
  8. * available through the world-wide-web at this URL:
  9. * https://www.mageplaza.com/LICENSE.txt
  10. *
  11. * DISCLAIMER
  12. *
  13. * Do not edit or add to this file if you wish to upgrade this extension to newer
  14. * version in the future.
  15. *
  16. * @category Mageplaza
  17. * @package Mageplaza_LayeredNavigation
  18. * @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
  19. * @license https://www.mageplaza.com/LICENSE.txt
  20. */
  21. namespace Mageplaza\LayeredNavigation\Plugin\Block\Swatches;
  22. /**
  23. * Class RenderLayered
  24. * @package Mageplaza\LayeredNavigation\Block\Plugin\Swatches
  25. */
  26. class RenderLayered
  27. {
  28. /** @var \Magento\Framework\UrlInterface */
  29. protected $_url;
  30. /** @var \Magento\Theme\Block\Html\Pager */
  31. protected $_htmlPagerBlock;
  32. /** @var \Mageplaza\LayeredNavigation\Helper\Data */
  33. protected $_moduleHelper;
  34. /** @type \Magento\Catalog\Model\Layer\Filter\AbstractFilter */
  35. protected $filter;
  36. /**
  37. * RenderLayered constructor.
  38. *
  39. * @param \Magento\Framework\UrlInterface $url
  40. * @param \Magento\Theme\Block\Html\Pager $htmlPagerBlock
  41. * @param \Mageplaza\LayeredNavigation\Helper\Data $moduleHelper
  42. */
  43. public function __construct(
  44. \Magento\Framework\UrlInterface $url,
  45. \Magento\Theme\Block\Html\Pager $htmlPagerBlock,
  46. \Mageplaza\LayeredNavigation\Helper\Data $moduleHelper
  47. )
  48. {
  49. $this->_url = $url;
  50. $this->_htmlPagerBlock = $htmlPagerBlock;
  51. $this->_moduleHelper = $moduleHelper;
  52. }
  53. /**
  54. * @param \Magento\Swatches\Block\LayeredNavigation\RenderLayered $subject
  55. * @param \Magento\Catalog\Model\Layer\Filter\AbstractFilter $filter
  56. * @return array
  57. */
  58. public function beforeSetSwatchFilter(\Magento\Swatches\Block\LayeredNavigation\RenderLayered $subject, \Magento\Catalog\Model\Layer\Filter\AbstractFilter $filter)
  59. {
  60. $this->filter = $filter;
  61. return [$filter];
  62. }
  63. /**
  64. * @param \Magento\Swatches\Block\LayeredNavigation\RenderLayered $subject
  65. * @param $proceed
  66. * @param $attributeCode
  67. * @param $optionId
  68. *
  69. * @return string
  70. */
  71. public function aroundBuildUrl(
  72. \Magento\Swatches\Block\LayeredNavigation\RenderLayered $subject,
  73. $proceed,
  74. $attributeCode,
  75. $optionId
  76. )
  77. {
  78. if (!$this->_moduleHelper->isEnabled()) {
  79. return $proceed($attributeCode, $optionId);
  80. }
  81. $attHelper = $this->_moduleHelper->getFilterModel();
  82. if ($attHelper->isMultiple($this->filter)) {
  83. $value = $attHelper->getFilterValue($this->filter);
  84. if (!in_array($optionId, $value)) {
  85. $value[] = $optionId;
  86. } else {
  87. $key = array_search($optionId, $value);
  88. if ($key !== false) {
  89. unset($value[$key]);
  90. }
  91. }
  92. } else {
  93. $value = [$optionId];
  94. }
  95. //Sort param on Url
  96. sort($value);
  97. $query = !empty($value) ? [$attributeCode => implode(',', $value)] : '';
  98. return $this->_url->getUrl(
  99. '*/*/*',
  100. ['_current' => true, '_use_rewrite' => true, '_query' => $query]
  101. );
  102. }
  103. }