Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7989439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:45:10+00:00 2026-06-04T12:45:10+00:00

As the title says: How do I programatically add one or more tags to

  • 0

As the title says: How do I programatically add one or more tags to an existing product? I couldn’t find anything useful about this topic on the internet, so every help, link or knowledge is welcome.

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:45:12+00:00Added an answer on June 4, 2026 at 12:45 pm

    Ok I found it by myself. I’ve copied and customized the saveAction() of the app/code/core/Mage/Tag/Controllers/IndexController.php and also some additional functions to get this thing to work.

    require_once $_SERVER['DOCUMENT_ROOT'] . "/app/Mage.php";
        umask(0);
        Mage::app();
        Mage::app()->getTranslator()->init('frontend');
        Mage::getSingleton('core/session', array('name' => 'frontend'));
    
    
    $customerSession = Mage::getSingleton('customer/session');
    
    $tagName    = 'Urban Extraloud';//(string) $this->getRequest()->getQuery('productTagName');
    $productId  = 257;//(int)$this->getRequest()->getParam('product');
    
    if(strlen($tagName) && $productId) {
        $session = Mage::getSingleton('catalog/session');
        $product = Mage::getModel('catalog/product')
            ->load($productId);
        if(!$product->getId()){
            //$session->addError($this->__('Unable to save tag(s).'));
        } else {
            try {
                $customerId = 58; //$customerSession->getCustomerId();
                $storeId = Mage::app()->getStore()->getId();
    
                $tagNamesArr = _cleanTags(_extractTags($tagName));
    
                $counter = new Varien_Object(array(
                    "new" => 0,
                    "exist" => array(),
                    "success" => array(),
                    "recurrence" => array())
                );
    
                $tagModel = Mage::getModel('tag/tag');
                $tagRelationModel = Mage::getModel('tag/tag_relation');
    
                foreach ($tagNamesArr as $tagName) {
                    $tagModel->unsetData()
                        ->loadByName($tagName)
                        ->setStoreId($storeId)
                        ->setName($tagName);
    
                    $tagRelationModel->unsetData()
                        ->setStoreId($storeId)
                        ->setProductId($productId)
                        ->setCustomerId($customerId)
                        ->setActive(Mage_Tag_Model_Tag_Relation::STATUS_ACTIVE)
                        ->setCreatedAt( $tagRelationModel->getResource()->formatDate(time()) );
    
                    if (!$tagModel->getId()) {
                        $tagModel->setFirstCustomerId($customerId)
                            ->setFirstStoreId($storeId)
                            ->setStatus($tagModel->getPendingStatus())
                            ->save();
    
                        $tagRelationModel->setTagId($tagModel->getId())->save();
                        $counter->setNew($counter->getNew() + 1);
                    } else {
                        $tagStatus = $tagModel->getStatus();
                        $tagRelationModel->setTagId($tagModel->getId());
    
                        switch($tagStatus) {
                            case $tagModel->getApprovedStatus():
                                if(_checkLinkBetweenTagProduct($tagRelationModel)) {
                                    $relation = _getLinkBetweenTagCustomerProduct($tagRelationModel, $tagModel);
                                    if ($relation->getId()) {
                                        if (!$relation->getActive()) {
                                            $tagRelationModel
                                                ->setId($relation->getId())
                                                ->save();
                                        }
                                    } else {
                                        $tagRelationModel->save();
                                    }
                                    $counter->setExist(array_merge($counter->getExist(), array($tagName)));
                                } else {
                                    $tagRelationModel->save();
                                    $counter->setSuccess(array_merge($counter->getSuccess(), array($tagName)));
                                }
                                break;
                            case $tagModel->getPendingStatus():
                                $relation = _getLinkBetweenTagCustomerProduct($tagRelationModel, $tagModel);
                                if ($relation->getId()) {
                                    if (!$relation->getActive()) {
                                        $tagRelationModel
                                            ->setId($relation->getId())
                                            ->save();
                                    }
                                } else {
                                    $tagRelationModel->save();
                                }
                                $counter->setNew($counter->getNew() + 1);
                                break;
                            case $tagModel->getDisabledStatus():
                                if(_checkLinkBetweenTagCustomerProduct($tagRelationModel, $tagModel)) {
                                    $counter->setRecurrence(array_merge($counter->getRecurrence(), array($tagName)));
                                } else {
                                    $tagModel->setStatus($tagModel->getPendingStatus())->save();
                                    $tagRelationModel->save();
                                    $counter->setNew($counter->getNew() + 1);
                                }
                                break;
                        }
                    }
                }
    
            } catch (Exception $e) {
    
                print 'Unable to save tag(s)';
            }
        }
    }
    
    
    
    
    
    
    function _extractTags($tagNamesInString)
    {
        return explode("\n", preg_replace("/(\'(.*?)\')|(\s+)/i", "$1\n", $tagNamesInString));
    }
    
    /**
     * Clears the tag from the separating characters.
     *
     * @param array $tagNamesArr
     * @return array
     */
    function _cleanTags(array $tagNamesArr)
    {
        foreach( $tagNamesArr as $key => $tagName ) {
            $tagNamesArr[$key] = trim($tagNamesArr[$key], '\'');
            $tagNamesArr[$key] = trim($tagNamesArr[$key]);
            if( $tagNamesArr[$key] == '' ) {
                unset($tagNamesArr[$key]);
            }
        }
        return $tagNamesArr;
    }
    
    
     /**
     * Checks whether the already marked this product in this store by this tag.
     *
     * @param Mage_Tag_Model_Tag_Relation $tagRelationModel
     * @return boolean
     */
    function _checkLinkBetweenTagProduct($tagRelationModel)
    {
        $customerId = $tagRelationModel->getCustomerId();
        $tagRelationModel->setCustomerId(null);
        $res = in_array($tagRelationModel->getProductId(), $tagRelationModel->getProductIds());
        $tagRelationModel->setCustomerId($customerId);
        return $res;
    }
    
    
    /**
     * Checks whether the already marked this product in this store by this tag and by this customer.
     *
     * @param Mage_Tag_Model_Tag_Relation $tagRelationModel
     * @param Mage_Tag_Model_Tag $tagModel
     * @return boolean
     */
    function _checkLinkBetweenTagCustomerProduct($tagRelationModel, $tagModel)
    {
        return (count(_getLinkBetweenTagCustomerProduct($tagRelationModel, $tagModel)
            ->getProductIds()) > 0);
    }
    
    /**
     * Get relation model for marked product in this store by this tag and by this customer.
     *
     * @param Mage_Tag_Model_Tag_Relation $tagRelationModel
     * @param Mage_Tag_Model_Tag $tagModel
     * @return Mage_Tag_Model_Tag_Relation
     */
    function _getLinkBetweenTagCustomerProduct($tagRelationModel, $tagModel)
    {
        return Mage::getModel('tag/tag_relation')->loadByTagCustomer(
            $tagRelationModel->getProductId(),
            $tagModel->getId(),
            $tagRelationModel->getCustomerId(),
            $tagRelationModel->getStoreId()
        );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The title says everything. I am talking about C/C++ specifically, because both consider this
As the title says, I'm trying to do something like this: public abstract class
As title says, is there any way to pull something like this up?: std::set<boost::shared_ptr<MyClass>>
Title says it mostly. I want to add a simple extension method to the
As title says, this issue happens in MS Access 2003 SP1. Does anyone know
Just what the title says, I need to change the password for an existing
Title says all. I'll have navigation that looks like this and it's dynamically created
The Title says everything.. Searched Google and Stackoverflow and didn't find something similiar..
The title says everything, but just to be clear I'll add some extra words.
As the title says, i need a way to programmatically edit an existing view

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.