您的位置:首页 > 其它

Automatic import using Magento cron job

2012-12-26 18:53 225 查看

Importing a large quantity of products via the shell.

When the importer loads (when the user has pressed run)it parses thecomplete source file (csv, xml) and places it in atemporary database table (dataflow_batch_import).From that table it creates a list of all the rows that need to beimported and it sends that list to the browser.The browser creates a Javascript Ajax request back to magento forevery batch (the number you configured during import).When Magento receives that request, magento starts, imports thebatch, sends the results back to the page and closes.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:
<?php$root='/PATH/TO/YOUR/MAGENTOINSTALLATION/';$username='USERNAME';$password='PASSWORD';//gettingMagentorequire_once$root.'app/Mage.php';ob_implicit_flush();Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);//startingthe importMage::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();echojson_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 7In 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 reindexesit. Call itsomething like import.sh (sh from shell):
echomag_import.php 7php /PATH/TO/YOUR/MAGENTOINSTALLATION/shell/mag_product_import.php7echoindexer.php reindexallphp /PATH/TO/YOUR/MAGENTOINSTALLATION/shell/indexer.phpreindexallTry and run the file with the following:
bash /PATH/TO/YOUR/MAGENTOINSTALLATION/shell/shell_dayly.shIf that all works properly, we can setup our cronjob to do itautomatically every night. Enter the following into you shell:
crontab -eIf you already have setup your Magento cron, you should seesomething like:
*/5 * * * * php -f /PATH/TO/YOUR/MAGENTOINSTALLATION/cron.phpNow, 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.
MAILTO=info@youremail.comBelow the Magento line we add our import cron:
0 0 * * * bash/PATH/TO/YOUR/MAGENTOINSTALLATION/shell/import.shNow 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
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: