Skip to content

Instantly share code, notes, and snippets.

@cergey-obr
Created November 22, 2017 10:31
Show Gist options
  • Save cergey-obr/b6de1f7e32066d47aaf3b8bd50677c83 to your computer and use it in GitHub Desktop.
Save cergey-obr/b6de1f7e32066d47aaf3b8bd50677c83 to your computer and use it in GitHub Desktop.
<?php
class SNQ_Catalog_Helper_Catalog_Product_List extends SNQ_Catalog_Helper_Data
{
const SALE_AMOUNT_SORT_FIELD = 'sale_amount';
const DEFAULT_SORT_FIELD = 'position';
/**
* @param Mage_Catalog_Model_Resource_Product_Collection $collection
* @param string $currentOrder
* @param string $currentDirection
*/
public function prepareCollectionSort($collection, $currentOrder, $currentDirection)
{
$select = $collection->getSelect();
$this->_addColumns($collection);
$this->_setCurrentOrder($select, $currentOrder, $currentDirection);
}
/**
* @param Varien_Db_Select $select
*/
protected function _addColumns($collection)
{
$select = $collection->getSelect();
$select->columns(
sprintf('(`price_index`.`final_price`-`price_index`.`price`) as %s', self::SALE_AMOUNT_SORT_FIELD));
/*$attrTableAlias = 'product_attributes';
if ($collection->isEnabledFlat()) {
$attrTableAlias = 'e';
}
$select->columns(
sprintf("-( IF(FIND_IN_SET(`%s`.`product_collection`,('%s')), 1, 0) * 10 + `at_stock_status1`.`stock_status` ) as %s",
$attrTableAlias,
implode(",", $this->_getNewArrivalsCollections()),
self::NEW_ARRIVAL_SORT_FIELD
)
);*/
}
/**
* @param Varien_Db_Select $select
* @param string $currentOrder
* @param string $currentDirection
*/
protected function _setCurrentOrder($select, $currentOrder, $currentDirection)
{
if($currentOrder == self::SALE_AMOUNT_SORT_FIELD){
$select->order(sprintf('%s %s', $currentOrder, $currentDirection));
}elseif($currentOrder == self::DEFAULT_SORT_FIELD){
//return $this->_getProductCollection();
$productCollectionHelper = Mage::helper('snq_catalog/product_collection');
$activeCollectionNames = $productCollectionHelper->getActiveCollections();
$collections = array_map(function ($activeCollectionName){
return "'" . $activeCollectionName . "'";
}, $activeCollectionNames);
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$select->reset('order')
->joinLeft(
['ps' => 'catalog_product_entity_varchar'],
'ps.attribute_id = 177 AND ps.entity_id = e.entity_id',
['product_collection' => 'ps.value']
)
->joinLeft(
['pr' => 'catalog_product_entity_varchar'],
'pr.attribute_id = 230 AND pr.entity_id = e.entity_id',
['price_rule' => 'pr.value']
)
->order('IF(product_collection IN ('.implode(',' ,$collections).'), 0, 1) '.$currentDirection.', price_rule '.$currentDirection);
} else {
$select
->reset('order')
->order('IF(e.product_collection IN ('.implode(',' ,$collections).'), 0, 1) '.$currentDirection.', e.price_rule '.$currentDirection);
}
}
/*elseif($currentOrder == self::DEFAULT_SORT_FIELD && strtolower($currentDirection) == 'desc'){
$category = Mage::registry('current_category');
if($category && in_array('59', $category->getPathIds())) {
$select->reset('order')
->order(['stock_status DESC', 'price_index.min_price DESC']);
}
}*/
}
/**
* Get new arrivals collections
* @return array|mixed
*/
protected function _getNewArrivalsCollections()
{
/** @var SNQ_Sync_Helper_Data $helper */
$helper = Mage::helper('snq_sync');
return $helper->getSaleLastCollections();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment