Magento 2.3. - custom import module with Magmi Integration

514 Views Asked by At

I'm trying to write a magento modulo for importing products from a csv file. I would like to use Magmi to achieve the import. I tried the following code but it doesn't work.

namespace My\Module\Controller\Adminhtml\Home;
  require_once(dirname(__FILE__) . "/../../../../../../../lib/magmi/inc/magmi_defs.php");
  require_once(dirname(__FILE__) . "/../../../../../../../lib/magmi/integration/inc/magmi_datapump.php");

  class Import extends \Magento\Backend\App\Action{

     public function execute()
    {

        // getting data from a casv file 

        for ($items as $item){
           $dp = Magmi_DataPumpFactory::getDataPumpInstance("productimport");
           $dp->beginImportSession("default", "xcreate");
           $item = "product field array";
           $run = $dp->ingest($item);
           $dp->endImportSession();
        }

    }

}

I receive errors on the Magmi classes "class not found". I tried also different code but the only way a think it can works is using class file named, e.g. Datapump.php with e defined class named Datapump. But I cannot rewrite all files of magmi to make it works, so maybe I'm doing something wrong.

1

There are 1 best solutions below

0
Lokesh Naik On

Though not an ideal solution. But you can try this.

1) Make below changes in file magmi/integration/inc/magmi_datapump.php

Change the class name from 'Magmi_DataPumpFactory' to 'Magmi_DataPump'

This is needed because Magento 2 interprets Factory keyword in a different way.

2) Now create an autoloader file

app/code/Vendor/Module/magmi-autoloader.php

<?php

$mapping = array(
    'Magmi_Defs' => BP . '/magmi/inc/magmi_defs.php"',
    'Magmi_DataPump' => BP . '/magmi/integration/inc/magmi_datapump.php'
);

spl_autoload_register(function ($class) use ($mapping) {
    if (isset($mapping[$class])) {
        require $mapping[$class];
    }
}, true);

3) Now change relative paths in below file to absolute path. Since while running magmi classes through Magento, it will be not able to read paths relative to magmi directory.

magmi/inc/magmi_defs.php

<?php

// change below define variables to their absolute paths according to your project's root directory

define("MAGMI_BASEDIR", dirname(dirname(__FILE__)));
define("MAGMI_INCDIR", MAGMI_BASEDIR . '/inc');
define("MAGMI_INTEGRATION_INCDIR", MAGMI_BASEDIR . '/integration/inc');
define("MAGMI_PLUGIN_DIR", MAGMI_BASEDIR.'/plugins');
define("MAGMI_ENGINE_DIR", MAGMI_BASEDIR . '/engines');
.
.
//other code
.
.

4) Now change your controller file as below

<?php

namespace Vendor\Module\Controller\Adminhtml\Home;

require BP.'/app/code/Vendor/Module/magmi-autoloader.php'; 

class Import extends \Magento\Backend\App\Action 
{
    public function execute()
    {
        // getting data from a csv file 

        $dp = \Magmi_DataPump::getDataPumpInstance("productimport");

        // move this outside the loop to prevent mysql max_connection error
        $dp->beginImportSession("default", "xcreate");

        for ($items as $item){
            $item = "product field array";
            $run = $dp->ingest($item);
        }

        $dp->endImportSession();
    }
}