在 Magento 中按类别筛选热门产品
我们想做的事情似乎很简单,我们想展示菜单中每个类别的热门产品。我们希望获得其中 5 个产品,因为这就是 UI 的设计目的。我们希望它们按受欢迎程度排序,我相信这是 Magento 中产品的浏览次数。
在很多情况下,我们请求的菜单类别本身没有产品。因此,它也必须支持为其孩子引入产品。我们的类别布局的一个分支示例是:
- 男士,#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
What we are try to do seems simple enough, we want to show the popular products from each category within our menu. We're looking to get 5 of those products, since that's what the UI is designed to handle. We would like them sorted by popularity, which I believe is number of views on the product in Magento.
It many cases, the menu category we're requesting has no products for itself. So, it has to support pulling in products for it's children as well. Example of one branch of our category layout is:
- Mens, #2 (no items)
- Shirts, #5 (has items)
- Accessories, #6 (has items)
- Pants, #7 (has items)
- Etc, #8 (has items)
What this code currently does, is return the same popular products list regardless of the Category we pass it to filter. Removing the ->addCategoryFilter(...) effectively returns the same results. I suspect if we can solve how to filter by Categories the rest will fall into place.
$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);
We've tried a few variations on this. I'm not sure if the addCategoryFilter(...) method takes into account children categories or not. If not, that should be easy enough to query out and solve. Of course, as it stands right now it just always returns the same products with no filtering on category... first things first as they say.
Running Magento 1.4.0.1
A quick look at the data for products shows these keys in the $products->getFirstItem()->getData() to be:
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
)
Sadly no category_ids
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我找到了一个答案,基本上可以通过缺乏工作和/或多类别过滤来解决。它很脏,但是有用。它基于此处的博客文章: http:// /asia-connect.com.vn/2009/07/magento-filter-by-multiple-categories/
我不能说我对这个解决方案感到满意。我不明白为什么 Magento 会删除以有用的方式对类别进行过滤的功能。事实上,它似乎根本没有过滤任何东西,这让我的问题更加复杂。请随意提出更明智的解决方案。我很乐意用它替换这个解决方案。
Alright, well I found an answer that basically works through the lack of working and/or multiple category filtering. It's dirty, but works. It's based off a blog post from here: http://asia-connect.com.vn/2009/07/magento-filter-by-multiple-categories/
I can't say I'm happy with this solution. I can't figure out why Magento would remove the ability to do filtering on Categories in a useful manner. The fact that it didn't appear to filter anything at all just compounded my issues. Feel free to chime in with a more sensible solution. I'd gladly replace this solution with it.
如果您只有一个类别,则可以使用 addCategoryFilter。产品的类别 ID 以逗号分隔的方式存储。因此,要按多个类别过滤产品集合,您必须使用:
$categoryIds 可以是单个类别 ID 或逗号分隔的类别 ID。
以下代码按两个类别 ID(10 和 15)过滤产品集合。
当我们使用finset时,Magento.mysql函数find_in_set在sql查询中使用mysql函数find_in_set:
FIND_IN_SET()在另一个包含逗号分隔值的字符串中查找字符串的第一次出现。
You can use addCategoryFilter if you have only one category. Category ids are stored for product in a comma separated way. So, to filter product collection by more than one category, you have to use:
$categoryIds can be a single category id or comma separated category ids.
The following code filters product collection by two category ids (10 and 15).
when we use finset, the mysql function find_in_set is used in the sql query by Magento.mysql function find_in_set:
FIND_IN_SET() looks for the first occurrence of a string within another string containing comma-separated values.