Layer.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogInventory\Model\Plugin;
  7. class Layer
  8. {
  9. /**
  10. * Stock status instance
  11. *
  12. * @var \Magento\CatalogInventory\Helper\Stock
  13. */
  14. protected $stockHelper;
  15. /**
  16. * Store config instance
  17. *
  18. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  19. */
  20. protected $scopeConfig;
  21. /**
  22. * @param \Magento\CatalogInventory\Helper\Stock $stockHelper
  23. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  24. */
  25. public function __construct(
  26. \Magento\CatalogInventory\Helper\Stock $stockHelper,
  27. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  28. ) {
  29. $this->stockHelper = $stockHelper;
  30. $this->scopeConfig = $scopeConfig;
  31. }
  32. /**
  33. * Before prepare product collection handler
  34. *
  35. * @param \Magento\Catalog\Model\Layer $subject
  36. * @param \Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection $collection
  37. *
  38. * @return void
  39. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  40. */
  41. public function beforePrepareProductCollection(
  42. \Magento\Catalog\Model\Layer $subject,
  43. \Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection $collection
  44. ) {
  45. if ($this->_isEnabledShowOutOfStock()) {
  46. return;
  47. }
  48. $this->stockHelper->addIsInStockFilterToCollection($collection);
  49. }
  50. /**
  51. * Get config value for 'display out of stock' option
  52. *
  53. * @return bool
  54. */
  55. protected function _isEnabledShowOutOfStock()
  56. {
  57. return $this->scopeConfig->isSetFlag(
  58. 'cataloginventory/options/show_out_of_stock',
  59. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  60. );
  61. }
  62. }