如何从magento中的类别id获取单个产品?
尝试以下内容如何从magento中的类别id获取单个产品?,magento,Magento,尝试以下内容 <?php foreach($this->getStoreCategories() as $_category) { $cur_category=Mage::getModel('catalog/category')->load($_category->getId()); $collectionnn = Mage::getModel('catalog/product') ->ge
<?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;
}
}
?>
试试这个
$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
$category=Mage::getModel('catalog/category')->加载(64);
$collection=$category->getProductCollection()->addAttributeToFilter('status',1);
$collection->getSelect()->limit(1);
foreach($收集为$产品)
{
回声“
”;
}
您使用了break;您不认为这会对性能产生影响吗?您也可以使用“限制”。在这段代码中使用->addAttributeToSelect('*')如何添加限制也是不可取的。请告诉我me@DushyantJoshi这怎么可能是正确答案?@slimshaddyyy。这就是我为什么这么做的原因。而且他也没有具体说明你在评论中提到的他想要哪种产品,从分类id中获得单个产品意味着什么?他只想展示一种id为64的产品,哪一种是该产品?@slimshaddyyy,这是一个谜:)这样的问题在这里无法回答。你必须准确地知道你想要什么。
$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
$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>";
}