Comments (3)
Hello JeanClaude,
This example is just a little something to get you started. Modify it in any way you need. This specific example shows a custom class that takes care of importing some products via a CSV file. The method processFile
is the main entry to import the file. If you have an array of data instead of a csv file, call importData
directly.
If the class is enough for you, you can strip out the rest of the code, but if what you need is a small self-sufficient command line script that loads Magento as well, then keep the bootstrap and application startup code. It loads a file called "abc.csv" that you would need to create. The file contains a name and a price. The class adds some necessary attributes to create a minimal product.
The example also makes the distinction between global attribute values and store view specific values; I hope this is clear. And as you can see, you can make config settings by modifying the $config object.
<?php
use BigBridge\ProductImport\Api\Data\Product;
use BigBridge\ProductImport\Api\Data\ProductStoreView;
use BigBridge\ProductImport\Api\Data\SimpleProduct;
use BigBridge\ProductImport\Api\ImportConfig;
use BigBridge\ProductImport\Api\ImporterFactory;
// =============================================================
// Example CSV file: abc.csv
//
// sku,name,price
// apple1,some apple,1.24
// banana4,this banana,0.75
// =============================================================
// =============================================================
// Bootstrap Magento
// =============================================================
$bootstrap = getcwd() . '/app/bootstrap.php';
if (!file_exists($bootstrap)) {
die("Execute this command from the root of a shop.\n");
}
require_once $bootstrap;
\Magento\Framework\App\Bootstrap::create(getcwd(), []);
// =============================================================
// Startup
// =============================================================
/** @var MyProductImport $import */
$import = \Magento\Framework\App\ObjectManager::getInstance()->get(MyProductImport::class);
$results = $import->processFile("abc.csv");
print_r($results);
// =============================================================
// A custom import class
// =============================================================
class MyProductImport
{
const HEADERS = ['sku', 'name', 'price'];
/** @var ImporterFactory */
protected $importerFactory;
public function __construct(
ImporterFactory $importerFactory
)
{
$this->importerFactory = $importerFactory;
}
public function processFile(string $filePath): array
{
$result = [];
list($productLabels, $messages) = $this->readFile($filePath);
if (!empty($messages)) {
return $result;
}
return $this->importData($productLabels);
}
protected function readFile(string $file): array
{
$messages = [];
$data = [];
$lines = file($file);
$header = array_shift($lines);
if (strpos($header, "\t") !== false) {
$sep = "\t";
} elseif (strpos($header, ",") !== false) {
$sep = ',';
} elseif (strpos($header, ";") !== false) {
$sep = ';';
} else {
$messages[] = "Could not detect column separator in first line";
goto end;
}
$headers = array_map('strtolower', array_map('trim', str_getcsv($header, $sep)));
if ($headers !== self::HEADERS) {
$messages[] = "The header of the CSV must be '" . implode(',', self::HEADERS) . "'";
goto end;
}
$i = 1;
foreach ($lines as $line) {
$i++;
$fields = array_map('trim', str_getcsv($line, $sep));
if ($fields === ['']) {
continue;
}
if (count($fields) !== count(self::HEADERS)) {
$messages[] = "Line $i does not have the right number of columns";
goto end;
}
$data[] = $fields;
}
end:
return [$data, $messages];
}
public function importData(array $data): array
{
$messages = [];
$result = [];
$productIds = [];
$config = new ImportConfig();
$config->emptyNonTextValueStrategy = ImportConfig::EMPTY_NONTEXTUAL_VALUE_STRATEGY_REMOVE;
$config->resultCallback = function(Product $product) use (&$messages, &$result, &$productIds) {
if (!$product->isOk()) {
$messages[] = sprintf("Line %s, sku = %s: %s",
$product->lineNumber, $product->getSku(), implode('; ', $product->getErrors()));
} else {
$result[] = $product->getSku();
$productIds[] = $product->id;
}
};
try {
$importer = $this->importerFactory->createImporter($config);
foreach ($data as $i => $datum) {
$sku = $datum[0];
$name = $datum[1];
$price = $datum[2];
$product = new SimpleProduct($sku);
$product->setAttributeSetByName("Default");
$product->addCategoriesByGlobalName(['Default Category/Desks', 'Default Category/Chairs', 'Default Category/Boards']);
$product->setWebsitesByCode(['base']);
// global scope data
$global = $product->global();
$global->setName($name);
$global->setPrice($price);
$global->setVisibility(ProductStoreView::VISIBILITY_BOTH);
$global->setStatus(ProductStoreView::STATUS_DISABLED);
$global->setTaxClassName("Taxable Goods");
$global->generateUrlKey();
// store view specific data (for example, store view code = nl)
$dutch = $product->storeView('nl');
$dutch->setName($name . ' met tulpen');
$dutch->setPrice($price + 1.25);
// stock data
$stockItem = $product->defaultStockItem();
$stockItem->setQty('100');
$stockItem->setIsInStock(true);
$product->lineNumber = $i + 2;
$importer->importSimpleProduct($product);
}
// process any remaining products in the pipeline
$importer->flush();
} catch (Throwable $throwable) {
// errors that were not foreseen by the importer (and should not occur)
$messages[] = $throwable->getMessage();
}
return ['messages' => $messages, 'skus' => $result, 'ids' => $productIds];
}
}
from product-import.
Hello @garfix,
That's great.
It works perfectly.
Greetings
from product-import.
I am working on an example. May take a few days. :)
from product-import.
Related Issues (20)
- website_codes restapi HOT 4
- Restapi update url issue HOT 15
- Category flat url structure HOT 9
- Image import creates hardlinks HOT 9
- Please provide example of how to use post method with webapi? HOT 11
- Saving product on storeview scope overwrites storeview attribute values with global values HOT 4
- How to set salable quantity ? HOT 5
- Export category attribute values HOT 2
- Trying to get property 'path' of non-object in Model/Resource/Storage/UrlRewriteStorage.php on line 376 HOT 1
- How to set out of stock threshold for backorders ? HOT 4
- Importing library fails after clearing all products HOT 1
- The setSelectAttribute method does not fill swatch input on Text Swatch HOT 3
- Module shouldn't rely on magento2-base package to exist HOT 3
- Fetching Magento configurations isn't working as expected HOT 3
- Generated Url key already exists HOT 1
- Clear Cache after change per product ID HOT 1
- PHP 8 Compatibility HOT 1
- Choose Affected Attribute Set prompt when saving configurable product
- Can't set is_in_stock = 0 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from product-import.