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?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityTo 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);
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)
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);