<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>gj |</title>
	<atom:link href="https://blog.gaiterjones.com/category/magento2/product-review-import/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.gaiterjones.com/category/magento2/product-review-import/</link>
	<description>gaiterjones</description>
	<lastBuildDate>Sun, 12 Jan 2020 12:00:33 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.4.3</generator>
	<item>
		<title>Exporting Magento 1 Product Reviews and Importing them to Magento 2</title>
		<link>https://blog.gaiterjones.com/exporting-magento-1-product-reviews-and-importing-them-to-magento-2/</link>
					<comments>https://blog.gaiterjones.com/exporting-magento-1-product-reviews-and-importing-them-to-magento-2/#respond</comments>
		
		<dc:creator><![CDATA[PAJ]]></dc:creator>
		<pubDate>Sat, 11 Jan 2020 17:38:43 +0000</pubDate>
				<category><![CDATA[Mage Migration]]></category>
		<category><![CDATA[Magento]]></category>
		<category><![CDATA[Magento Product Export]]></category>
		<category><![CDATA[Magento2]]></category>
		<category><![CDATA[Product Review Import]]></category>
		<guid isPermaLink="false">http://blog.gaiterjones.com/?p=2123</guid>

					<description><![CDATA[I am Migrating Magento 1.9 to 2.3 &#8211; yikes! I wasn&#8217;t able to use the Magento 2 migration tool for products so one of the tasks on my TO DO...<a class="more-link" href="https://blog.gaiterjones.com/exporting-magento-1-product-reviews-and-importing-them-to-magento-2/" title="Continue reading">Continue reading</a>]]></description>
										<content:encoded><![CDATA[<p>I am Migrating Magento 1.9 to 2.3 &#8211; yikes!</p>
<p>I wasn&#8217;t able to use the Magento 2 migration tool for products so one of the tasks on my TO DO list was product review migration. Looking around for (free) solutions the only stuff I came across was no longer free or didn&#8217;t work. $100 to import 500 reviews &#8211; I don&#8217;t think so.</p>
<p>If you are looking to do something similar here is a quick copy and paste of the php code I used for the Magento 1 export, and the Magento 2 import.</p>
<p>I am assuming you can plug this into your existing Magento php cli tools which already bootstrap the corresponding Magento1 or 2 core classes. For Magento 1 I first took a full product collection and parsed each product for review data. All the data is saved to a tab seperated CSV file which is then used for the Magento 2 product review import.</p>
<figure style="width: 800px" class="wp-caption aligncenter"><img fetchpriority="high" decoding="async" src="https://blog.gaiterjones.com/dropbox/mage-product-review-migration.gif" width="800" height="600" alt="Magento Migration Product Review Migration" class="size-large" /><figcaption class="wp-caption-text">Magento Migration Product Review Migration</figcaption></figure>
<h2>Magento 1.9 product review export to csv</h2>
<pre class="brush: php; title: ; notranslate">
echo $c(&quot;MAGE1 Migration - Export MAGE1 Product Reviews to CSV&quot;)-&gt;header. &quot;\n\n&quot;;

// LOAD ALL PRODUCTS
//
$_storeID=0; // admin

$_reviewVisibleInStoreViews=array(1,2);

$_storeLocale&#x5B;0]='admin';
$_storeLocale&#x5B;1]='de';
$_storeLocale&#x5B;2]='en';
$this-&gt;getProductCollectionAllSKUs($_storeID);

$_products=$this-&gt;get('collection');
$_reviewData=array();
$_count=0;

foreach($_products as $_id=&gt;$_product)
{
    $_sku = $_product-&gt;getSku();
    $_productId = $_product-&gt;getId();
    $_reviewcCollection = \Mage::getModel('review/review')-&gt;getCollection()
        -&gt;addStoreFilter($_storeID)
        -&gt;addEntityFilter('product', $_productId)
        -&gt;addStatusFilter(\Mage_Review_Model_Review::STATUS_APPROVED)
        -&gt;setDateOrder()
        -&gt;addRateVotes();

        if (count($_reviewcCollection))
        {

            $_productReview=array();

            foreach($_reviewcCollection as $_review) {

                $_ratings=array();
                $votes = $_review-&gt;getRatingVotes();
                $total = 0;

                foreach($votes as $vote)
                {
                    $total += $vote-&gt;getPercent();
                    $_ratings&#x5B;]=$vote-&gt;getValue();
                }

                $avgRating = $total / count($votes);

                $_reviewDetail=trim(preg_replace('/\s\s+/', '&lt;br&gt;', $_review&#x5B;'detail']));
                $_reviewTitle=trim(preg_replace('/\s\s+/', '&lt;br&gt;', $_review&#x5B;'title']));

                //var_dump($total, count($votes), $avg);
                $_productReview&#x5B;]=array(
                    'createdat' =&gt; $_review-&gt;getCreatedAt(),
                    'review' =&gt; $_reviewDetail,
                    'summary' =&gt; $_reviewTitle,
                    'nickname' =&gt; $_review&#x5B;'nickname'],
                    'customer_id' =&gt; ($_review&#x5B;'customer_id'] ? $_review&#x5B;'customer_id'] : null),
                    'ratings' =&gt; implode('|', $_ratings),
                    'visibilty' =&gt; implode('|', $_reviewVisibleInStoreViews),
                );

                $_count++;
                echo '.';

            }

            $_reviewData&#x5B;$_sku]=$_productReview;

        }

}

//print_r($_reviewData);
echo &quot;\n&quot;.$c($_count. ' product reviews exported for store view '. $_storeID)-&gt;info.&quot;\n&quot;;

// EXPORT CSV
//
//
$_now = new \DateTime(null, new \DateTimeZone('Europe/Berlin'));
$_reviewExportFilename='/home/data/Mage1MigrateReviewsProducts_'.$_now-&gt;format('Y-m-d').'_'.$_storeLocale&#x5B;$_storeID].'.csv';

$_reviewExportHeader='sku,createdat,review,summary,nickname,customer_id,ratings,visibility';
$_reviewExportHeader=explode(',',$_reviewExportHeader);

if (file_exists($_reviewExportFilename)) {unlink($_reviewExportFilename);}

// -- csv seperator
$_seperator=&quot;\t&quot;; // TAB
//$_seperator=','; // COMMA

ini_set('default_charset','UTF-8');

$_fileHandle = fopen($_reviewExportFilename, 'w');

    // write UTF8-BOM
    //fwrite($_fileHandle, pack(&quot;CCC&quot;,0xef,0xbb,0xbf));

    // write header
    if ($_reviewExportHeader) {fwrite($_fileHandle, implode($_seperator, $_reviewExportHeader).&quot;\r\n&quot;);}

    echo $c('Creating CSV export file '. $_reviewExportFilename)-&gt;info.&quot;\n&quot;;

    // write data
    foreach ($_reviewData as $_sku =&gt; $_reviews)
    {
        foreach ($_reviews as $_review)
        {
            fwrite($_fileHandle, $_sku.$_seperator.implode($_seperator, $_review).&quot;\r\n&quot;);
        }
    }

fclose($_fileHandle);

echo $c('Product review export complete!')-&gt;success. &quot;\n&quot;;

</pre>
<h2>Magento 2 csv product import</h2>
<pre class="brush: php; title: ; notranslate">
&lt;?php

	echo $c(&quot;MAGE2 Import Product Reviews from MAGE1 Migration CSV&quot;)-&gt;header. &quot;\n\n&quot;;

	$_reviewDataCSVFile='/home/data/Mage1MigrateReviewsProducts_2020-01-11_admin.csv';
	$_reviewDataCSV=file('/home/data/Mage1MigrateReviewsProducts_2020-01-11_admin.csv', FILE_IGNORE_NEW_LINES);

	echo $c(&quot;Importing from &quot;. $_reviewDataCSVFile)-&gt;info. &quot;\n&quot;;

	$_count=0;
	$_total=(count($_reviewDataCSV)-1);
	foreach ($_reviewDataCSV as $_key =&gt; $_data)
	{
		if ($_key===0){ // build header
			$_keys=explode(&quot;\t&quot;,trim($_data));
			continue;
		}

		$_dataArray=explode(&quot;\t&quot;,$_data);

		foreach ($_dataArray as $_key =&gt; $_data)
		{
			$_reviewData&#x5B;$_keys&#x5B;$_key]]=$_data;
		}

		//print_r($_reviewData);

		try
		{
			$_success=false;

            $_obj=new \PAJ\Library\Magento2\Reviews\AddReview(
                array(
					'reviewdata' =&gt; $_reviewData,
					'storeid' =&gt; '0'
                )
            );
				$_output=$_obj-&gt;get('output');
				$_success=$_obj-&gt;get('success');

			if($_success)
			{
				$_count++;
				echo $c(&quot;Review &quot;. $_count.&quot;/&quot;.$_total. &quot; imported.&quot;)-&gt;success. &quot;\n&quot;;
				//print_r($_output);

			}

		}
		catch (\Exception $e)
		{
			// catch bad guys
			//throw new \Exception($e);
			echo $c(&quot;ERROR: &quot;. $e-&gt;getMessage())-&gt;error. &quot;\n&quot;;
			continue;
		}

		// 1 product test exit;
		//exit;
	}


if (!$_silent) {

	echo 'Import complete.'. &quot;\n&quot;;
}


exit;


class MagentoAddReview extends AbstractApp
{

    /**
     * @var \Magento\Review\Model\RatingFactory;
     */
    private $_ratingFactory;
    /**
     * @var \Magento\Review\Model\ReviewFactory;
     */
    private $_reviewFactory;

    public function run()
    {
        // load a product
        //

        // init vars
        $_data=array();

        $_storeID=$this-&gt;_variables&#x5B;'storeid'];
        $_store = $this-&gt;_storeManagerInterface-&gt;getStore($_storeID);
        $this-&gt;_storeManagerInterface-&gt;setCurrentStore($_store);
        $_reviewData=$this-&gt;_variables&#x5B;'reviewdata'];

        $_sku=$_reviewData&#x5B;'sku'];
        $_customerId=$_reviewData&#x5B;'customer_id'];

        //for Guest user $_customerId=Null;
        //
        if (!$_customerId){$_customerId=Null;}

        // create review data
        //
        $_customerNickName=$_reviewData&#x5B;'nickname'];
        $_reviewTitle=$_reviewData&#x5B;'summary'];
        $_reviewDetail=preg_replace('#&lt;br\s*/?&gt;#i', &quot;\n&quot;, $_reviewData&#x5B;'review']);
        $_title=preg_replace('#&lt;br\s*/?&gt;#i', &quot;\n&quot;, $_reviewData&#x5B;'summary']);
        $_createdAt=$_reviewData&#x5B;'createdat'];

        // store visibility array, i.e. 1,2 for visibile in store view 1 and 2
        // 
        $_reviewVisibleInStoreViews=explode('|',$_reviewData&#x5B;'visibility']);
        $_ratings=explode('|',$_reviewData&#x5B;'ratings']);

        $_product = $this-&gt;_productRepository-&gt;get($_sku);
        $_productId=$_product-&gt;getId();

        $_reviewFactory = $this-&gt;getReviewFactory();
        $_ratingFactory = $this-&gt;getRatingFactory();

        foreach ($_ratings as $_key =&gt; $_rating)
        {
            $_reviewFinalData&#x5B;'ratings']&#x5B;$_key+1] = $_rating;
        }

        $_reviewFinalData&#x5B;'nickname'] = $_customerNickName; //add user nickname
        $_reviewFinalData&#x5B;'title'] = $_title; //add title of the review
        $_reviewFinalData&#x5B;'detail'] = $_reviewDetail; //add details of the review
        $_reviewFinalData&#x5B;'customerid'] = $_customerId;

        $review = $_reviewFactory-&gt;create()-&gt;setData($_reviewFinalData);

        $review-&gt;unsetData('review_id');
        $review-&gt;setEntityId($review-&gt;getEntityIdByCode(\Magento\Review\Model\Review::ENTITY_PRODUCT_CODE))
            -&gt;setEntityPkValue($_productId)
            -&gt;setStatusId(\Magento\Review\Model\Review::STATUS_APPROVED) //By default set approved
            -&gt;setStoreId($_storeID)
            -&gt;setCreatedAt($_createdAt)
            -&gt;setStores($_reviewVisibleInStoreViews)
            -&gt;save();

        foreach ($_reviewFinalData&#x5B;'ratings'] as $ratingId =&gt; $rating) {
            $_ratingFactory-&gt;create()
                -&gt;setRatingId($ratingId)
                -&gt;setReviewId($review-&gt;getId())
                -&gt;addOptionVote($rating, $_productId);
        }
        $review-&gt;aggregate();

        //$this-&gt;debug($_data);
        $_data&#x5B;'store']&#x5B;'storeid']=$_storeID;
        $_data&#x5B;'reviews']=array(
            'productid'=&gt; $_productId
        );


        return &#x5B;'magento' =&gt; $_data];
    }

    /**
     * Get Factories
     *
     */
    private function getRatingFactory()
    {
        if (!$this-&gt;_ratingFactory) {
            $this-&gt;_ratingFactory = \Magento\Framework\App\ObjectManager::getInstance()
                -&gt;get(\Magento\Review\Model\RatingFactory::class);
        }
        return $this-&gt;_ratingFactory;
    }
    private function getReviewFactory()
    {
        if (!$this-&gt;_reviewFactory) {
            $this-&gt;_reviewFactory = \Magento\Framework\App\ObjectManager::getInstance()
                -&gt;get(\Magento\Review\Model\ReviewFactory::class);
        }
        return $this-&gt;_reviewFactory;
    }
}

</pre>
<p>The echo&#8217;s use the cool colours from <a href="https://github.com/kevinlebrun/colors.php">https://github.com/kevinlebrun/colors.php</a> The will probably throw an error for you, sorry!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.gaiterjones.com/exporting-magento-1-product-reviews-and-importing-them-to-magento-2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
