$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);
You can use magento object to filter.
Example:
$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);
I'm using the above code to display sister categories in my template - it's not ideal but it works.
It's sort of a hack because I did not yet have time to learn all the layout XML madness. Otherwise if you use the XMLs you need to keep in mind - it all depends on where you are at. Where means the template file and essentially also the layout (in terms of app/design/frontend/default/default/layout/*).
I know it's not much, but I hope it helps to get you started.
// 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 />";
}
Here is the code to get products from any particular category. You can use this in view file as well.
// 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 />";
}
<?php
$category_id = 10; // if you know static category then enter number
$catagory_model = Mage::getModel('catalog/category')->load($category_id); //where $category_id is the id of the category
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addCategoryFilter($catagory_model); //category filter
$collection->addAttributeToFilter('status',1); //only enabled product
$collection->addAttributeToSelect(array('name','url','small_image')); //add product attribute to be fetched
//$collection->getSelect()->order('rand()'); //uncomment to get products in random order
$collection->addStoreFilter();
if(!empty($collection))
{
foreach ($collection as $_product):
echo $_product->getName(); //get product name
endforeach;
}else
{
echo 'No products exists';
}
?>
<?php
$category_id = 10; // if you know static category then enter number
$catagory_model = Mage::getModel('catalog/category')->load($category_id); //where $category_id is the id of the category
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addCategoryFilter($catagory_model); //category filter
$collection->addAttributeToFilter('status',1); //only enabled product
$collection->addAttributeToSelect(array('name','url','small_image')); //add product attribute to be fetched
//$collection->getSelect()->order('rand()'); //uncomment to get products in random order
$collection->addStoreFilter();
if(!empty($collection))
{
foreach ($collection as $_product):
echo $_product->getName(); //get product name
endforeach;
}else
{
echo 'No products exists';
}
?>
You should always avoid putting code like this into a view, it's very bad practice. You can also run into issues as views can be cached, leading to unexpected behaviour.
you should override the block you are using, placing code there. you can then call any new methods inside your view files.
for example, you could copy Mage_Catalog_Block_Product_List
you could then add a new method, possibly using some of the code mentioned in the above posts. your new method would then be available inside your view file (list.phtml or any view using this block)
发布评论
评论(8)
您可以使用magento对象来过滤。
例子:
You can use magento object to filter.
Example:
这完全取决于您所在的视图。;-)
首先,我希望您留在模板集中(在我的示例中是默认的)。
以此作为示例:
我使用上面的代码在我的模板中显示姐妹类别 - 它并不理想,但它有效。
这有点像黑客,因为我还没有时间学习所有布局 XML 的疯狂内容。 否则,如果您使用 XML,则需要记住 - 这完全取决于您所处的位置。 Where 表示模板文件,本质上也是布局(就 app/design/frontend/default/default/layout/* 而言)。
我知道这并不多,但我希望它能帮助你开始。
It all depends on which view you're in. ;-)
First off, I hope you stayed within your template set (default in my example).
Use this as an example:
I'm using the above code to display sister categories in my template - it's not ideal but it works.
It's sort of a hack because I did not yet have time to learn all the layout XML madness. Otherwise if you use the XMLs you need to keep in mind - it all depends on where you are at. Where means the template file and essentially also the layout (in terms of app/design/frontend/default/default/layout/*).
I know it's not much, but I hope it helps to get you started.
这是从任何特定类别获取产品的代码。 您也可以在视图文件中使用它。
Here is the code to get products from any particular category. You can use this in view file as well.
我也非常需要同样的东西。 这是我的做法:
希望这会有所帮助。
I pretty much needed the same. Here is how I have done it:
Hope this helps.
您应该始终避免将这样的代码放入视图中,这是非常糟糕的做法。
您还可能会遇到问题,因为视图可以被缓存,从而导致意外的行为。
您应该覆盖您正在使用的块,并将代码放在那里。 然后您可以在视图文件中调用任何新方法。
例如,您可以将 Mage_Catalog_Block_Product_List
从: app/code/core/Catalog/Block/Product/List.php
复制到: app/code/local/Catalog/Block/Product/List.php
然后您可以添加一个新方法,可能使用上面帖子中提到的一些代码。
然后,您的新方法将在您的视图文件(list.phtml 或使用此块的任何视图)中可用
You should always avoid putting code like this into a view, it's very bad practice.
You can also run into issues as views can be cached, leading to unexpected behaviour.
you should override the block you are using, placing code there. you can then call any new methods inside your view files.
for example, you could copy Mage_Catalog_Block_Product_List
from: app/code/core/Catalog/Block/Product/List.php
to: app/code/local/Catalog/Block/Product/List.php
you could then add a new method, possibly using some of the code mentioned in the above posts.
your new method would then be available inside your view file (list.phtml or any view using this block)
这是将所有产品及其类别导出到 csv 的代码
Here is a code to export all product with it's category into csv