如何从magento中的类别ID获取单个产品?

时间:2014-05-13 09:14:28

标签: magento

<?php

    $categoryid = 64;

    $category = new Mage_Catalog_Model_Category();
    $category->load($categoryid);
    $collection = $category->getProductCollection();
    $collection->addAttributeToSelect('*');

    foreach ($collection as $_product) { ?>

    <a href="<?php echo $_product->getProductUrl() ?>"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" width="200" height="200" alt="" /></a> <a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a>

<?php } ?>

3 个答案:

答案 0 :(得分:2)

试试这个

$category = Mage::getModel('catalog/category')->load(64);
$collection = $category->getProductCollection()->addAttributeToFilter('status', 1);
$collection->getSelect()->limit(1);
foreach($collection  as $product)
{
    echo "<a href=".$product->getProductUrl().">".$product->getName()."</a><br>";
}

答案 1 :(得分:0)

尝试以下

$category=Mage::getModel('catalog/category')->load($category_id);
$products = $category->getProductCollection()
//->addAttributeToSelect('*') // add all attributes - optional
->addAttributeToSelect(array('name','entity_id','price','small_image','frontend_name'))
->setPageSize(1)
->setOrder('created_at', 'DESC'); //sets the order by created_at

答案 2 :(得分:-3)

<?php
foreach($this->getStoreCategories() as $_category)
{
    $cur_category=Mage::getModel('catalog/category')->load($_category->getId());
    $collectionnn = Mage::getModel('catalog/product')
                              ->getCollection()
                              ->joinField('category_id','catalog/category_product', 'category_id', 'product_id = entity_id',null, 'left')
                              ->addAttributeToSelect('*')
                              ->addAttributeToFilter('category_id',array('in' => $cur_category->getId()));        
                    foreach($collectionnn as $product)
                    {
                        echo "<a href=".$product->getProductUrl().">".$product->getName()."</a><br>";
                        break;
                    }
}            
?>