1

I needed to programatically export and import customer data, product data and order details. I've successfully created my exports. I'm having problems with my import.

I need to do this using an external PHP script, which is located at project root. I need to upload products, customer data, customer addresses from a (separate) CSV file.

How can I do this?

I have the ususal Magento attributes and few custom product attributes.

Please can someone help me out?

Here is what I got so far:

<?php

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';

ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);

$importProduct = getProductImport();

function getProductImport(){

    $bootstrap = Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();

    $state = $objectManager->get('Magento\Framework\App\State');
    $state->setAreaCode('frontend');

    $file = 'import/products.csv';
    $arrResult = array();
    $headers = false;
    $handle = fopen($file, "r");
    if (empty($handle) === false) {
        while (($data = fgetcsv($handle, 100000, ",")) !== FALSE) {
            if (!$headers) {
                $headers[] = $data;
            } else {
                $arrResult[] = $data;
            }
        }
        fclose($handle);
    }


    foreach( $arrResult as $importProduct ) {
        $product = $objectManager->create('\Magento\Catalog\Model\Product');

        $product->setWebsiteIds(array(1));

        $product->setAttributeSetId($product->getDefaultAttributeSetId());

        $product->setSku($importProduct[0]);
        $product->setName($importProduct[5]); 

        $product->setStockData(
            array(
                'use_config_manage_stock' => 0, 
                'manage_stock' => 1, // manage stock
                'min_sale_qty' => 1, // Shopping Cart Minimum Qty Allowed 
                'max_sale_qty' => 2, // Shopping Cart Maximum Qty Allowed
                'is_in_stock' => 1, // Stock Availability of product
                'qty' => 78
            )
        );
        $product->save();
        echo "Upload simple product id :: ".$product->getId()."<br>";

    }

}

?>
  • where will you stuck? – Amit Bera May 1 '18 at 6:15
  • @AmitBera I tried using same export script to do the imports, didn't work. I followed few Magento1 tutorials, but seems like they were unable to make to work with M2. so all and all, I'm stuck at the begining. very much stuck. please help me if you can. even a small pointer to right path is golden to me at this time – Joey May 1 '18 at 6:19
  • Please share your import script – Amit Bera May 1 '18 at 6:20
  • @AmitBera here it is, paste.ofcode.org/Mt5mwsAy5YpGU6wwnLZsmb – Joey May 1 '18 at 6:37
  • @AmitBera I've updated my question. please look at it and help me if you can – Joey May 1 '18 at 11:40

Your Answer

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

Browse other questions tagged or ask your own question.