I would I am creating an import script. I would like to know how to programatically add a product tag via php script?

Any experience doing this?


To programatically add tag to a product, try the below solution :

  $productIds = array(

$tag = Mage::getModel('tag/tag')
    ->load('my_tag_name', 'name');

    ->addRelations($tag, $productIds);
  • 1
    this gives me an error of a:5:{i:0;s:214:"SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'store_id' cannot be null, query was: INSERT INTO vwkvtag_relation (tag_id,store_id,product_id,customer_id,created_at) VALUES (?, ?, ?, ?, ?)";i:1;s:2375:"#0 E:\xampp\htdocs\magento\lib\Varien\Db\Statement\Pdo\Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array) – tom_cruz Jul 28 '15 at 11:46

You can add a tag by below code:

$tagName=preg_replace("/(\'(.*?)\')|(\s+)/i", "$1\n", $tagNamesInString)
 $tagModel = Mage::getModel('tag/tag');

    // added tag relation statuses
    $counter = array(
        Mage_Tag_Model_Tag::ADD_STATUS_NEW => array(),
        Mage_Tag_Model_Tag::ADD_STATUS_EXIST => array(),
        Mage_Tag_Model_Tag::ADD_STATUS_SUCCESS => array(),
        Mage_Tag_Model_Tag::ADD_STATUS_REJECTED => array()


$storeId = Mage::app()->getStore()->getId();
if (!$tagModel->getId()) {

$relationStatus = $tagModel->saveRelation($productId, $customerId, $storeId);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.