Magento 1 || Create csv with product data - mpaz-redstage/magento-snippets GitHub Wiki

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);
set_time_limit(0);
ini_set('memory_limit', '-1');

/**
 * Notice: Delete the first line of the spreadsheet (Headers)
 */

require_once 'abstract.php';

class NonCherokeeUpdate extends Mage_Shell_Abstract {

	const PURE_CSV = 'http://172.17.0.1/file.csv';

	const CATEGORY_ID_COLUMN = 8;

	const SKU_COLUMN = 9;

	const HAS_OPTIONS_COLUMN = 10;

	const NAME_COLUMN = 11;

	const STATUS_COLUMN = 24;

	const DESCRIPTION_COLUMN = 37;

	protected $skusWithProblem;

	function show_status($done, $total, $size=30) {

		static $start_time;

		// if we go over our bound, just ignore it
		if($done > $total) return;

		if(empty($start_time)) $start_time=time();
		$now = time();

		$perc=(double)($done/$total);

		$bar=floor($perc*$size);

		$status_bar="\r[";
		$status_bar.=str_repeat("=", $bar);
		if($bar<$size){
			$status_bar.=">";
			$status_bar.=str_repeat(" ", $size-$bar);
		} else {
			$status_bar.="=";
		}

		$disp=number_format($perc*100, 0);

		$status_bar.="] $disp%  $done/$total";

		$rate = ($now-$start_time)/$done;
		$left = $total - $done;
		$eta = round($rate * $left, 2);

		$elapsed = $now - $start_time;

		$status_bar.= " remaining: ".number_format($eta)." sec.  elapsed: ".number_format($elapsed)." sec.";

		echo "$status_bar  ";

		flush();

		// when done, send a newline
		if($done == $total) {
			echo "\n";
		}

	}

	public function getPureData() {
		$file = fopen(self::PURE_CSV, 'r');

		$data = [];
		ini_set('memory_limit', '-1');
		while (($line = fgetcsv($file, 900000, ",")) !== FALSE) {
			$data[] = $line;
		}
		fclose($file);

		return $data;
	}

	public function createCSV($data) {
		$file = fopen('finished.csv', 'w');

		foreach ($data as $row) {
			fputcsv($file, $row);
		}

		fclose($file);
	}

	public function getProductCollection($row) {
		return Mage::getModel('catalog/product')->loadByAttribute('sku', $row[self::SKU_COLUMN]);
	}

	public function insertNewColumns($row) {
		return $row[self::SKU_COLUMN];
	}

	public function insertParent($product) {
		if($product && $product->getTypeId() == "simple") {
			$parentIds = Mage::getModel('catalog/product_type_grouped')->getParentIdsByChild($product->getId());
			if(!$parentIds)
				$parentIds = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($product->getId());
			if(isset($parentIds[0]))
				$parent = Mage::getModel('catalog/product')->load($parentIds[0]);
		}

		return $parent ? $parent->getSku() : '';
	}

	public function insertName($product) {
		return $product->getName();
	}

	public function insertDescription($product) {
		return $product->getDescription();
	}

	public function insertStatus($product) {
		return $product->getStatus();
	}

	public function insertQty($product) {
		$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product);
		return (int)$stock->getQty();
	}

	public function insertPrice($product) {
		return $product->getPrice();
	}

	public function insertSpecialPrice($product) {
		return $product->getSpecialPrice() ;
	}

	public function run() {
		$data = $this->getPureData();
		$aux = 0;
		foreach ($data as $key => $row) {
			$aux++;
			//echo "Processing ..." . (int)$percent . "%\n";
			$this->show_status($aux, count($data));
			$product = $this->getProductCollection($row);
			if (!$product) {
				$this->skusWithProblem[] = $row[self::SKU_COLUMN];
				continue;
			}
			$newCSV[$key]['sku'] = $this->insertNewColumns($row); //sku
			$newCSV[$key]['parent'] = $this->insertParent($product); //parent_id
			$newCSV[$key]['name'] = $this->insertName($product); //name
			$newCSV[$key]['description'] = $this->insertDescription($product); //description
			$newCSV[$key]['status'] = $this->insertStatus($product); //status
			$newCSV[$key]['qty'] = $this->insertQty($product); //qty
			$newCSV[$key]['price'] = $this->insertPrice($product); //price
			$newCSV[$key]['special_price'] = $this->insertSpecialPrice($product); //special price
		}

		$this->printOnTerminal($newCSV);

		$this->createCSV($newCSV);
		echo "\n\n\n||||File created at http://172.17.0.1/finished.csv\n";

		echo "Skus with problems \n";

		foreach ($this->skusWithProblem as $problem) {
			echo $problem . "\n";
		}
	}

	public function printOnTerminal($newCSV) {
		echo "====================================" . "\n";
		foreach ($newCSV as $key => $row) {
			if ($key <= 20) {
				var_dump($row);
			}
		}
		echo "====================================" . "\n";
	}
}

$shell = new NonCherokeeUpdate();
$shell->run();