0

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?

2 Answers 2

1

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

  $productIds = array(
        $product_id_1,
        $product_id_2
    );

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

Mage::getModel('tag/tag_relation')
    ->addRelations($tag, $productIds);
1
  • 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, 2015 at 11:46
0

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


 $tagModel->unsetData()->loadByName($tagName);

$storeId = Mage::app()->getStore()->getId();
if (!$tagModel->getId()) {
            $tagModel->setName($tagName)
                ->setFirstCustomerId($customerId)
                ->setFirstStoreId($storeId)
                ->setStatus($tagModel->getPendingStatus())
                ->save();
   }

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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