Is there a way of importing product tags via a spreadsheet?
if not through a module, can this be开发者_开发知识库 done through a script?
Tags to be imported format should be like this
And place the about spreadsheet in root/tagimport/tagsheet.csv
and paste the following code in root/tagimport/importtag.php
<?php
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/../app/Mage.php';
Mage::app();
$fileName = 'importtag.csv';
getAttributeCsv($fileName);
function getAttributeCsv($fileName){
$file = fopen($fileName,"r");
$RemoveLine = 0;
while(!feof($file)){
$data = fgetcsv($file);
if($RemoveLine != 0){
$labelText[] = $data[1];
}$RemoveLine++;
}createAttribute($labelText);
fclose($file);
}
function createAttribute($labelText)
{
foreach($labelText as $tag){
if($tag){
$quote = Mage::getModel('tag/tag');
$quote->loadByName($tag);
if(!$quote->getId()){
$quote->setName($tag);
$quote->setStatus(1);
$quote->setFirstStoreId(0);
$quote->setPopularity(0);
$quote->save();
##### Save product tags in "tag_properties"
$connectionresource = Mage::getSingleton('core/resource');
$connectionWrite = $connectionresource->getConnection('core_write');
$table = 'tag_properties';
$query = "insert into ".$table." "."(tag_id,store_id,base_popularity) values "."(:tag_id, :store_id, :base_popularity)";
$binds = array(
'tag_id' => $quote->getTagId(),
'store_id' => '1',
'base_popularity' => '0',
);
$connectionWrite->query($query, $binds);
echo 'Tags Imported Successfully';
}
}
}
}
run the above php file using
www.yourdomain.com/tagimport/importtag.php
If you have tags csv like below, (the first col is product SKU, the second column is the tag name)
8697550050245,White Egg
6291104283077,Organic Egg
6291100152520,Eco Veg Fed Egg
5701607588896,Organic Egg
Below script will import the tags for you
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
ini_set('memory_limit', '-1');
ini_set('max_execution_time', 0);
ini_set('request_terminate_timeout', 0);
set_time_limit(0);
require_once 'app/Mage.php';
Mage::app("admin");
$storeId = 1;
try {
$h = fopen(dirname(__FILE__) . '/var/tags.csv', 'r');
if ($h !== FALSE) {
$updated = '';
$notFound = '';
while (($data = fgetcsv($h, 1000, ",")) !== FALSE) {
$sku = preg_replace("/[^A-Za-z0-9 ]/", '', $data[0]);
$tagName = preg_replace("/[^A-Za-z0-9\&\/\(\)\- ]/", '', $data[1]);
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
if(!empty($product) && !empty($tagName)) {
$tagName = strtolower($tagName);
$tag = Mage::getModel('tag/tag');
$tag->loadByName($tagName);
if (!$tag->getId()) {
$tag->setName($tagName)
->setFirstCustomerId(null)
->setFirstStoreId($storeId)
->setStatus($tag->getApprovedStatus())
->save();
}
$tag->saveRelation($product->getId(), null, $storeId);
$updated.= $sku.','.$tagName.'<br >';
}else{
$notFound.= $sku.','.$tagName.'<br >';
}
}
fclose($h);
}
echo $updated;
echo "<hr>";
echo "**************** BELOW TAGS ARE NOT UPDATED ********************************";
echo "<hr>";
echo $notFound;
} catch (Exception $e) {
echo $e->getMessage();
die;
}
精彩评论