Skip to content

Instantly share code, notes, and snippets.

@tschallacka
Created November 2, 2022 19:39
Show Gist options
  • Save tschallacka/f7fd2192fdf611b9f1b7d880a8305191 to your computer and use it in GitHub Desktop.
Save tschallacka/f7fd2192fdf611b9f1b7d880a8305191 to your computer and use it in GitHub Desktop.
Bigbridge importer example
<?php
declare(strict_types=1);
use Symfony\Component\Console\Output\OutputInterface;
use BigBridge\ProductImport\Api\Importer;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use BigBridge\ProductImport\Api\Data\SimpleProduct;
use BigBridge\ProductImport\Api\Data\SimpleProductFactory;
use BigBridge\ProductImport\Api\ImportConfigFactory;
use BigBridge\ProductImport\Api\ImporterFactory;
use Psr\Log\LoggerInterface;
class MySeed implements SeedInterface
{
protected ImportConfigFactory $import_config_factory;
protected ImporterFactory $importer_factory;
protected SimpleProductFactory $simple_product_factory;
protected AttributeHelper $attribute_helper;
protected ProductRepositoryInterface $productRepository;
protected LoggerInterface $logger;
protected $old_data = [];
protected $datastore;
const LABEL = 'verzendtype';
public function __construct(
ImportConfigFactory $import_config_factory,
ImporterFactory $importer_factory,
SimpleProductFactory $simple_product_factory,
AttributeHelper $attribute_helper,
ProductRepositoryInterface $productRepository,
LoggerInterface $logger
)
{
$this->import_config_factory = $import_config_factory;
$this->importer_factory = $importer_factory;
$this->simple_product_factory = $simple_product_factory;
$this->attribute_helper = $attribute_helper;
$this->productRepository = $productRepository;
$this->logger = $logger;
$this->datastore = $datastore = __DIR__.'/'.__FILE__ . '.datastore';
}
public function seed(OutputInterface $output)
{
if(file_exists($this->datastore)) {
throw new \Exception('Datastore already exists! Please run bigbridge:cypress:clean first.');
}
$importer = $this->importer_factory->createImporter($this->import_config_factory->create());
/** @var array $input */
$input = [
[
'sku' => '000001-PakketDienst',
'type' => 'PakketDienst',
]
];
foreach($input as $type) {
$this->importType($type, $importer);
}
$importer->flush();
file_put_contents($this->datastore, json_encode($this->old_data));
}
public function clean(OutputInterface $output)
{
if(!file_exists($this->datastore)) {
$output->writeln(__CLASS__ . ': Datastore does not exist. Nothing to clean.');
return;
}
$this->old_data = json_decode(file_get_contents($this->datastore), true);
$importer = $this->importer_factory->createImporter($this->import_config_factory->create());
/** @var array $input */
$input = // something
foreach($input as $type) {
$this->importType($type, $importer);
}
$importer->flush();
unlink($this->datastore);
}
/** Doing all the importing and setting the cleanup data.
in this example "existing" products are being modified.
Better would it be to create new objects and then delete them in cleanup.
*/
private function importType($type, Importer $importer)
{
$sku = $type['sku'];
$shipping_method = $type['type'];
$real = $this->productRepository->get($sku);
$old_value = $real->getData(self::LABEL);
$this->old_data[] = ['sku' => $sku, 'type' => $old_value];
/** @var SimpleProduct $product */
$product = $this->simple_product_factory->create(['sku' => $type['sku']]);
$global_product = $product->global();
try {
$global_product->setCustomAttribute(
self::LABEL,
$this->attribute_helper->getAttributeValueForLabelValue(self::LABEL, $shipping_method)
);
} catch (NoSuchEntityException $e) {
$this->logger->error("Error importing $sku with [".self::LABEL."]: ".$e->getMessage(), [$e]);
return;
}
try {
$importer->importSimpleProduct($product);
} catch (\Exception $e) {
$this->logger->error("Error importing $sku with [".self::LABEL."]:". $e->getMessage(), [$e]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment