在magento中导入产品评论CSV

时间:2015-09-25 11:21:15

标签: php shell magento csv

我正在努力将csv的产品评论导入我的magento。

enter image description here

csv位于我的magento shell文件夹中。我在shell目录中创建了一个product_review.php脚本

<?php 
require '../app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
ini_set('max_execution_time', 84000);
ini_set('memory_limit', '5120M');
$fp = fopen('final_available_dup.csv', 'r');
//Mage::app()->setCurrentStore(4); //desired store id 
while($line = fgetcsv($fp)) {
 $review = Mage::getModel('review/review'); 
 $review->setEntityPkValue($line[0]);//previews1.csv
 $review->setCreatedAt($line[1]);//previews1.csv
 $review->setStatusId($line[2]); //approved
 $review->setTitle($line[3]);
 $review->setNickname($line[4]);  
 $review->setDetail($line[5]); 
 $review->setEntityId($line[6]);                                       
 $review->setStoreId(Mage::app()->getStore()->getId());                      
 //$review->setStatusId($line[5]);  
 $review->setCustomerId($line[7]);//null is for administrator 
 $review->setReviewsCount($line[8]);//null is for administrator
 $review->setReviewId($review->getId()); 
 $review->setStores(array(Mage::app()->getStore()->getId()));                     
 $review->save(); 
 $review->aggregate(); 
}

?>

当我运行shell文件夹并运行product_review.php时,出现了一个空白页,我猜这是正确的方法。

但当我进入后端并检查我看不到任何评论时。我无法查看评论的哪个产品。

enter image description here

我不知道还有什么我应该做的吗?

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码导入产品评论。

我的CSV格式如下:

"created_at","Sku","status_id","title","detail","nickname","customer_id","option_id","entity_id"
"2016-04-01 19:42:09","1991474","2","Top","Blij van!!","claes wassenaar","","1:4@3:15@2:9","24582"


因此,请确保在以下代码中编辑CSV路径并为变量分配适当的值。
    

    require_once 'app/Mage.php';
    Mage::app();

    $fileLocation = "var/import/import_review.csv"; // Set your CSV path here
    $fp = fopen($fileLocation, 'r');
    $count = 1;

    while($data = fgetcsv($fp)){
        if($count > 1){
            //initiate required variables
            $_createdAt     = $data[0];
            $_sku           = $data[1];
            $_catalog       = Mage::getModel('catalog/product');
            $_productId     = $_catalog->getIdBySku($_sku);
            $_statusId      = $data[2];
            $_title         = $data[3];
            $_detail        = $data[4];
            $_customerId    = NULL;
            $_nickname      = $data[5];

            //load magento review model and assign values
            $review = Mage::getModel('review/review');
            $review->setCreatedAt($_createdAt); //created date and time
            $review->setEntityPkValue($_productId);//product id
            $review->setStatusId($_statusId); // status id
            $review->setTitle($_title); // review title
            $review->setDetail($_detail); // review detail
            $review->setEntityId(1); // leave it 1
            $review->setStoreId(Mage::app()->getStore()->getId()); // store id
            $review->setCustomerId($_customerId); //null is for administrator
            $review->setNickname($_nickname); //customer nickname
            $review->setReviewId($review->getId());//set current review id
            $review->setStores(array(Mage::app()->getStore()->getId()));//store id's
            $review->save();
            $review->aggregate();

            //set review ratings
            if($data[7]){
                $arr_data = explode("@",$data[7]);
                if(!empty($arr_data)) {
                    foreach($arr_data as $each_data) {
                        $arr_rating = explode(":",$each_data);
                        if($arr_rating[1] != 0) {
                            Mage::getModel('rating/rating')
                            ->setRatingId($arr_rating[0])
                            ->setReviewId($review->getId())
                            ->setCustomerId($_customerId)
                            ->addOptionVote($arr_rating[1], $_productId);
                        }
                    }
                }
                $review->aggregate();
            }
        }
       // if($count == 5){
       //       die("total $count reviews are imported!");
       //  }
        $count++;
    }

    echo "total $count reviews are imported!";
?>