PHP code example of lsv / magento2-magmi-dump

1. Go to this page and download the library: Download lsv/magento2-magmi-dump library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lsv / magento2-magmi-dump example snippets


use Lsv\Datapump\Configuration;
use Lsv\Datapump\ItemHolder;
use Lsv\Datapump\Logger;
use Lsv\Datapump\Product\ConfigurableProduct;use Monolog\Handler\StreamHandler;
use Lsv\Datapump\Product\SimpleProduct;use Symfony\Component\Console\Output\NullOutput;

// First we need to create a logger, due to Magmi is using a non standardized method, we need to make a little of a work around
$stream = __DIR__ . '/logfile.log';
$monologHandler = new StreamHandler($stream);
$monolog = new Monolog\Logger('default', [$monologHandler]);
$logger = new Logger($monolog);

// Next we need a configuration
$configuration = new Configuration(
    'path/to/magento/root', // Path to the root of your magento installation
    'databse name', // The name of the database
    'database host', // The host of the database
    'database username', // The username to the database
    'database password' // The password to the database
);

// Now we need a instance of Magmi

// Now we can create our ItemHolder which will hold the product we gonna import
$magmi = \Magmi_DataPumpFactory::getDataPumpInstance('productimport');

// If you want to have progressbar on the import, you can change $output to your OutputInterface 
$output = new NullOutput();

// As magmi have troubles doing indexing on the fly, I have opted to turn it off, you can turn it with a true
$useMagmiIndexer = false;

$holder = new ItemHolder($configuration, $logger, $magmi, $output, $useMagmiIndexer);

// Now we can start by adding products to the item holder
$simpleProduct = (new SimpleProduct())
    ->setName('Simple product')
    ->setSku('sku')
    ->setDescription('Product description')
    ->setPrice(15.99)
    ->setTaxClass('tax name')
    ->setQuantity(10);

// This is the minimum data able
$simple2 = new SimpleProduct();
$simple2->setName('simple2');
$simple2->setSku('simple2');
$simple2->setDescription('description');
$simple2->setPrice(13.99);
$simple2->setTaxClass('tax name');
$simple2->set('color', 'green');
$simple2->set('size', 'M');
$configurable->addProduct($simple2);

// If you already have created the simple products, you can also add them to the configurable product by using
$configurable->addSimpleSku('already-created-sku-1');
$configurable->addSimpleSku('already-created-sku-2');

// And lets add our configurable product to our itemHolder
$holder->addProduct($configurable);
// Only the configurable product should be added to the itemHolder as the simple products will automatically be imported and checked for missing attributes