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.
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
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
4) Now change your controller file as below