Фильтр Magento для сортировки по большинству отзывов на странице категории
У меня есть требование, когда мне нужно создать фильтр на странице каталога для фильтрации продуктов на основе большинства отзывов. Я хочу добавить его для сортировки по фильтрам на странице списка товаров категории.
Помогите мне, ребята, пожалуйста, я нахожусь под огромным давлением.
2 answers
Коллекция продуктов для определенной категории выбирается этим методом: Mage_Catalog_Model_Category::Получить коллекцию продуктов()
Вам нужно будет изменить описанный выше метод и объединить его с таблицей отзывов, чтобы эта коллекция продуктов теперь также имела доступ к количеству просмотров продукта.
Это может быть хорошей отправной точкой:
Отвечая на комментарии:
Код в этом посте работает для меня довольно идеально, за исключением тот факт, что мне пришлось изменить: getAttributeUsedForSortByArray() вместо getAttributeUsedForSortByArray() в app/code/core/Mage/Catalog/Model/config.php
/**
* Retrieve Attributes Used for Sort by as array
* key = code, value = name
*
* @return array
*/
public function getAttributeUsedForSortByArray()
{
$options = array(
'position' => Mage::helper('catalog')->__('Position'),
'popularity' => Mage::helper('catalog')->__('Popularity')
);
foreach ($this->getAttributesUsedForSortBy() as $attribute) {
/* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */
$options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();
}
return $options;
}
Это запрос для сформированной коллекции:
SELECT `e`.*, `cat_index`.`position` AS `cat_index_position`, `price_index`.`price`, `price_index`.`tax_class_id`, `price_index`.`final_price`, IF(price_index.tier_price IS NOT NULL, LEAST(price_index.min_price, price_index.tier_price), price_index.min_price) AS `minimal_price`, `price_index`.`min_price`, `price_index`.`max_price`, `price_index`.`tier_price`, count(review_id) AS `review` FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_category_product_index` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id=1 AND cat_index.visibility IN(2, 4) AND cat_index.category_id = '18'
INNER JOIN `catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND price_index.website_id = '1' AND price_index.customer_group_id = 0
LEFT JOIN `review` AS `t2` ON t2.entity_pk_value = e.entity_id and t2.entity_id = '1' GROUP BY `e`.`entity_id` ORDER BY `review` asc
И для меня это работает довольно гладко.
Копия
App/code/core/Mage/Catalog/Model/Config.php
До app/code/local/Mage/Catalog/Model/Config.php
И добавьте индекс в список инструментов в getAttributeUsedForSortByArray()
public function getAttributeUsedForSortByArray()
{
$options = array(
'position' => Mage::helper('catalog')->__('Position')
);
foreach ($this->getAttributesUsedForSortBy() as $attribute) {
/* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */
$options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();
}
return $options;
}
До
public function getAttributeUsedForSortByArray()
{
$options = array(
'position' => Mage::helper('catalog')->__('Position'),
'mostview' => Mage::helper('catalog')->__('Most View'),
'review' => Mage::helper('catalog')->__(' Review')
);
foreach ($this->getAttributesUsedForSortBy() as $attribute) {
/* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */
$options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();
}
return $options;
}
Копия
App/code/core/Mage/Catalog/Model/Resource/Product/Collection.php
До
App/code/local/Mage/Catalog/Model/Resource/Product/Collection.php
Добавить новую функцию sortBymostview для большинство просмотров и просмотров для просмотра
public function sortBymostview($dir){
$RecentviewSelect=Mage::getSingleton('core/resource')->getConnection('core_read')
->select()
->from('report_event',array('mycount'=>'COUNT(event_id)','product_id'=>'object_id'))
->where('report_event.event_type_id = 1')
->group('report_event.object_id');
echo $this->getSelect()->joinLeft(array('bs'=>$RecentviewSelect), 'bs.product_id = e.entity_id',
array('mostview' => 'bs.mycount'))->order("mostview $dir");
}
И
public function sortByReview($dir){
$RecentviewSelect=Mage::getSingleton('core/resource')->getConnection('core_read')
->select()
->from($this->getTable('review/review'),array('review_count' => new Zend_Db_Expr('COUNT(*)'),'product_id'=>'entity_pk_value'))
->where("{$this->getTable('review/review')}.status_id = 1")
->group("{$this->getTable('review/review')}.entity_pk_value");
$this->getSelect()->joinLeft(array('bs'=>$RecentviewSelect), 'bs.product_id = e.entity_id',
array('review' => 'bs.review_count'))->order("review_count $dir");
}
И гото
Копия
App/code/core/Mage/Catalog/Block/Product/List/Toolbar.php
До
App/code/local/Mage/Catalog/Block/Product/List/Toolbar.php
public function setCollection($collection)
{
$this->_collection = $collection;
$this->_collection->setCurPage($this->getCurrentPage());
$limit = (int)$this->getLimit();
if ($limit) {
$this->_collection->setPageSize($limit);
}
if($this->getCurrentOrder() == 'mostview'){
$this->_collection->sortBymostview($this->getCurrentDirection());
}
elseif($this->getCurrentOrder() == 'review'){
$this->_collection->sortByReview($this->getCurrentDirection());
}
elseif ($this->getCurrentOrder()) {
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
}
return $this;
}