-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSaveSize.php
70 lines (60 loc) · 2.21 KB
/
SaveSize.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
<?php
/**
* @author Krishaweb Team
* @package Krishaweb_AdvanceFilter
*/
namespace Krishaweb\AdvanceFilter\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Stdlib\CookieManagerInterface;
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
class SaveSize implements ObserverInterface
{
protected $cookieManager;
protected $cookieMetadataFactory;
public function __construct(
\Magento\Framework\App\RequestInterface $request,
CookieManagerInterface $cookieManager,
CookieMetadataFactory $cookieMetadataFactory
)
{
$this->_request = $request;
$this->cookieManager = $cookieManager;
$this->cookieMetadataFactory = $cookieMetadataFactory;
}
public function execute(Observer $observer)
{
$request = $this->_request;
$handleName = $request->getFullActionName();
$params = $request->getParams();
// check if remove size is triggered
if(isset($params['remove_size'])){
$metadata = $this->cookieMetadataFactory
->createPublicCookieMetadata()
->setPath('/')
->setDuration(time()-3600);
$this->cookieManager
->setPublicCookie('size', "", $metadata);
$size = array('size'=>"");
$request->setParams($size);
}else{
// if the user size is already stored
$cookieValue = $this->cookieManager->getCookie('size');
if(isset($cookieValue)){
$size = array('size'=>$cookieValue);
$request->setParams($size);
}else{
if($handleName == 'catalog_category_view'){
if(isset($params['size'])){
$metadata = $this->cookieMetadataFactory
->createPublicCookieMetadata()
->setPath('/')
->setDuration(time()+30*24*60*60);
$this->cookieManager
->setPublicCookie('size', $params['size'], $metadata);
}
}
}
}
}
}