-->

Adding tags to products in Magento

2020-08-05 10:48发布

问题:

what is the code needed to insert tags to a product in Magento?

thanks

回答1:

    $tagName = 'php';
    $customerID = NULL;
    $storeId = Mage::app()->getStore()->getId();
    $productID = 1;

    $tagModel = Mage::getModel('tag/tag');
    $tagModel->loadByName($tagName);
    //$tagModel->unsetData()->loadByName($tagName);  //if using a loop 

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

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


回答2:

By default Magento Developer have the option to allow customers to tag your products. When a customer tags a certain product, the tag appears as pending and must be approved before it can show up on the product page.

Let's add a tag to our product, approve it and see how it then shows up on the product page. To add a tag to a product, simply write the tag word in the Add Your Tags: field available on the product page and click Add Tags. We'll add "Great" as a tag. A confirmation message will show up saying that the tag has been accepted for moderation:

Magento New Tag

Now go to your Magento admin area > Catalog > Tags > Pending Tags to view all your pending tags. In our case, there will be only one pending tag for the word "Great":

Magento Pending Tags

Click it and you will be taken to a page where you can change the status of the tag. It will appear as "Pending", so le't change it to "Approved" and click Save Tag.

Now that the tag has been approved, all other customers will see it on the product page.

You can also manage tags on a per-product basis. Go to your Magento admin area > Catalog > Manage Products and click the product the tags of which you want to check. Then from the left menu click Product Tags and you will see all the tags for that product.



回答3:

That is default magento functionality. Catalog->Tags in backend.



回答4:

Have a look at the place how it's already done in Magento. See at the saveAction method in the Mage_Tag_IndexController class (app/code/core/Mage/Tag/controllers/IndexController.php)



标签: php magento tags