For one of our Magento webshops, one of the requirements was that the product feed was imported automatically overnight. This ment that I had to find a way to automate this process, but since Magento doesn't support a non-browser productimport this was easier said then done. After looking for a while, I found the following topic on the magento forums. This looked good, this meant I could use the regular Import/Export profiles and run them in the shell. After investigating a bit further, I discovered I could also use Advanced Import/Export profiles - what was just what I needed. So I set up my Advanced profile (one for importing products, one for importing product relations) and did my first first testrun. Everything went well, but after about 1000 - 2000 products It suddenly crashed: PHP Allowed Memory Size Exchausted Fatal Error. That is just great ...
Please note: This functionality was created for Magento 1.4.0.1 and is not tested for Magento 1.5+ and therefor may not work as expected. This script has been replaced with a new script (tested with 1.6.2 and 1.7.0), which can be found on H&O's GitHub Page.
Importing a large quantity of products via the shell.
I wondered what happened. After logging my memory usage a bit more, I noticed that after every product that was saved, the memory usage got a little bit higher every time until it eventually crashed. How could this happen, and why does the regular import doesn't have this problem? It seems that Magento doesn't free the used memory AFTER saving a product. So after a 1000-2000 product saves, the system is out of memory. One other thing I noticed, is that the import also gets much slower after imported a few hundred products.
Why does the browser import doesn't have this problem? Why doesn't it crash and why doesn't it get slower? That is because the browser import does something different:
-
When the importer loads (when the user has pressed run) it parses the complete source file (csv, xml) and places it in a temporary database table (dataflow_batch_import).
-
From that table it creates a list of all the rows that need to be imported and it sends that list to the browser.
-
The browser creates a Javascript Ajax request back to magento for every batch (the number you configured during import).
-
When Magento receives that request, magento starts, imports the batch, sends the results back to the page and closes.
-
When the page receives the results back, it prints them, and starts over with step three.
This process solves the problem that Magento will run out of memory and the import getting slower. If we were to call the importpage from the shell in some way, this solution wouldn't work, since the page depends on Javascript to place it's Ajax requests. What we need to do, is recreate this step within the shell. Since the shell can't execute Javascript we'll have to recreate this in PHP (in the shell, PHP is allowed to run for an infinite amount of time). In PHP we create cURL requests instead of Ajax requests. We fully simulate the browser-import-situation.
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:
mag_login.php:
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:
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):
Try and run the file with the following:
If that all works properly, we can setup our cronjob to do it automatically every night. Enter the following into you shell:
If you already have setup your Magento cron, you should see something like:
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.
Below the Magento line we add our import cron:
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:
Further reading:
Making your import faster is always great: How to speed up your magento import.
H&O is expert in het oplossen van complexe Magento vraagstukken, maatwerk grafisch ontwerp en het bouwen van custom Magento modules.
Als je op zoek bent naar een partner die je kan helpen met het beantwoorden van moeilijke Magento vragen, een maatwerk productimport voor je ontwikkeld of een custom Magento module voor je schrijft die jouw assortiment met een andere webshop koppelt, dan ben je aan het juiste adres. H&O heeft zich sinds 2007 verdiept in de techniek en ontwikkelingen van het e-commerce CMS, speelt een actieve rol in de community, heeft verschillende modules uitgebracht en heeft voor klanten uiteenlopende technische modules ontwikkeld. Ons kantoor is gevestigd in Roelofarendsveen, direct naast de A4 én slechts op 15 minuten afstand van Amsterdam. Neem contact met ons op en kom eens langs, dan bespreken we jouw vraagstuk!
Paul Hachmang
Paul Hachmang is a Back-end Developer from Roelofarendsveen (The Netherlands) and co-founder of H&O. His weapons of choice include PHP, MySQL, HTML, Javascript, ExpressionEngine, Magento and a good cup of coffee. Contact Paul Hachmang
Alta
Paul Hachmang
David O B
David O B
David O B
Paul Hachmang
MagentoKnight
Idealclic
Paul Hachmang
Idealclic
Tiron
Idealclic
Magento Knight
Eduard Fabra
Paul Hachmang
Bojan
Allard Muis
Allard Muis
Paul Hachmang
bojan
Rich
Christian Schwan
Drew
Erwin Otten
Markus Fuchs
Inz
Paul Hachmang
Djé
Josh
Nick
Nick
Matt Ramage
Cao
Cao
stephane
Gareth Shaw
Dave Pearson
Dave Pearson
Stuart Drennan
Auto importeren
DW
Daniel Gafitescu
website design
Magento UK
Daniel
Annemieke de rijp
custom papers
best cover letter ever
thesis writers
spss in research methodology
(nov 07)