Pez
Pez

Reputation: 1

Magento: How to get products by tag name?

I am using Magento 1.7.0.0 CE and have a few questions.

How can i get products by tag name?

Is there any method in Magento that takes tag name and returns products assigned to that tag (tags are made by admin and products are assigned to it)

I ideally am looking for Mage Core API to do this. Does Mage Core API include this functionality?

Many thanks, Pez

Upvotes: 0

Views: 1721

Answers (2)

MagePal Extensions
MagePal Extensions

Reputation: 17656

$tagId = 3;  //Mage::getModel('tag/tag')->loadByName($tagName)->getId();

$collection = Mage::getResourceModel('tag/product_collection')
                    ->addAttributeToSelect('sku')
                    ->addAttributeToSelect('name')
                    ->addTagFilter($tagId);

print_r($collection->getData());

Upvotes: 1

PauGNU
PauGNU

Reputation: 793

You can just use the «addTag» filter to the collection:

$collection->addTagFilter($tagId);

That should do the trick.

Upvotes: 1

Related Questions