-
Notifications
You must be signed in to change notification settings - Fork 16
/
Plugin.php
145 lines (124 loc) · 4 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* Tweakwise (https://www.tweakwise.com/) - All Rights Reserved
*
* @copyright Copyright (c) 2017-2022 Tweakwise.com B.V. (https://www.tweakwise.com)
* @license Proprietary and confidential, Unauthorized copying of this file, via any medium is strictly prohibited
*/
namespace Tweakwise\Magento2Tweakwise\Block\Catalog\Product\ProductList\Toolbar;
use Closure;
use Magento\CatalogInventory\Api\StockConfigurationInterface;
use Magento\Framework\Exception\LocalizedException;
use Psr\Log\LoggerInterface;
use Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\NavigationContext\CurrentContext;
use Tweakwise\Magento2Tweakwise\Model\Client\Request\ProductSearchRequest;
use Tweakwise\Magento2Tweakwise\Model\Client\Type\SortFieldType;
use Tweakwise\Magento2Tweakwise\Model\Config;
use Magento\Catalog\Block\Product\ProductList\Toolbar;
class Plugin
{
/**
* @var CurrentContext
*/
protected $context;
/**
* @var Config
*/
protected $config;
/**
* @var StockConfigurationInterface
*/
private $stockConfiguration;
/**
* @var LoggerInterface
*/
private $logger;
/**
* Plugin constructor.
*
* @param Config $config
* @param CurrentContext $context
* @param StockConfigurationInterface $stockConfiguration
* @param LoggerInterface $logger
*/
public function __construct(
Config $config,
CurrentContext $context,
StockConfigurationInterface $stockConfiguration,
LoggerInterface $logger
) {
$this->context = $context;
$this->config = $config;
$this->stockConfiguration = $stockConfiguration;
$this->logger = $logger;
}
/**
* @param Toolbar $subject
* @param Closure $proceed
* @return array
*/
public function aroundGetAvailableOrders(Toolbar $subject, Closure $proceed)
{
if (!$this->config->isLayeredEnabled()) {
if (!$this->config->isSearchEnabled() || !($this->context->getRequest() instanceof ProductSearchRequest)) {
return $proceed();
}
}
//page is search and search is not enabled
if ((!$this->config->isSearchEnabled()) && ($this->context->getRequest() instanceof ProductSearchRequest)) {
return $proceed();
}
if (empty($this->context->getResponse())) {
return $proceed();
}
/** @var SortFieldType[] $sortFields */
$sortFields = $this->context->getResponse()->getProperties()->getSortFields();
$result = [];
foreach ($sortFields as $field) {
$result[$field->getUrlValue()] = $field->getDisplayTitle();
}
return $result;
}
/**
* @param Toolbar $subject
* @param string $result
* @return false|string
*/
public function afterGetWidgetOptionsJson(Toolbar $subject, string $result)
{
if (!$this->config->isAjaxFilters()) {
return $result;
}
$options = json_decode($result, true);
$options['productListToolbarForm']['ajaxFilters'] = true;
return json_encode($options);
}
/**
* Update toolbar count if store is in single source mode
* Used in the commerce version
*
* @param Toolbar $subject
* @param callable $proceed
* @return int
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @throws LocalizedException
*/
public function aroundGetTotalNum(Toolbar $subject, callable $proceed): int
{
if (!$this->config->isLayeredEnabled()) {
return $proceed();
}
if ($this->stockConfiguration->isShowOutOfStock()) {
try {
$response = $this->context->getResponse();
$result = $response->getProperties()->getNumberOfItems();
} catch (\Exception $e) {
$this->logger->critical($e->getMessage());
}
}
if (!isset($result)) {
return $proceed();
}
return $result;
}
}