简体   繁体   English

Magento产品系列按自定义多选属性筛选

[英]Magento Product Collection Filter by Custom Multiple Select Attribute

I have an almost complete solution for showing Magento products selectively by region. 我有一个几乎完整的解决方案,可以按区域选择性地显示Magento产品。

The regions are: 这些地区是:
UK 联合王国
Europe 欧洲
Americas 美洲
Middle East 中东
Rest of World 世界其他地区

All customers have been assigned a region, which I have made available via the variable $customerRegion. 已为所有客户分配了一个区域,我通过变量$ customerRegion提供了该区域。

All my Magento Products have a custom attribute 'product_region', which has region options available for multiple selection. 我的所有Magento产品都有一个自定义属性'product_region',它有多个区域选项可供多种选择。

list.phtml should only present products that match the customer's region. list.phtml应该只显示与客户所在地区相匹配的产品。 I have a partially working solution: 我有一个部分工作的解决方案:

$_productCollection = Mage::getResourceModel('catalog/product_collection')
    ->addCategoryFilter(Mage::getSingleton('catalog/layer')->getCurrentCategory())
    ->addAttributeToFilter('product_region',
        array('eq' => Mage::getResourceModel('catalog/product')
            ->getAttribute('product_region')
            ->getSource()
            ->getOptionId($customerRegion)
        )
    )
    ->addAttributeToSelect('*');

If the customer's region is UK, and the product's region is UK, the filter works as expected. 如果客户的区域是英国,并且产品的区域是英国,则过滤器按预期工作。
If the customer's region is UK, and the product's region is UK AND Europe, the filter does not work as expected (no results are given). 如果客户的区域是英国,并且产品的区域是英国和欧洲,则过滤器不能按预期工作(未给出结果)。

How can I adjust the filter to get the desired behaviour? 如何调整过滤器以获得所需的行为?

I have tried using 'in' and 'like' instead of 'eq' but have not been successful. 我尝试使用'in'和'like'而不是'eq',但没有成功。

Thank you. 谢谢。

PS An easier way to view the same problem is to look at the following code: PS查看同一问题的更简单方法是查看以下代码:

->addFieldToFilter(array(
    array('attribute'=>'product_region','eq'=>'49')
))

使用finset而不是eq (多个选择值以CSV格式存储)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM