Magento Themes and Magento Extensions

Batch Product Entity Updates in Magento

Share

Batch Product Entity Updates in Magento
I’ve been writing scripts that interact directly with the database to modify products, etc., but today, I found a much better way to do things. Although direct DB modification is FAST, it’s also very unsafe, and I’m very wary of putting our clients’ sites at risk in order to do the updates they want. Magento, although very frustrating, offers some flexible functionality to alter its often immense database. Following is a PHP script I wrote to update products based on a CSV. The CSV contains product SKUs, but it can be easily modified to grab IDs from a CSV, and one can export their products and modify the fields they want, and then modify the script slightly to modify entities where needed, and grab the correct columns from the database. It will also resume if it needs to be terminated early (useful because it can take a long time for large updates), and reports percentage of completion. So, without further ado….


/**************************************************
 *   Product Entity Update Script                 *
 *    @author: Benjamin Yunker                    *
 *    @version: 0.1                               *
 **************************************************/

/* Settings for memory, precision, and error display */
ini_set('memory_limit', '1024M');
ini_set("precision", 2);
error_reporting(E_ALL);
ini_set('display_errors', '1');

/* Path to resume key file */
$STATUS_FILE = ".importstatus";

/* FILE CONFIGURATION */
$MAGE_PATH = "www/app/Mage.php";
$DATA_FILE = "catUpdate.csv";

/* Try to read key file, and set to stored file or beginning of file */
$fh = @fopen($STATUS_FILE, 'r');
if($fh) {
	$RESUME_POINT = fread($fh, filesize($STATUS_FILE));
	fclose($fh);
	echo "\nStarting from key ".$RESUME_POINT;
} else {
	echo "\nNo Import found, starting from beginning…";
	$RESUME_POINT = 0;
}

/* Magento's Mage.php path */
require_once ($MAGE_PATH);
umask(0);
Mage::app("default"); 

$cProd = Mage::getModel('catalog/product'); /* Load Product model */

echo "\nLoading Data…\n";
/* Prepare for percentage count, gather row totals */
$count = 0;
$total = 0;
if (($handle = fopen($DATA_FILE, "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
        $curr_id = $cProd->getIdBySku($data[1]);
        if ($curr_id > 0) {
        	if ($count getIdBySku($data[1]);
        if ($curr_id > 0) {
        	if ($count load($curr_id);

			/* Data loaded from CSV columns */
			$new_description = $data[0];
			$new_brand = $data[2];

			/* Product Entity Modifications */
			if ($new_description > "")
				$newProd->setDescription($new_description);
			if ($new_brand > "")
				$newProd->setBrand($new_brand);

			/* Save Product Entity */
			$newProd->save(); 

			/* Calculate Percentage Done */
			$percent = (++$actual / $total) * 100;

			/* Save key to file, for resume */
			$fh = fopen($STATUS_FILE, 'w') or die("File Open Error, will not resume…\n");
			fwrite($fh, $count+$actual);
			fclose($fh);
			echo "\nResume Key: ".($count+$actual);
			/* Nice output */
			system("clear");
        	echo "\n33[".(($percent==100) ? "0;32m" : "1;31m").$actual."33[0m / 33[0;32m".$total."33[0m";
			echo "\nSaved product 33[0;34m".$curr_id."33[0m - now at 33[".
(($percent==100) ? "0;32m" : "1;31m").$percent."%33[0m\n";
		}
    }
    fclose($handle);

    echo "\n\n33[0;32mAll Done!33[0m\n";
}

Comments are closed.