magentoimportmagmidatapump

Magmi Datapump API - Item not showing in store


When I add a product with Magmi Datapump API, the product shows up in the Manage Products page but the product is not visible in the store.

I open the product in the Manage Products page for editing and just hit save and the product shows up in the store. Is Magento doing some stuff in the backend when I save the product manually?

Also, when I save the product manually I get this message:

The export profile-product relations has been updated.

This meesage was not there before.


Solution

  • You haven't mentioned the Magento version you're using, but I'll assume it's something in the vicinity of 1.8/1.9 CE or above.

    What is Magmi?

    Magmi is a third party package/utility that lets you import data directly into Magento. It reads the Magento configuration (model's table names, structure, types etc) and uses it to write directly to a MySQL database.

    The purpose of this is that it bypasses the Magento ORM layer - this provides benefits in some situations, primarily speed. The drawback is that unless you're doing it because you know exactly why you're doing it and what the drawbacks of it are already, you miss things that happen with Magento's ORM - particularly event observers and pre/post write callbacks.

    When you save a product

    By default in Magento when you save a product in the admin interface it will write directly to the database through the ORM via the $model->save() operation.

    This process by association (via _afterSave or event observers) does one of two things (if you have flat product tables enabled, which I assume you do):

    Depending on your Magento version the default setting of the product flat index will be important here. 1.9 introduces scheduling them by default I believe, whereas older versions would run them synchronously by default.

    Enterprise edition: If you're running an enterprise version, MySQL table triggers should actually detect these product record changes automatically anyway, and schedule a reindex via the changelog tables (*_cl). For this question I'll assume you're using community edition.

    What you're missing

    Going back to Magmi - it writes directly to the database tables and so skips the ORM layer, which includes the commands for reindexing.

    When you press Save in Magento admin on a product (and you say this makes your changes work) indexing is performed automatically for this product - synchronously. This behaviour indicates that your product flat index is set to be run "on save" rather than "on schedule". This is why you see the product on the frontend after saving it again in admin, purely because the Magento ORM triggers the reindex for that product which inserts it into the product flat table (e.g. catalog_product_flat_1). If you were aware, these tables are the tables that the frontend of Magento reads from when you have flat tables enabled - this is to avoid the complexity of the EAV model structure and the many joins (and configuration lookup/processing) that are required to read from the product (and other) models. Flat tables (indexed) have everything they need in long, but single rows.

    What you need to do

    So - all that being said - we can assume your product flat index is set to run on save.

    What I would suggest primarily is that you change it to run "on schedule". You can do this from the Magento admin panel via System -> Index Management.

    From here you need to enable the Magento system cron.

    This will work if your CE (community edition) version has MySQL table triggers that update the changelog tables. You can check for this.

    If not, you'll need to trigger a reindex manually. Depending on how you are calling Magmi, you could do this one of two ways:

    Magmi via command line

    Trigger a command line initiated reindex of product related data after you've run the Magmi import command:

    php indexer.php --reindex catalog_product_price,catalog_url,catalog_product_flat
    

    Magmi via PHP

    If you're running Magmi from a thirdparty integration script which is not triggered from command line, or you don't want to add a CLI command like the one above afterwards (again, you don't mention these details), you can tack on a reindexing PHP script to the end of your Magmi datapump call:

    foreach (...) {
        $datapump->ingest($data);
    }
    $datapump->endImportSession();
    
    // reindex now!
    foreach (['catalog_product_price', 'catalog_url', 'catalog_product_flat'] as $indexCode) {
        $process = Mage::getModel('index/indexer')->getProcessByCode($indexCode);
        $process->reindexAll();
    }
    

    TL;DR

    Ensure you reindex your product data after importing with Magmi, since it skips the Magento ORM which would normally take care of this for you.