The code consists of two files. mag_login.php and mag_import.php. Both are needed. Place them in /PATH/TO/YOUR/MAGENTOINSTALLATION/shell/
mag_import.php:
<?php
/**
* Path to the root of your magento installation
*/
$root = '/PATH/TO/YOUR/MAGENTOINSTALLATION/';
/**
* Url to your magento installation.
*/
$url = 'http://www.yourmagentostore.com/';
/**
* relative path from the magento root to the login file.
*/
$login = 'shell/mag_login.php';
/**
* name of the logfile, will be places in magentoroot/var/log/
*/
$logFileName= 'import.log';
/**
* how many products will be parsed at each post. Usually 10-50.
*/
$atOnce = 50;
/**
* DO NOT EDIT BELOW THIS LINE
*/
function convert ($size)
{
$unit=array('b','kb','mb','gb','tb','pb');
}
$profileId = $argv[1];
if (! isset($profileId)) {
exit ("\nPlease specify a profile id. You can find it in the admin panel->Import/Export->Profiles.\nUsage: \n\t\t php -f $argv[0] PROFILE_ID\n\t example: php -f $argv[0] 7\n");
}
$recordCount = 0;
//getting Magento
require_once $root.'app/Mage.php';
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
//starting the import
Mage
::log("\n\n", null, $logFileName);
$profile = Mage::getModel('dataflow/profile');
$userModel = Mage::getModel('admin/user');
$userModel->setUserId(0);
Mage::getSingleton('admin/session')->setUser($userModel);
if ($profileId) {
$profile->load($profileId);
if (!$profile->getId()) {
Mage::getSingleton('adminhtml/session')->addError('ERROR: Could not load profile');
}
}
/**
* get het login information.
*/
exec("php -f {$root}{$login}", $result);
$sessionId = $loginInformation->sessionId;
$formKey = $loginInformation->formKey;
//clean dataflow_batch_import table so it doesn't get amazingly big.
$db = Mage::getSingleton('core/resource')->getConnection('core_write');
$db->query("TRUNCATE TABLE `dataflow_batch_import`");
Mage
::log(convert
(memory_get_usage()) . " - " . "Table dataflow_batch_import cleaned", null, $logFileName);
//load profile
if ($profileId) {
$profile->load($profileId);
if (!$profile->getId()) {
Mage::getSingleton('adminhtml/session')->addError('ERROR: Could not load profile');
}
}
Mage::register('current_convert_profile', $profile);
//run the profile
$profile->run();
//get to work
$batchModel = Mage::getSingleton('dataflow/batch');
if ($batchModel->getId()) {
#echo "getId ok\n";
if ($batchModel->getAdapter()) {
#echo "getAdapter ok\n";
$batchId = $batchModel->getId();
Mage
::log(convert
(memory_get_usage()) . " - " . "Loaded batch id $batchId", null, $logFileName);
$batchImportModel = $batchModel->getBatchImportModel();
$importIds = $batchImportModel->getIdCollection();
$batchModel = Mage::getModel('dataflow/batch')->load($batchId);
$adapter = Mage::getModel($batchModel->getAdapter());
$postnum = 0;
$totalproducts = count($importIds);
foreach ($importIds as $importId) {
#echo "importing $importId\n";
$recordCount++;
$postdata[] = "rows[]=$importId";
//echo "$importId ";
if ($recordCount%$atOnce == 0 || $recordCount == $totalproducts) {
$postnum++;
$postdata[] = "batch_id=$batchId";
$postdata[] = "form_key=$formKey";
$postdatastring = implode('&', $postdata);
//print_r($postdatastring);
//Mage::log(convert(memory_get_usage()) . " - Start cURL request #$postnum", null, $logFileName);
curl_setopt($ch, CURLOPT_URL
,$url."index.php/admin/system_convert_profile/batchRun/?isAjax=true");
curl_setopt($ch, CURLOPT_COOKIE
, "adminhtml=$sessionId" );
{
}
else
{
Mage
::log(convert
(memory_get_usage()) . " - {$recordCount}/{$totalproducts} [$buffer]" , null, $logFileName);
if (count($result->errors))
{
foreach ($result->errors as $error)
{
}
}
}
}
}
foreach ($profile->getExceptions() as $e) {
}
}
}
mag_login.php:
<?php
/**
* Path to the root of your magento installation.
* include traing slash.
*/
$root = '/PATH/TO/YOUR/MAGENTOINSTALLATION/';
/**
* Username that has the rights to import products.
*/
$username = 'USERNAME';
/**
* Password
*/
$password = 'PASSWORD';
/**
* DO NOT EDIT BELOW THIS LINE
*/
//getting Magento
require_once $root.'app/Mage.php';
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
//starting the import
Mage
::getSingleton("admin/session", array("name"=>"adminhtml"));
$session = Mage::getSingleton("admin/session");
try
{
$session->login($username, $password);
} catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}
if(! $session->isLoggedIn())
{
Mage
::log(convert
(memory_get_usage()) . " - " . "Could not log in with '$username'", null, $logFileName);
}
$sessionId = $session->getEncryptedSessionId();
$formKey = Mage::getSingleton('core/session')->getFormKey();
Copy the above code to your local Magento installation
, put them in a secure folder
, that isn
't accesible by a browser and login to the shell. Fill in the correct information into the files. After that is done, start up your shell, browse to the folder. Run the file with:
1
php -f mag_import.php 7
In the line above, 7 should be replaced by your own profile Id, which can be found the first column of the profiles.
Making the Magento product import fully automatic.
If we got the above working, we need to get the working fully automatic. We want to import our products and do a complete reindex of magento every night. First we create a file that can be executed by the shell that imports the products and reindexes it. Call it something like import.sh (sh from shell):
1
2
3
4
5
echo mag_import.php 7
php /PATH/TO/YOUR/MAGENTOINSTALLATION/shell/mag_product_import.php 7
echo indexer.php reindexall
php /PATH/TO/YOUR/MAGENTOINSTALLATION/shell/indexer.php reindexall
Try and run the file with the following:
1
bash /PATH/TO/YOUR/MAGENTOINSTALLATION/shell/shell_dayly.sh
If that all works properly, we can setup our cronjob to do it automatically every night. Enter the following into you shell:
1
crontab -e
If you already have setup your Magento cron, you should see something like:
1
*/5 * * * * php -f /PATH/TO/YOUR/MAGENTOINSTALLATION/cron.php
Now, if something happens to our import, somethings goes wrong, etc, we wan't to get notified
. Add the following line at the top of the crontab
file.
1
MAILTO=info@youremail.com
Below the Magento line we add our import cron:
1
0 0 * * * bash /PATH/TO/YOUR/MAGENTOINSTALLATION/shell/import.sh
Now save the crontab (depends on the editor how) and your all set.
Note
: if you want to see error regarding the import
, lines that could not be processed open
/PATH
/TO
/YOUR
/MAGENTOINSTALLATION
/var/log/import
.log or browse to the
file in the shell and type
:
1
mag_import.php:
<?php
/**
* Path to the root of your magento installation
*/
$root = '/PATH/TO/YOUR/MAGENTOINSTALLATION/';
/**
* Url to your magento installation.
*/
$url = 'www.yourmagentostore.com/';
/**
* relative path from the magento root to the login file.
*/
$login = 'shell/mag_login.php';
/**
* name of the logfile, will be places in magentoroot/var/log/
*/
$logFileName= 'import.log';
/**
* how many products will be parsed at each post. Usually 10-50.
*/
$atOnce = 50;
/**
* DO NOT EDIT BELOW THIS LINE
*/
function convert ($size)
{
$unit=array('b','kb','mb','gb','tb','pb');
return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}
set_time_limit(0);
ini_set('memory_limit', '128M');
$profileId = $argv[1];
if (! isset($profileId)) {
exit ("\nPlease specify a profile id. You can find it in the admin panel->Import/Export->Profiles.\nUsage: \n\t\t php -f $argv[0] PROFILE_ID\n\t example: php -f $argv[0] 7\n");
}
$recordCount = 0;
//getting Magento
require_once $root.'app/Mage.php';
ob_implicit_flush();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
//starting the import
Mage::log("\n\n", null, $logFileName);
Mage::log(convert(memory_get_usage()) . " - " . "STARTING IMPORT", null, $logFileName);
$profile = Mage::getModel('dataflow/profile');
$userModel = Mage::getModel('admin/user');
$userModel->setUserId(0);
Mage::getSingleton('admin/session')->setUser($userModel);
if ($profileId) {
$profile->load($profileId);
if (!$profile->getId()) {
Mage::getSingleton('adminhtml/session')->addError('ERROR: Could not load profile');
}
}
/**
* get het login information.
*/
exec("php -f {$root}{$login}", $result);
$loginInformation = json_decode($result[0]);
$sessionId = $loginInformation->sessionId;
$formKey = $loginInformation->formKey;
//clean dataflow_batch_import table so it doesn't get amazingly big.
$db = Mage::getSingleton('core/resource')->getConnection('core_write');
$db->query("TRUNCATE TABLE `dataflow_batch_import`");
Mage::log(convert(memory_get_usage()) . " - " . "Table dataflow_batch_import cleaned", null, $logFileName);
//load profile
if ($profileId) {
$profile->load($profileId);
if (!$profile->getId()) {
Mage::getSingleton('adminhtml/session')->addError('ERROR: Could not load profile');
}
}
Mage::register('current_convert_profile', $profile);
//run the profile
Mage::log(convert(memory_get_usage()) . " - " . "Preparing profile...", null, $logFileName);
$profile->run();
Mage::log(convert(memory_get_usage()) . " - " . "...Done", null, $logFileName);
//get to work
$batchModel = Mage::getSingleton('dataflow/batch');
if ($batchModel->getId()) {
#echo "getId ok\n";
if ($batchModel->getAdapter()) {
#echo "getAdapter ok\n";
$batchId = $batchModel->getId();
Mage::log(convert(memory_get_usage()) . " - " . "Loaded batch id $batchId", null, $logFileName);
$batchImportModel = $batchModel->getBatchImportModel();
$importIds = $batchImportModel->getIdCollection();
$batchModel = Mage::getModel('dataflow/batch')->load($batchId);
$adapter = Mage::getModel($batchModel->getAdapter());
$postdata = array();
$postnum = 0;
$totalproducts = count($importIds);
Mage::log(convert(memory_get_usage()) . " - 0/{$totalproducts}", null, $logFileName);
foreach ($importIds as $importId) {
#echo "importing $importId\n";
$recordCount++;
$postdata[] = "rows[]=$importId";
//echo "$importId ";
if ($recordCount%$atOnce == 0 || $recordCount == $totalproducts) {
$postnum++;
$postdata[] = "batch_id=$batchId";
$postdata[] = "form_key=$formKey";
$postdatastring = implode('&', $postdata);
$postdata = array();
//print_r($postdatastring);
//Mage::log(convert(memory_get_usage()) . " - Start cURL request #$postnum", null, $logFileName);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url."index.php/admin/system_convert_profile/batchRun/?isAjax=true");
curl_setopt($ch, CURLOPT_TIMEOUT, 100 );
curl_setopt($ch, CURLOPT_COOKIE, "adminhtml=$sessionId" );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdatastring);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$buffer = curl_exec($ch);
if (empty($buffer))
{
Mage::log(convert(memory_get_usage()) . " - {$recordCount}/{$totalproducts} - Response is empty - ERROR" . curl_error($ch), null, $logFileName);
}
else
{
$result = json_decode($buffer);
Mage::log(convert(memory_get_usage()) . " - {$recordCount}/{$totalproducts} [$buffer]" , null, $logFileName);
if (count($result->errors))
{
foreach ($result->errors as $error)
{
Mage::log(convert(memory_get_usage()) . " - ERROR: $error", null, $logFileName);
}
}
}
curl_close ($ch);
}
}
foreach ($profile->getExceptions() as $e) {
Mage::log(convert(memory_get_usage()) . " - " . $e->getMessage(), null, $logFileName);
}
}
}
Mage::log(convert(memory_get_usage()) . " - " . "Completed!", null, $logFileName);