magento - 在 Magento 中按类别筛选热门产品

标签 magento magento-1.4

我们想做的事情看起来很简单,我们想展示菜单中每个类别的热门产品。我们希望获得其中 5 个产品,因为这就是 UI 的设计目的。我们希望它们按受欢迎程度排序,我相信这是 Magento 中产品的浏览次数。

在很多情况下,我们请求的菜单类别本身没有产品。因此,它也必须支持为其 child 引入产品。我们的类别布局的一个分支的示例是:

  • 男士,#2(无商品)
    • 衬衫,#5(有元素)
    • 配件,#6(有元素)
    • 裤子,#7(有元素)
    • 等等,#8(有项目)

此代码当前的作用是返回相同的热门产品列表,无论我们传递给过滤器的类别是什么。删除 ->addCategoryFilter(...) 实际上会返回相同的结果。我怀疑如果我们能够解决如何按类别过滤的问题,那么其余的事情就会水到渠成。

$storeId = 1;
$category; // Category Object for id = 2 passed to this code
$productCount = 5;

$products = Mage::getResourceModel('reports/product_collection')
->addOrderedQty()
->addAttributeToSelect('*')
->addAttributeToSelect(array('name','small_image'))
->setStoreId($storeId)
->addStoreFilter($storeId)
->addCategoryFilter($category)
->addViewsCount()
->setPageSize($productCount);

我们对此尝试了一些变体。我不确定 addCategoryFilter(...) 方法是否考虑到子类别。如果没有,那么查询和解决应该很容易。当然,就目前情况而言,它总是返回相同的产品,没有对类别进行过滤......正如他们所说,首先要做的事情。

运行 Magento 1.4.0.1

快速查看产品数据会发现 $products->getFirstItem()->getData() 中的这些键为:

Array
(
[0] => entity_id
[1] => entity_type_id
[2] => attribute_set_id
[3] => type_id
[4] => sku
[5] => has_options
[6] => required_options
[7] => created_at
[8] => updated_at
[9] => name
[10] => url_key
[11] => gift_message_available
[12] => meta_title
[13] => meta_description
[14] => image
[15] => small_image
[16] => thumbnail
[17] => custom_design
[18] => page_layout
[19] => options_container
[20] => url_path
[21] => image_label
[22] => thumbnail_label
[23] => small_image_label
[24] => description
[25] => short_description
[26] => meta_keyword
[27] => custom_layout_update
[28] => weight
[29] => price
[30] => special_price
[31] => cost
[32] => news_from_date
[33] => news_to_date
[34] => special_from_date
[35] => special_to_date
[36] => custom_design_from
[37] => custom_design_to
[38] => exclusive
[39] => size
[40] => color
[41] => status
[42] => visibility
[43] => is_imported
[44] => tax_class_id
[45] => enable_googlecheckout
[46] => is_recurring
[47] => is_salable
[48] => stock_item
) 

遗憾的是没有category_ids

最佳答案

好吧,我找到了一个基本上可以通过缺乏工作和/或多类别过滤来解决的答案。它很脏,但是有用。它基于此处的博客文章:http://asia-connect.com.vn/2009/07/magento-filter-by-multiple-categories/

我不能说我对这个解决方案感到满意。我不明白为什么 Magento 会删除以有用的方式对类别进行过滤的功能。事实上,它似乎根本没有过滤任何东西,这让我的问题更加复杂。请随意提出更明智的解决方案。我很乐意用它替换这个解决方案。

$products = Mage::getResourceModel('reports/product_collection')
        ->addOrderedQty()
        ->addAttributeToSelect('*')
        ->addAttributeToSelect(array('name','small_image'))
        ->setStoreId($storeId)
        ->addStoreFilter($storeId)
//        ->addCategoryFilter($category)
        ->addViewsCount()
        ->setPageSize($productCount);

$alias = 'cat_index';
$categoryCondition = $products->getConnection()->quoteInto(
        $alias.'.product_id=e.entity_id AND '.$alias.'.store_id=? AND ',
        $storeId
);
$cats = array( $category->getId() );
foreach($category->getChildren() as $catChild) {
        $cats[] = $catChild->getId();
}


$categoryCondition.= $alias.'.category_id IN ('.implode(',',$cats).')';

$products->getSelect()->joinInner(
        array($alias => $products->getTable('catalog/category_product_index')),
        $categoryCondition,
        array('position'=>'position')
);

$products->_categoryIndexJoined = true;

关于magento - 在 Magento 中按类别筛选热门产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5476694/

相关文章:

ios - 用于移动应用程序的 Magento REST API OAuth

magento - 向报价项目(购物车中的产品)添加自定义选项?

Magento:应用购物车价格规则,但折扣不会从小计中扣除

soap - 通过 SOAP 调用获取图像缩略图

magento-1.4 - 如何向 Magento 中的所有页面添加 cms 静态 block ?

css - 在 Magento 中为单个页面插入 css 引用的最佳方法

javascript - Magento Custom 模块用于 T 恤设计定制

php - magento 自定义布局更新

php - Magento:无效的方法Mage_Bundle_Block_Catalog_Product_View_Type_Bundle_Option_Radio

php - 在启用 BLOCK_HTML 缓存的情况下检测 Magento .phtml 中的主页