4

Is there a way to customize the Magmi plugin so that an import.csv can be added directly from the browser?

Currently, we are using FTP but it is not practical for our admin to be logging in and out of FTP changing permissions on our firewall etc just to add products.

Failing this is there a way to upload files directly from the backend?

3
+50

Actually, this is out of the scope of magento.stackexchange.com - yet, I think that the missing possibility of uploading files (even though I've never questioned it and went for FTP uploads or remote SQL data sources) is actually a draw back of Magmi.

The solution is rather simple, though:

Magmi is working with plugins managed in different "scopes" and "names spaces" (if we wish to use that terminology) not unsimilar to Magento. So basically you can create plugins and hook them into different areas of Magmi.

To make it easier I'll assume you're having Magmi uploaded to a folder called magmi.

So, magmi/plugins is the place where the individual functionality resides. In there you got something like your "Vendor" directories (and some other stuff that doesn't really fit into that definition) - by default I would say the following could be seen as "Vendors" (this list may slightly differ based on your version and download source):

  • 5b5
  • base
  • extra
  • garbocom

Within these directories you got up to three directories that then stand for particular area in the GUI (see figure 1):

  • datasources
  • general
  • itemprocessors

Hence, magmi/plugins/{vendor name}/{GUI area} would be the place to put any custom plugin you wish to use in order to keep your system as update-proof as possible.


Now since we know where we can put stuff, let's take a quick look at how and what to put.

To keep it simple, I'll just go for a real basic implementation (people may argue that it looks horrible or isn't really the best approach - but from my point of view an iframe is the easiest way of getting a form inside another form as I'm going to integrate the file upload inside the profile configuration section and do not want to have it saved when the profile itself is saved).

Most plugins holds a options_panel.php - this is basically your template file.

So for me this is magmi/plugins/weboffice/datasources/csvupload/options_panel.php:

<div class="plugin_description">
    This plugin enables magmi to upload CSV files.<br/>
</div>
<iframe src="../plugins/weboffice/datasources/csvupload/csv_upload.php" id="upload_target" name="upload_target" src="#" style="width:auto;height:auto;border:0px solid #fff;"></iframe>

Next, we need the iframe's source magmi/plugins/weboffice/datasources/csvupload/csv_upload.php:

<form action="csv_upload.php" method="post" enctype="multipart/form-data">
    Select file to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload File" name="submit">
</form>

<?php

require_once("../../../../inc/magmi_config.php"); // just calling it to get Magento's base dir which is stored in Magmi's config already. Something similar could/should be done for the CSV basedir four lines below as well.
$conf = Magmi_Config::getInstance();
$conf->load();
$conf_ok = 1;

$csvLocation = "var/import/";

$target_dir = $conf->get("MAGENTO", "basedir").$csvLocation;
echo "Destination: ". $target_dir."<br>";

if(isset($_POST["submit"])) {

    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $uploadFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
    // Check if file already exists
    if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
    }
    // Allow certain file formats
    if($uploadFileType != "csv") {
        echo "Sorry, only CSV files are allowed.";
        $uploadOk = 0;
    }
    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    // if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
            echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }
}

The last step is to a particular are (in our case the datasource) to add the plugin into the list of available items in Magmi:

Honestly, I didn't dig too deep into the naming convention nor the way how Magmi loads plugins, so this needs to do first of all:

magmi/plugins/weboffice/datasources/csvupload/weboffice_csvupload.php:

<?php

class Magmi_CsvUpload extends Magmi_DataSource {

    public function getPluginInfo() {

        return array("name"=>"Simple CSV Uploader","author"=>"Christoph Farnleitner, Weboffice","version"=>"0.0.1");

    }
}

Et voilà, when we refresh Magmi we have a new option in the Datasources section allowing us to upload CSV files:

Figure 2: New option to upload files in Magmi: new section to upload CSV files in Magmi

Figure 1: GUI areas reflecting "magmi/plugins": Figure 1: GUI areas reflecting "magmi/plugins"


Finally maybe just one word about "Why the heck did you use an iframe here?!"

Now obviously there would be nicer ways like implementing an AJAX uploader or stuff like this, however, sometimes simple solutions can do as well. The most important thing is just to keep your hands away from the core files (no matter what framework your using). The main reason of why I went for an iframe is the fact that all plugins reside inside a <form> already - and as we know, nested <form>s are not a good idea. Also, this way you don't have to save the profile in order to see the newly uploaded CSVs. So, unless you want to go for a jQuery implementation this should be the easiest way to achieve your goal.

  • Vielen Dank @Christoph Farnleitner. This is basically exactly what I'm looking for. I can't believe this is not a readily available plugin. Just a couple of questions. When I used it I had to change the datasource back to CSV Datasource. Is this the intended operation to upload the file then change datasource before running magmi? Can it be set up to always save the file as import.csv and overwrite the existing import.csv? This way the profile can remain unedited. – Pragman Feb 12 '18 at 21:48
  • Your welcome, @ZUBU. Basically, yes, this procedure was somewhat indented since the if(file_exists($target_file)) would imply that the user has to upload a file that is not yet there (name-wise) -- so usually one wants to upload a file and then select it. If you don't want to go with that approach you could change line 15 to read $target_file = $target_dir."import.csv"; and remove the if-condition (line 19-22) [untested but should do]. Basically, you do not need to hit "save profile" either in this nor the other case as the file is uploaded inside the iframe and instantly available. – Christoph Farnleitner Feb 12 '18 at 22:12
  • You're * ... :o – Christoph Farnleitner Feb 12 '18 at 22:35
  • @ZUBU - did the modification work out for you? – Christoph Farnleitner Feb 17 '18 at 1:08
0

The easiest solution I see is to create some ftp resource that can be accesseed through URL (best would be to add also some kind of Bacic Authentication to be more safe) and using Remote import mode (see screenshot).

Other option would be to allowing uploading of CSV files through Magento media uploader : enter image description here

0

The easiest way would be to allow csv to be uploaded through the WYSIWYG uploader. To do this there's many answer on stackexchange that goes into detail, but in short you would add something like this to your config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <modules>
    <Pdf_WysiwygFiles>
      <version>1.0.0</version>
    </Pdf_WysiwygFiles>
  </modules>
  <adminhtml>
    <cms>
      <browser>
        <extensions>
          <allowed>
            <csv>1</csv>
          </allowed>
        </extensions>
      </browser>
    </cms>
  </adminhtml>
</config>

Then you can create a folder for csv_import , then symlink it to the right location where the files would be picked up for import / whatever you want to (I suspect in the var folder?)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.