Last active
January 26, 2020 12:50
-
-
Save kmddevdani/fd0c068e9bb36fec905ce259b44b13f5 to your computer and use it in GitHub Desktop.
Trying out product creation for Magento2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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