Ues
Ues

Reputation: 197

Get product's tags on category page in Magento?

How can I get and display product's tags on category view (in loop where products are listing)? I used code (from another question) and put in \app\design\frontend\...\template\catalog\product\list.phtml:

<?php $_tags = new Mage_Tag_Block_Product_List(); ?>

and in foreach (where products listing) display tags:

<?php if($_tags) : ?>
    <?php foreach($_tags->getTags() as $tag):?>
        <span class="tag"><?=$tag->getName()?></span>
    <?php endforeach; ?>
<?php endif; ?>

And it isn't working, because instantiating $_tags is giving me error:

Call to a member function getItems() on a non-object in \app\code\core\Mage\Tag\Block\Product\List.php on line 45.

Upvotes: 0

Views: 3985

Answers (2)

LemonAV
LemonAV

Reputation: 23

I think this is a good point to start:

To see what products have a certain tag you can use

$tagId = 9;  
$collection = Mage::getResourceModel('tag/product_collection')->addTagFilter($tagId);
?>
    <ul class=\"tags-list tag_overview\">
        <?php foreach ($collection as $_tag): ?>
            <li><a href=\"<?php echo $_tag->getTaggedProductsUrl() ?>\" style=\"font-size:<?php echo $_tag->getRatio()*70+75 ?>%;\"><?php echo $this->htmlEscape($_tag->getName()) ?></a></li>
        <?php endforeach; ?>
    </ul>

you can use addTagFilter($tagId) or not, depends your goals

Upvotes: 2

Ues
Ues

Reputation: 197

I used this code and product's tags are displaying in loop for each product in collection:

foreach ($_productCollection as $_product):
    $model=Mage::getModel('tag/tag');
    $tags= $model->getResourceCollection()
        ->addPopularity()
        ->addStatusFilter($model->getApprovedStatus())
        ->addProductFilter($_product->getId())
        ->setFlag('relation', true)
        ->addStoreFilter(Mage::app()->getStore()->getId())
        ->setActiveFilter()
        ->load();

    if(isset($tags) && !empty($tags)):
        foreach($tags as $tag):
            echo '<span class="tag">'.$tag->getName().'</span>';
        endforeach;
    endif;
endforeach;

Upvotes: 2

Related Questions