Last active
July 9, 2018 12:02
-
-
Save luckyduck/f66f597168cc956dd0fe889754f191f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace ...; | |
class LayerPlugin | |
{ | |
const FILTER_ADDED_KEY = 'tierPriceAdded'; | |
/** | |
* @var \Magento\Framework\Registry | |
*/ | |
protected $_coreRegistry; | |
/** | |
* @var \Magento\Framework\App\Request\Http | |
*/ | |
protected $_request; | |
/** | |
* LayerPlugin constructor. | |
* @param \Magento\Framework\Registry $coreRegistry | |
*/ | |
public function __construct( | |
\Magento\Framework\Registry $coreRegistry, | |
\Magento\Framework\App\Request\Http $request | |
) | |
{ | |
$this->_coreRegistry = $coreRegistry; | |
$this->_request = $request; | |
} | |
/** | |
* @param \Magento\Catalog\Model\Layer $layer | |
* @param $collection | |
* @return mixed | |
*/ | |
public function afterGetProductCollection(\Magento\Catalog\Model\Layer $layer, $collection) | |
{ | |
if ($this->_filterAlreadyAdded()) { | |
return $collection; | |
} | |
if ($this->_request->getModuleName() == 'catalogsearch') { | |
return $collection; | |
} | |
$tierPriceSelect = new \Zend_Db_Expr(" | |
(SELECT entity_id, value_id FROM catalog_product_entity_tier_price ip) | |
"); | |
$collection->getSelect()->joinLeft(['pr' => 'catalog_product_relation'], 'pr.parent_id = e.entity_id', []); | |
$collection->getSelect()->joinLeft(['tierprices' => $tierPriceSelect], 'e.entity_id = tierprices.entity_id OR tierprices.entity_id = pr.child_id', []); | |
$collection->getSelect()->group('e.entity_id'); | |
$collection->getSelect()->having("(e.type_id IN ('configurable', 'bundle') AND count(tierprices.entity_id) = count(pr.child_id)) OR (e.type_id NOT IN ('configurable', 'bundle')) AND tier_price IS NOT NULL"); | |
$this->_rememberFilter(); | |
return $collection; | |
} | |
/** | |
* | |
*/ | |
protected function _rememberFilter() | |
{ | |
$this->_coreRegistry->register(self::FILTER_ADDED_KEY, true); | |
} | |
/** | |
* @return mixed | |
*/ | |
protected | |
function _filterAlreadyAdded() | |
{ | |
return $this->_coreRegistry->registry(self::FILTER_ADDED_KEY); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment