Automatic import using Magento cron job


Importing a large quantity of products via the shell.


  1. When the importer loads (when the user has pressedrun)it parses the complete source file (csv, xml) and places it in atemporary database table (dataflow_batch_import).
  2. From that table it creates a list of all the rows that need to beimported and it sends that list to the browser.
  3. The browser creates a Javascript Ajax request back to magento forevery batch (the number you configured during import).
  4. When Magento receives that request, magento starts, imports thebatch, sends the results back to the page and closes.
  5. When the page receives the results back, it prints them, and startsover with step three.
http://www.h-o.nl/blog/automatic-import-with-magento-using-ssh-no-browser-needed/

mag_login.php:



$root = '/PATH/TO/YOUR/MAGENTOINSTALLATION/' ;

$username = 'USERNAME' ;

$password = 'PASSWORD' ;



//gettingMagento
require_once $root . 'app/Mage.php' ;
ob_implicit_flush ();
Mage :: app () -> setCurrentStore ( Mage_Core_Model_App :: ADMIN_STORE_ID );

//startingthe 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 );
exit ;
}

$sessionId = $session -> getEncryptedSessionId ();
$formKey = Mage :: getSingleton ( 'core/session' ) -> getFormKey ();

echo json_encode ( array ( 'sessionId' => $sessionId , 'formKey' => $formKey ));

Copy the above code to your local Magento installation, put them ina secure folder, that isn't accesible by a browser and login to theshell. Fill in the correct information into the files. After thatis done, start up your shell, browse to the folder. Run the filewith:


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 fullyautomatic. We want to import our products and do a complete reindexof magento every night. First we create a file that can be executedby the shell that imports the products and reindexes it. Call itsomething like import.sh (sh from shell):


echomag_import.php 7
php /PATH/TO/YOUR/MAGENTOINSTALLATION/shell/mag_product_import.php7

echoindexer.php reindexall
php /PATH/TO/YOUR/MAGENTOINSTALLATION/shell/indexer.phpreindexall

Try and run the file with the following:


bash /PATH/TO/YOUR/MAGENTOINSTALLATION/shell/shell_dayly.sh

If that all works properly, we can setup our cronjob to do itautomatically every night. Enter the following into you shell:


crontab -e

If you already have setup your Magento cron, you should seesomething like:


*/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 ofthe crontab file.

Below the Magento line we add our import cron:


0 0 * * * bash/PATH/TO/YOUR/MAGENTOINSTALLATION/shell/import.sh

Now save the crontab (depends on the editor how) and your allset.

Note: if you want to see error regarding the import, lines thatcould not be processed open/PATH/TO/YOUR/MAGENTOINSTALLATION/var/log/import.log or browse tothe file in the shell and type:


tail -f import.log

你可能感兴趣的:(Automatic import using Magento cron job)