Automatic import using Magento cron job


Importing a large quantity of products via the shell.


  1. When the importer loads (when the user has pressed<wbr></wbr>run)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:

 
      
<wbr></wbr>
<?php
<wbr></wbr>

<wbr></wbr>
<wbr></wbr>
$root = '/PATH/TO/YOUR/MAGENTOINSTALLATION/' ;
<wbr></wbr>

<wbr></wbr>
<wbr></wbr>
$username = 'USERNAME' ;
<wbr></wbr>

<wbr></wbr>
<wbr></wbr>
$password = 'PASSWORD' ;
<wbr></wbr>

<wbr></wbr>

<wbr></wbr>

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

<wbr></wbr>
//startingthe import
<wbr></wbr>

<wbr></wbr>
Mage :: getSingleton ( "admin/session" , array ( "name" => "adminhtml" ));
<wbr></wbr>
$session = Mage :: getSingleton ( "admin/session" );
<wbr></wbr>

<wbr></wbr>
try
<wbr></wbr>
{
<wbr></wbr>
$session -> login ( $username , $password );
<wbr></wbr>
} catch ( Exception $e ) {
<wbr></wbr>
echo 'Message: ' . $e -> getMessage ();
<wbr></wbr>
}
<wbr></wbr>

<wbr></wbr>
if ( ! $session -> isLoggedIn ())
<wbr></wbr>
{
<wbr></wbr>
Mage :: log ( convert ( memory_get_usage ()) . " - " . "Could not log in with ' $username '" , null , $logFileName );
<wbr></wbr>
exit ;
<wbr></wbr>
}
<wbr></wbr>

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

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

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:

 
      
<wbr></wbr>
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):

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

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

Try and run the file with the following:

 
      
<wbr></wbr>
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:

 
      
<wbr></wbr>
crontab -e

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

 
      
<wbr></wbr>
*/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.

 
      
<wbr></wbr>
MAILTO =info@youremail.com

Below the Magento line we add our import cron:

 
      
<wbr></wbr>
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:

 
      
<wbr></wbr>
tail -f import.log
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值