mirror of
https://github.com/foomo/Foomo.Magento2.git
synced 2025-10-16 12:35:34 +00:00
initial commit
This commit is contained in:
parent
5b3fd04c94
commit
ffa154d3bd
@ -3,23 +3,37 @@
|
||||
*/
|
||||
|
||||
namespace Foomo\Magento2;
|
||||
|
||||
use Magento\Framework\App\Bootstrap;
|
||||
|
||||
require \Foomo\Magento2\Module::getModuleConfig()->magentoRootFolder . '/app/bootstrap.php';
|
||||
|
||||
|
||||
class Boostrap
|
||||
{
|
||||
private static $bootsrap;
|
||||
private static $bootstrap;
|
||||
|
||||
private static $appState;
|
||||
|
||||
|
||||
/**
|
||||
* @return Bootstrap
|
||||
*/
|
||||
public static function bootstrap() {
|
||||
if (!class_exists('\Magento\Framework\App\Bootstrap')) {
|
||||
return self::init();
|
||||
}
|
||||
return self::$bootstrap;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return \Magento\Framework\App\Bootstrap
|
||||
*/
|
||||
public static function init() {
|
||||
private static function init()
|
||||
{
|
||||
ini_set('max_execution_time', 30 * 60);
|
||||
ini_set('memory_limit', '2G');
|
||||
require \Foomo\Magento2\Module::getConf()->magentoRootFolder . '/app/bootstrap.php';
|
||||
|
||||
if (is_null(self::$bootstrap)) {
|
||||
//handle dockerized setup
|
||||
@ -28,6 +42,7 @@ class Boostrap
|
||||
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
|
||||
$_SERVER['HTTPS'] = 'on';
|
||||
}
|
||||
// add bootstrap
|
||||
|
||||
//create the bootstrap object
|
||||
self::$bootstrap = Bootstrap::create(BP, $_SERVER);
|
||||
@ -35,7 +50,48 @@ class Boostrap
|
||||
$state = $obj->get('Magento\Framework\App\State');
|
||||
$state->setAreaCode('frontend');
|
||||
}
|
||||
return self::$bootsrap;
|
||||
return self::$bootstrap;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Magento\Store\Api\Data\StoreInterface[]
|
||||
*/
|
||||
public static function getStores() {
|
||||
if (!class_exists('\Magento\Framework\App\Bootstrap')) {
|
||||
self::init();
|
||||
}
|
||||
/* @var \Magento\Store\Model\StoreManagerInterface $storeManager */
|
||||
$storeManager = \Magento\Framework\App\ObjectManager::getInstance()->get('\Magento\Store\Model\StoreManagerInterface');
|
||||
return $storeManager->getStores();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return \Magento\Store\Api\Data\WebsiteExtensionInterface
|
||||
*/
|
||||
public static function getWebsites() {
|
||||
if (!class_exists('\Magento\Framework\App\Bootstrap')) {
|
||||
self::init();
|
||||
}
|
||||
/* @var \Magento\Store\Model\StoreManagerInterface $storeManager */
|
||||
$storeManager = \Magento\Framework\App\ObjectManager::getInstance()->get('\Magento\Store\Model\StoreManagerInterface');
|
||||
return $storeManager->getWebsites();
|
||||
}
|
||||
|
||||
public static function getStoreCodes() {
|
||||
$ret = [];
|
||||
foreach (self::getStores() as $store) {
|
||||
$ret[] = $store->getCode();
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
|
||||
public static function getWebsiteCodes() {
|
||||
$ret = [];
|
||||
foreach (self::getWebsites() as $website) {
|
||||
$ret[] = $website->getCode();
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
92
lib/Foomo/Magento2/Import.php
Normal file
92
lib/Foomo/Magento2/Import.php
Normal file
@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace Foomo\Magento2;
|
||||
|
||||
use BigBridge\ProductImport\Api\Data\SimpleProduct;
|
||||
use BigBridge\ProductImport\Api\ImportConfig;
|
||||
use BigBridge\ProductImport\Api\ImporterFactory;
|
||||
use Kennys\Services\Log;
|
||||
use Magento\Framework\App\ObjectManager;
|
||||
|
||||
class Import
|
||||
{
|
||||
/**
|
||||
* @var \BigBridge\ProductImport\Api\Importer
|
||||
*/
|
||||
private $importer = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->importer = self::init();
|
||||
}
|
||||
|
||||
|
||||
public function commit() {
|
||||
//process an remaining in the pipeline
|
||||
$this->importer->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \BigBridge\ProductImport\Api\Data\Product $product
|
||||
*/
|
||||
public function import($product) {
|
||||
$this->importer->importSimpleProduct($product);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \BigBridge\ProductImport\Api\Data\ConfigurableProduct $product
|
||||
*/
|
||||
public function importConfigurableProduct($product) {
|
||||
$this->importer->importConfigurableProduct($product);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
private static function init() {
|
||||
\Foomo\Magento2\Boostrap::bootstrap();
|
||||
$config = self::getImporterConfig();
|
||||
// a callback function to postprocess imported products \BigBridge\ProductImport\Api\Data\Product
|
||||
$config->resultCallback = function( $product) use (&$log) {
|
||||
|
||||
/** @var \BigBridge\ProductImport\Api\Data\Product $product **/
|
||||
if ($product->isOk()) {
|
||||
$log .= $product->getSku() . ' magento upsert';
|
||||
} else {
|
||||
$log .= sprintf("%s: failed! error = %s\n", $product->getSku(), implode('; ', $product->getErrors()));
|
||||
}
|
||||
};
|
||||
|
||||
$factory = ObjectManager::getInstance()->get(ImporterFactory::class);
|
||||
return $factory->createImporter(self::getImporterConfig());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return \BigBridge\ProductImport\Api\ImportConfig
|
||||
*/
|
||||
private static function getImporterConfig() {
|
||||
$config = new ImportConfig();
|
||||
$config->autoCreateCategories = true;
|
||||
$config->autoCreateOptionAttributes = ['size'];
|
||||
$config->batchSize = 5000;
|
||||
$config->duplicateUrlKeyStrategy = ImportConfig::DUPLICATE_KEY_STRATEGY_ADD_SERIAL;
|
||||
|
||||
$config->resultCallback = function($product) use (&$log) {
|
||||
|
||||
if ($product->isOk()) {
|
||||
$log = sprintf("%s: success! sku = %s, id = %s\n", $product->lineNumber, $product->getSku(), $product->id);
|
||||
} else {
|
||||
$log = sprintf("%s: failed! error = %s\n", $product->lineNumber, implode('; ', $product->getErrors()));
|
||||
\Foomo\Utils::appendToPhpErrorLog('**************' . $log . PHP_EOL);
|
||||
}
|
||||
|
||||
};
|
||||
return $config;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
37
lib/Foomo/Magento2/Indexer.php
Normal file
37
lib/Foomo/Magento2/Indexer.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
/*
|
||||
*/
|
||||
|
||||
namespace Foomo\Magento2;
|
||||
|
||||
class Indexer
|
||||
{
|
||||
|
||||
public static function reindex($indexerIds = [])
|
||||
{
|
||||
$bootstrap = Boostrap::bootstrap();
|
||||
$indexerFactory = $bootstrap->getObjectManager()->get('Magento\Indexer\Model\IndexerFactory');
|
||||
|
||||
if (empty($indexerIds)) {
|
||||
$indexerIds = array(
|
||||
'catalog_category_product',
|
||||
'catalog_product_category',
|
||||
'catalog_product_price',
|
||||
'catalog_product_attribute',
|
||||
'cataloginventory_stock',
|
||||
'catalogrule_product',
|
||||
'catalogsearch_fulltext',
|
||||
);
|
||||
}
|
||||
|
||||
foreach ($indexerIds as $indexerId) {
|
||||
|
||||
$indexer = $indexerFactory->create();
|
||||
$indexer->load($indexerId);
|
||||
$indexer->reindexAll();
|
||||
\Foomo\Utils::appendToPhpErrorLog('REINDEX: ' . $indexerId . ' done.' . PHP_EOL);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -80,7 +80,7 @@ class Module extends \Foomo\Modules\ModuleBase
|
||||
/**
|
||||
* @return DomainConfig
|
||||
*/
|
||||
public static function getModuleConfig()
|
||||
public static function getConf()
|
||||
{
|
||||
return \Foomo\Config::getConf(self::NAME, DomainConfig::NAME);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user