Skip to content

Instantly share code, notes, and snippets.

@kmddevdani
Last active January 26, 2020 12:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmddevdani/fd0c068e9bb36fec905ce259b44b13f5 to your computer and use it in GitHub Desktop.
Save kmddevdani/fd0c068e9bb36fec905ce259b44b13f5 to your computer and use it in GitHub Desktop.
Trying out product creation for Magento2
<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$start = microtime(true);
echo 'creating repository ...' ;
$productRepository = $objectManager->create('\Magento\Catalog\Api\ProductRepositoryInterface');
echo getDuration() . PHP_EOL . 'creating factory ...' ;
$productFactory = $objectManager->create('\Magento\Catalog\Api\Data\ProductInterfaceFactory');
echo getDuration() . PHP_EOL . 'creating product ...' ;
$product = $productFactory->create();
// We should set required attribute data for product then save it once
// then after that we can use the save attributes for the reset
echo getDuration() . PHP_EOL . 'initialising product ...';
initialiseProductDataForCreateMode($product);
echo getDuration() . PHP_EOL . 'saving product...' ;
$product->save();
echo getDuration() . PHP_EOL;
function initialiseProductDataForCreateMode($product) {
$prodNameSku = 'PERFTEST' . rand(0, 999999);
$product->setName($prodNameSku);
$product->setSku($prodNameSku);
}
function getDuration() {
global $start;
return microtime(true) - $start;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment