1

如果我要创建一个将使用给定collection检索所有products属于的,如下所示:categoryID

    $storeId    = Mage::app()->getStore()->getId();  
    $product    = Mage::getModel('catalog/product');  
    $category   = Mage::getModel('catalog/category')->load(39);
    $catName    = $category->getName();

    $visibility = array(  
        Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,  
        Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG  
    );  

    $products = $product->setStoreId($storeId)  
        ->getCollection()  
        ->addAttributeToFilter('visibility', $visibility)  
        ->addCategoryFilter($category)  
        ->addAttributeToSelect(array('name'), 'inner')
        ->setOrder('name', 'asc')
    ;  

    $collection = $products;  

然后我将如何遍历集合并访问每个项目的数据,在这种情况下,项目是一个产品。

我本来希望像下面这样的东西给我每个项目可用的数据,但似乎不起作用:

    foreach ($collection as $key => $value) {
        var_dump($value->getData());
    }
4

1 回答 1

5

你很近。尝试做

foreach ($collection as $obj) {
        echo $obj->getName();
}
于 2013-01-24T14:54:35.357 回答