php 如何从 Magento 获取类别列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/604743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a category listing from Magento?
提问by alex
I want to create a page in Magento that shows a visual representation of the categories.. example
我想在 Magento 中创建一个页面,显示类别的可视化表示.. 示例
CATEGORY
product 1
product 2
ANOTHER CATEGORY
product 3
My problem is, their database is organised very differently to what I've seen in the past. They have tables dedicated to data types like varchar, int, etc. I assume this is for performance or similar.
我的问题是,他们的数据库的组织方式与我过去看到的非常不同。他们有专用于 varchar、int 等数据类型的表。我认为这是为了性能或类似的。
I haven't found a way to use MySQL to query the database and get a list of categories. I'd then like to match these categories to products, to get a listing of products for each category. Unfortunately Magento seems to make this very difficult.
我还没有找到使用 MySQL 查询数据库并获取类别列表的方法。然后我想将这些类别与产品相匹配,以获得每个类别的产品列表。不幸的是,Magento 似乎让这变得非常困难。
Also I have not found a method that will work from within a page block.. I have created showcase.phtml and put it in the XML layout and it displays and runs its PHP code. I was hoping for something easy like looping through $this->getAllCategories()and then a nested loop inside with something like $category->getChildProducts().
此外,我还没有找到可以在页面块内工作的方法。我创建了showcase.phtml 并将其放在 XML 布局中,它显示并运行其 PHP 代码。我希望有一些简单的东西,比如循环$this->getAllCategories(),然后在里面有一个嵌套循环,比如$category->getChildProducts().
Can anyone help me?
谁能帮我?
回答by Alan Storm
From code found in an SEO related class (Mage_Catalog_Block_Seo_Sitemap_Category)
来自 SEO 相关类中的代码 (Mage_Catalog_Block_Seo_Sitemap_Category)
$helper = Mage::helper('catalog/category');
$collection = $helper->getStoreCategories('name', true, false);
$array = $helper->getStoreCategories('name', false, false);
Try to forget that it's a database that's powering your store, and instead concentrate on using the objects that the Magento system provides.
试着忘记它是一个为您的商店提供动力的数据库,而是专注于使用 Magento 系统提供的对象。
For example, I had no no idea how to get a list of categories. However, I grepped through the Mage codebase with
例如,我不知道如何获取类别列表。但是,我通过 Mage 代码库搜索了
grep -i -r -E 'class.+?category'
Which returned a list of around 30 classes. Scrolling through those, it was relatively easy to guess which objects might have methods or need to make method calls that would grab the categories.
它返回了大约 30 个类的列表。滚动浏览这些内容,可以相对容易地猜测哪些对象可能具有方法或需要进行方法调用以获取类别。
回答by alex
Hey something like thismight help you, I've adapted it slightly to answer your question more specifically.
嘿,这样的事情可能对你有帮助,我已经稍微调整了一下,以更具体地回答你的问题。
$h3h3=Mage::getModel('catalog/category')->load(5); // YOU NEED TO CHANGE 5 TO THE ID OF YOUR CATEGORY
$h3h3=$h3h3->getProductCollection();
foreach($h3h3->getAllIds() as $lol)
{
$_product=Mage::getModel('catalog/product')->load($lol);
print $_product->getName()."<br/>";
}
回答by sws
I adapted this from Paul Whipp's website:
我改编自Paul Whipp 的网站:
SELECT e.entity_id AS 'entity_id', vn.value AS 'name'
FROM catalog_category_entity e
LEFT JOIN catalog_category_entity_varchar vn
ON e.entity_id = vn.entity_id AND vn.attribute_id = 33
ORDER BY entity_id;
This will provide you with the catalog category IDs.
这将为您提供目录类别 ID。
回答by bluescrubbie
Here's a quick example
这是一个快速示例
$categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addAttributeToSelect('url_key')
->addAttributeToSelect('my_attribute')
->setLoadProductCount(true)
->addAttributeToFilter('is_active',array('eq'=>true))
->load();
回答by Nikola
I used this in /app/design/frontend/default/default/template/catalog/product/feature.xml
我在 /app/design/frontend/default/default/template/catalog/product/feature.xml 中使用了这个
<?php
/**
* Home page Featured Product list template
*
* @see Mage_Catalog_Block_Product_List
*/
?>
<?php
if (!is_null($this->_productCollection)) {
$_origCollection = $this->_productCollection;
$this->setCollection(null);
}
$this->setCategoryId(16);
$_productCollection=$this->getLoadedProductCollection() ?>
<?php if($_productCollection->count()): ?>
<div class="featured-products">
<h2><?php echo $this->__('Featured Products') ?></h2>
<?php foreach ($_productCollection as $_product): ?>
<div>
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(150, 50); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
<h3 class="product-name"><?php echo $this->htmlEscape($_product->getName())?></h3>
<?php echo nl2br($this->htmlEscape($_product->getShortDescription())) ?>
</a>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
回答by Devin R. Olsen
I made this little video on how I create custom category listing blocks with Magento. I am sure there are better ways of achieving this or even something I could have done better, but it's just my method. I only created this it in hopes that it helps explain somethings to some people out there.
我制作了这个关于如何使用 Magento 创建自定义类别列表块的小视频。我相信有更好的方法来实现这一点,甚至还有一些我可以做得更好的方法,但这只是我的方法。我创建它只是希望它有助于向那里的某些人解释一些事情。
回答by Pooja maheshwari
category Listing block:
类别列表块:
<?php
$categories = Mage::getModel('catalog/category')->load(2)->getChildren();
$catIds = explode(',',$cats);
?>
<ul>
<?php foreach($catIds as $catId): ?>
<li>
<?php
$category = Mage::getModel('catalog/category')->load($catId);
echo $category->getName();
$subCats = Mage::getModel('catalog/category')->load($category->getId())->getChildren();
$subCatIds = explode(',',$subCats);
?>
<?php if(count($subCatIds) > 1):?>
<ul>
<?php foreach($subCatIds as $subCat) :?>
<li>
<?php
$subCategory = Mage::getModel('catalog/category')->load($subCat);
echo $subCategory->getName();
?>
</li>
<?php endforeach;?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
回答by Pooja maheshwari
Thanks a lot. Really helps. To get the game, make a loop and then getName()
非常感谢。真的有帮助。要获得游戏,请进行循环,然后使用 getName()
foreach ($collection as $cat):
echo $cat->getName();
endforeach;

