Php 从类别中加载所有产品,并按Magento中选定的多选属性进行筛选
我的Magento 1.9安装中有两个多选属性(带有各自的选项),如下所示:Php 从类别中加载所有产品,并按Magento中选定的多选属性进行筛选,php,magento,Php,Magento,我的Magento 1.9安装中有两个多选属性(带有各自的选项),如下所示: // Load category products having selected "Make" & "Model" $products = Mage::getModel('catalog/category') ->load($load_from_category) ->getProductCollection() ->addAttributeToSelect('*')
// 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'
);
制造
- 制造商A
- 制造商B
- 制造商C
- A型
- B型
- C型
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'
);
如果产品仅选择了一个“制造”和“型号”选项,则显示该选项。有什么想法吗?我是这样解决的:
// 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'
);
你想用代码做什么?