在Magento获取产品系列,按类别& amp;自定义属性值

时间:2016-01-21 18:35:27

标签: magento

我正在创建magento API,所以现在我需要从类别ID和目录属性id中获取产品集合。

代表: 我有名称测试类别,ID为1。此外,我设置了属性color(来自catalog->attributes->manage attributes),我已为此颜色属性设置了值whiteblueblack

现在我添加了一些产品,它的类别是test(id = 1),颜色属性设置为white

我的问题是: 现在,我希望获得类别ID为1且颜色为white的产品系列。

我如何获得此系列。 提前致谢

2 个答案:

答案 0 :(得分:4)

<?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'),
                       ));

更多信息检查

How to get products from a particular category in magento ecommerce

Magento - Retrieve products with a specific attribute value

https://magento.stackexchange.com/questions/5838/get-product-collection-from-a-category-id

答案 1 :(得分:0)

<?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();
        }
    }
?>