Migrating data into Magento can always be a pain. So I wrote a quick script that enabled you to go through a csv of email address and rewards points balances and import them into Aheadworks Points and Rewards.
I imagine it would only be a small adjustment to get this to work with points and rewards module shipped with Magento Enterprise but that wont be covered here.
Firstly, your CSV file has to be formatted like this but without the table headers :
Email Address | Points Balance |
---|---|
test@example.com | 500 |
test+2@example.com | 200 |
test+3@example.com | 100 |
test+4@example.com | 10 |
The script goes through each line in the csv and looks up the customer by the email address and only updates customers that exist in Magento.
I called my CSV file ‘test-csv.csv’ you will need to update the below script to match what you called it.
Add a php file called update-points.php to your magneto root and paste the following code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<?php //create an array of customer email addresses with reward points $csvData = file_get_contents('./test-csv.csv'); //change this to whatever you called your file $lines = explode(PHP_EOL, $csvData); $customers = array(); foreach ($lines as $line) { $customers[] = str_getcsv($line); } $mageFilename = 'app/Mage.php'; // relative path to Mage.php require_once $mageFilename; Mage::init(); $lineNo = 1; foreach($customers as $customer) { $emailAddress = $customer[0]; $points = $customer[1]; if($emailAddress && $points) { if($points > 0) { $customer = Mage::getModel("customer/customer") ->setWebsiteId(Mage::app()->getWebsite()->getId()) ->loadByEmail($emailAddress); if($customer->getEmail()) { var_dump( 'Row : '.$lineNo. ' email: '.$emailAddress.' is being assigned '.$points . ' points' ); Mage::getModel('points/api')->addTransaction($points,'added_by_admin',$customer,null, array( 'comment' => 'Points carried over from old site' ), array( 'points_expiration_days' => '' ) ); } else { var_dump($emailAddress . ' could not be found'); Mage::log('email: '.$emailAddress.' could not be found in magento the row is: '.$lineNo,null,'points-update.log',true); } } else { var_dump($customer->getEmail() . ' has a negative amount of points NOT IMPORTED'); Mage::log('email: '.$emailAddress. 'has a negative amount of points: '.$points . ' NOT IMPORTED',null,'points-update.log',true); } $lineNo++; } } |
This file can be ran by running php update-points.php
from your magento root directory you should see something like this :
Magento Migrate Reward Points into Aheadworks Points and Rewards