如何从 Magento 获取类别列表?

发布于 2024-07-14 06:05:32 字数 529 浏览 9 评论 0原文

我想在 Magento 中创建一个页面,显示类别的可视化表示。示例

CATEGORY
 product 1
 product 2

ANOTHER CATEGORY
 product 3

我的问题是,他们的数据库的组织方式与我过去看到的非常不同。 他们有专门用于 varchar、int 等数据类型的表。我认为这是为了性能或类似目的。

我还没有找到使用 MySQL 查询数据库并获取类别列表的方法。 然后,我想将这些类别与产品相匹配,以获得每个类别的产品列表。 不幸的是,Magento 似乎让这变得非常困难。

另外,我还没有找到可以在页面块中工作的方法。我创建了showcase.phtml并将其放入XML布局中,它显示并运行其PHP代码。 我希望有一些简单的东西,比如循环 $this->getAllCategories() ,然后在内部使用类似 $category->getChildProducts() 的嵌套循环。

谁能帮我?

I want to create a page in Magento that shows a visual representation of the categories.. example

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.

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.

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().

Can anyone help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

尐偏执 2024-07-21 06:05:32

从 SEO 相关类 (Mage_Catalog_Block_Seo_Sitemap_Category) 中找到的代码

$helper     = Mage::helper('catalog/category');
$collection = $helper->getStoreCategories('name', true, false);
$array      = $helper->getStoreCategories('name', false, false);

尝试忘记它是为您的商店提供支持的数据库,而专注于使用 Magento 系统提供的对象。

例如,我不知道如何获取类别列表。 然而,我用 grep 搜索了 Mage 代码库

grep -i -r -E 'class.+?category'

,返回了大约 30 个类的列表。 滚动浏览这些内容,可以相对容易地猜测哪些对象可能具有方法或需要进行方法调用来获取类别。

From code found in an SEO related class (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.

For example, I had no no idea how to get a list of categories. However, I grepped through the Mage codebase with

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.

戒ㄋ 2024-07-21 06:05:32

类似这样的< /a> 可能对您有帮助,我对其进行了稍微调整以更具体地回答您的问题。

 $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/>";

}

Hey something like this might 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/>";

}
荒路情人 2024-07-21 06:05:32

我改编自 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;

这将为您提供目录类别 ID。

I adapted this from Paul Whipp's website:

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.

靖瑶 2024-07-21 06:05:32

这是一个简单的例子

$categories = Mage::getModel('catalog/category')->getCollection()  
    ->addAttributeToSelect('name')
    ->addAttributeToSelect('url_key')
    ->addAttributeToSelect('my_attribute')
    ->setLoadProductCount(true)
    ->addAttributeToFilter('is_active',array('eq'=>true))
    ->load();

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();
薆情海 2024-07-21 06:05:32

多谢。 确实有帮助。 要获取游戏,请先循环然后 getName()

foreach ($collection as $cat):

    echo $cat->getName();

endforeach;

Thanks a lot. Really helps. To get the game, make a loop and then getName()

foreach ($collection as $cat):

    echo $cat->getName();

endforeach;
往事风中埋 2024-07-21 06:05:32

我在 /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; ?>

I used this in /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; ?>
不打扰别人 2024-07-21 06:05:32

类别列表块:

<?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>

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>
情话墙 2024-07-21 06:05:32

我制作了这个小视频,介绍如何使用 Magento 创建自定义类别列表块。
我确信有更好的方法来实现这一目标,甚至是我可以做得更好的事情,但这只是我的方法。 我创建这个只是希望它能帮助向一些人解释一些事情。

Magento 自定义类别列表教程

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 Custom Category Listing Tutorial

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文