The Following file will downlode the csv from ftp server to local server process the file update inventory,delete the files from server and local server

<?php
	define('MAGENTO', realpath(dirname(__FILE__)));
	require_once MAGENTO . '/app/Mage.php';
	Mage::setIsDeveloperMode(true);
	ini_set('display_errors', 1);
	umask(0);
	Mage::app('admin');
	Mage::register('isSecureArea', 1);
	Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
 
	set_time_limit(0);
	ini_set('memory_limit','10024M');
	$pCollection = Mage::getSingleton('index/indexer')->getProcessesCollection(); 
	foreach ($pCollection as $process) {
		$process->setMode(Mage_Index_Model_Process::MODE_MANUAL)->save();
	}
	// ftp ser details
	$ftp_server = "your_server";
	$ftp_username = "username";
	$ftp_userpass = "password";
	$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
	$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
	$contents = ftp_nlist($ftp_conn, "/ECOM_MV/MBTUK/INVENTORY/FRONTEND/TEST/");
	foreach ($contents as &$value) :
	// download server file
	ftp_get($ftp_conn, MAGENTO . '/var/import/Inventory/inventory_update.csv', $value, FTP_BINARY);
	//ftp_delete($ftp_conn, $value); // if you want to remove file from server
	if(FALSE === @fopen(MAGENTO . '/var/import/Inventory/inventory_update.csv', 'r')):
		echo "No File Found";
	else:
		$count = 0;
		$countItem = 0;
		$file = fopen(MAGENTO . '/var/import/Inventory/inventory_update.csv', 'r');
		while (($line = fgetcsv($file)) !== FALSE) { 
			if ($count == 0) {
				foreach ($line as $key=>$value) {
					$cols[$value] = $key;
				}
			} 
			$count++;
			if ($count == 1) continue;
			#Convert the lines to cols 
			if ($count > 0) { 
				foreach($cols as $col=>$value) {
					unset(${$col});
					${$col} = $line[$value];
				} 
			}
			// Check if SKU exists
			$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku); 
			if ( $product ) {
			
				$productId = $product->getIdBySku($sku);
				$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
				$stockItemId = $stockItem->getId();
				$stock = array();
				if (!$stockItemId) {
					$stockItem->setData('product_id', $product->getId());
					$stockItem->setData('stock_id', 1); 
				}
				else {
					$stock = $stockItem->getData();
				}
				foreach($cols as $col=>$value) {
					
					$stock[$col] = $line[$value];
				}
				foreach($stock as $field => $value) {
					$stockItem->setData($field, $value?$value:0);
					$stockItem->setData('is_in_stock',$stock[$col] > 0 ? 1 : 0);
				}
				$stockItem->save();
				unset($stockItem);
				unset($product);
				$countItem++;
			}
			echo "<br />SKU = ".$sku;
		}
		echo "<br /><br />Total Updated SKU = ".$countItem;
		echo "<br /><br />Inventory Updated For the Above SKU Successfully";
		fclose($file);
		unlink(MAGENTO . '/var/import/Inventory/inventory_update.csv');
	endif;
	endforeach;
	ftp_close($ftp_conn);
	$pCollection = Mage::getSingleton('index/indexer')->getProcessesCollection(); 
	foreach ($pCollection as $process) {
		$process->setMode(Mage_Index_Model_Process::MODE_REAL_TIME)->save();
		$process->reindexEverything();
	}
?>

working-on-a-computer-smiley-emoticon

Categories: Magento

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *