[英]Get product collection in Magento, filtered by Category & Custom Attribute Value
I am creating magento API so now I need to get product collection from category id and catalog attribute id. 我正在创建magento API,所以现在我需要从类别ID和目录属性ID获取产品集合。
for ex : I have category with name test and id is 1 . 例如:我有一个类别,名称为test,id为1。 Also I have set attribute
color
(from catalog->attributes->manage attributes
) and I have set values for this color attribute like white
, blue
, black
. 我还设置了属性
color
(来自catalog->attributes->manage attributes
),并且已经为此颜色属性设置了值,例如white
, blue
, black
。
now I add few product and it's category is test (id=1) and color attribute is set white
. 现在我添加了一些产品,其类别为test(id = 1),并且color属性设置为
white
。
My Question is : Now I want to get product collection for whom category id is 1
and color is white
. 我的问题是:现在,我要获取类别ID为
1
并且颜色为white
产品集合。
How can I get this collection. 我如何获得这个收藏。 Thanks in advance
提前致谢
<?php
// load category object by category ID
$category = Mage::getModel('catalog/category')->load(1);
// get product collection, filter it by category,
// add the color attribute to select to be able to filter using it later
$productCollection = Mage::getResourceModel('catalog/product_collection')
->addCategoryFilter($category)
->addAttributeToSelect('color')
->addFieldToFilter(array(
array('attribute'=>'color','eq'=>'white'),
));
More info check 更多信息检查
How to get products from a particular category in magento ecommerce 如何从Magento电子商务中的特定类别中获取产品
Magento - Retrieve products with a specific attribute value Magento-检索具有特定属性值的产品
https://magento.stackexchange.com/questions/5838/get-product-collection-from-a-category-id https://magento.stackexchange.com/questions/5838/get-product-collection-from-a-category-id
<?php
$attributeCode = 'Your_Attribute_Code';
$attributeOption = 'Attribute_Option';
$attributeDetails = Mage::getSingleton('eav/config')->getAttribute('catalog_product', $attributeCode);
$options = $attributeDetails->getSource()->getAllOptions(false);
$selectedOptionId = false;
foreach ($options as $option){
if ($option['label'] == $attributeOption) {
$selectedOptionId = $option['value'];
}
}
if ($selectedOptionId) {
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter($attributeCode, array('eq' => $selectedOptionId));
foreach($products as $product){
echo $product->getId();
echo $product->getName();
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.