小智 21
您可以使用magento对象进行过滤.
例:
$categoryId = 123; // a category id that you can get from admin
$category = Mage::getModel('catalog/category')->load($categoryId);
$products = Mage::getModel('catalog/product')
->getCollection()
->addCategoryFilter($category)
->load();
print_r($products);
Run Code Online (Sandbox Code Playgroud)
这一切都取决于你所处的观点.;-)
首先,我希望你留在你的模板集中(在我的例子中默认).
以此为例:
<?php
$_cat = $this->getCurrentCategory();
$_parent = $_cat->getParentCategory();
$_categories = $_parent->getChildren();
/* @var $category Mage_Catalog_Model_Category */
$collection = Mage::getModel('catalog/category')->getCollection();
/* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
$collection->addAttributeToSelect('url_key')
->addAttributeToSelect('name')
->addAttributeToSelect('is_anchor')
->addAttributeToFilter('is_active', 1)
->addIdFilter($_categories)
->setOrder('position', 'ASC')
->joinUrlRewrite()
->load();
$productCollection = Mage::getResourceModel('catalog/product_collection');
$layer = Mage::getSingleton('catalog/layer');
$layer->prepareProductCollection($productCollection);
$productCollection->addCountToCategories($collection);
// $productCollection should be ready here ;-)
?>
Run Code Online (Sandbox Code Playgroud)
我正在使用上面的代码在我的模板中显示姐妹类别 - 它不是理想的但它有效.
这有点像黑客,因为我还没有时间学习所有布局XML疯狂.否则,如果您使用XML,则需要记住 - 这一切都取决于您所处的位置.其中表示模板文件,基本上也表示布局(就app/design/frontend/default/default/layout/*而言).
我知道这并不多,但我希望能帮助你入门.
以下是从任何特定类别获取产品的代码.您也可以在视图文件中使用它.
// if you want to display products from current category
$category = Mage::registry('current_category');
// if you want to display products from any specific category
$categoryId = 10;
$category = Mage::getModel('catalog/category')->load($categoryId);
$productCollection = Mage::getResourceModel('catalog/product_collection')
->addCategoryFilter($category);
// printing products name
foreach ($productCollection as $product) {
echo $product->getName();
echo "<br />";
}
Run Code Online (Sandbox Code Playgroud)
<?php
$c_id = 2;
$category = new Mage_Catalog_Model_Category();
$category->load($c_id);
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
foreach ($collection as $_product) { ?>
<a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a>
<?php } ?>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
52074 次 |
最近记录: |