从类别中加载所有产品并按 Magento 中选定的多选属性进行过滤

Posted

技术标签:

【中文标题】从类别中加载所有产品并按 Magento 中选定的多选属性进行过滤【英文标题】:Load all products from category and filter by selected multi-select attribute in Magento 【发布时间】:2017-06-23 06:11:35 【问题描述】:

我的 Magento 1.9 安装中有两个多选属性(带有各自的选项),如下所示:

制作

制造商A 制造商 B 制造商C

型号

A 型 B 型 C 型

我有一些产品会有多个“品牌”和“型号”。

如何从 Magento 中加载特定类别的所有产品并按具有特定选定多选属性选项的产品进行过滤?

我正在尝试加载存在此类场景的产品:

make = [Manufacturer A] and [Manufacturer C]
model = [Type A] and [Type B]

这是我迄今为止尝试过的:

// Mini-config
$make_attribute_code = 'make';
$model_attribute_code = 'model';
$filter_by_make = 'Manufacturer A';
$filter_by_model = 'Type A';
$load_from_category = 10117;

// Parse attribute option id of "Make"
$make_option_id = null;
$makeAttributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product', $make_attribute_code);
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($makeAttributeId);
$attributeOptions = $attribute ->getSource()->getAllOptions();
foreach ($attributeOptions as $attributeOption) 
    if (trim($attributeOption['label']) == $filter_by_make) 
        $make_option_id = (int)trim($attributeOption['value']);
        break;
    


// Parse attribute option id of "Model"
$model_option_id = null;
$modelAttributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product', $model_attribute_code);
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($modelAttributeId);
$attributeOptions = $attribute ->getSource()->getAllOptions();
foreach ($attributeOptions as $attributeOption) 
    if (trim($attributeOption['label']) == $filter_by_model) 
        $model_option_id = (int)trim($attributeOption['value']);
        break;
    


// Load category products having selected "Make" & "Model"
$products = Mage::getModel('catalog/category')
    ->load($load_from_category)
    ->getProductCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter(array(array('attribute' => $make_attribute_code, 'eq' => $make_option_id)))
    ->addAttributeToFilter(array(array('attribute' => $model_attribute_code, 'eq' => $model_option_id)))
    ->joinField(
        'qty',
        'cataloginventory/stock_item',
        'qty',
        'product_id=entity_id',
        'table.stock_id=1',
        'left'
    );

如果产品只选择了一个“品牌”和“型号”选项,它似乎正在显示。有什么想法吗?

【问题讨论】:

你想用什么代码? 【参考方案1】:

我已经这样解决了:

// Load category products having selected "Make" & "Model"
$products = Mage::getModel('catalog/category')
    ->load($load_from_category)
    ->getProductCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter(array(array('attribute' => $make_attribute_code, 'finset' => array($make_option_id))))
    ->addAttributeToFilter(array(array('attribute' => $model_attribute_code, 'finset' => array($model_option_id))))
    ->joinField(
        'qty',
        'cataloginventory/stock_item',
        'qty',
        'product_id=entity_id',
        'table.stock_id=1',
        'left'
    );

【讨论】:

以上是关于从类别中加载所有产品并按 Magento 中选定的多选属性进行过滤的主要内容,如果未能解决你的问题,请参考以下文章

如何在不加载页面的情况下在一个页面中加载超过 1500 个产品?

无法在 magento 2.4.3 中查看类别下的产品

所有产品(类别和子类别产品)都计入 magento 中的类别列表页面

Magento 类别页面未包含属性过滤器中的所有产品

如何从 Magento 搜索中过滤掉某些类别的产品?

magento 从类别中获取产品,按 rand() 排序