Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. <?php
  2.  
  3. namespace VendorFilterPreferenceModelLayerFilter;
  4.  
  5. use MagentoFrameworkStdlibCookieCookieMetadataFactory;
  6. use MagentoFrameworkStdlibCookieManagerInterface;
  7.  
  8. /**
  9. * Layer attribute filter
  10. */
  11. class Attribute extends MagentoCatalogSearchModelLayerFilterAttribute {
  12.  
  13. const COOKIE_NAME = 'appliedfilter';
  14. const COOKIE_DURATION = 86400; // One day (86400 seconds)
  15.  
  16. /**
  17. * @param MagentoCatalogModelLayerFilterItemFactory $filterItemFactory
  18. * @param MagentoStoreModelStoreManagerInterface $storeManager
  19. * @param MagentoCatalogModelLayer $layer
  20. * @param MagentoCatalogModelLayerFilterItemDataBuilder $itemDataBuilder
  21. * @param MagentoFrameworkFilterStripTags $tagFilter
  22. * @param MagentoFrameworkStdlibCookieManagerInterface $cookieManager
  23. * @param MagentoFrameworkStdlibCookieCookieMetadataFactory $cookieMetadataFactory
  24. * @param array $data
  25. */
  26.  
  27. public function __construct(
  28. MagentoCatalogModelLayerFilterItemFactory $filterItemFactory, MagentoStoreModelStoreManagerInterface $storeManager, MagentoCatalogModelLayer $layer, MagentoCatalogModelLayerFilterItemDataBuilder $itemDataBuilder, MagentoFrameworkFilterStripTags $tagFilter, CookieManagerInterface $cookieManager, CookieMetadataFactory $cookieMetadataFactory, array $data = []
  29. ) {
  30. parent::__construct(
  31. $filterItemFactory, $storeManager, $layer, $itemDataBuilder, $tagFilter, $data
  32. );
  33. $this->_cookieManager = $cookieManager;
  34. $this->_cookieMetadataFactory = $cookieMetadataFactory;
  35. }
  36.  
  37. /**
  38. * Apply attribute option filter to product collection
  39. *
  40. * @param MagentoFrameworkAppRequestInterface $request
  41. * @return $this
  42. * @throws MagentoFrameworkExceptionLocalizedException
  43. */
  44. public function apply(MagentoFrameworkAppRequestInterface $request) {
  45. $attributeValue = $request->getParam($this->_requestVar);
  46. if (empty($attributeValue))
  47. $cookieValue = '';
  48. $cookieValue = $this->_cookieManager->getCookie(self::COOKIE_NAME);
  49. if ($cookieValue) {
  50. $param = explode('-', $cookieValue);
  51. if ($this->_requestVar == $param[0]) {
  52. $attributeValue = $param[1];
  53. }
  54. }
  55. if (empty($attributeValue) && !is_numeric($attributeValue)) {
  56. return $this;
  57. }
  58. $attribute = $this->getAttributeModel();
  59. /** @var MagentoCatalogSearchModelResourceModelFulltextCollection $productCollection */
  60. $productCollection = $this->getLayer()
  61. ->getProductCollection();
  62.  
  63. /* Set Cookie of Filter---Start */
  64. $cookieValue = $this->_cookieManager->getCookie(self::COOKIE_NAME);
  65. if (!$cookieValue || $cookieValue != $attribute->getAttributeCode() . '-' . $attributeValue) {
  66. $metadata = $this->_cookieMetadataFactory
  67. ->createPublicCookieMetadata()
  68. ->setDuration(self::COOKIE_DURATION);
  69. $this->_cookieManager
  70. ->setPublicCookie(self::COOKIE_NAME, $attribute->getAttributeCode() . '-' . $attributeValue, $metadata);
  71. }
  72. /* Set Cookie of Filter---End */
  73.  
  74. $productCollection->addFieldToFilter($attribute->getAttributeCode(), $attributeValue);
  75. $label = $this->getOptionText($attributeValue);
  76. $this->getLayer()
  77. ->getState()
  78. ->addFilter($this->_createItem($label, $attributeValue));
  79.  
  80. $this->setItems([]); // set items to disable show filtering
  81. return $this;
  82. }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement