Last active
April 18, 2025 10:45
-
-
Save MagePsycho/1425999a8a10dc87f06648ffa73aecd7 to your computer and use it in GitHub Desktop.
Magento2 Sandbox Script - http://www.blog.magepsycho.com/sandbox-script-quick-testing-magento2/
This file contains hidden or 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 | |
#################################################### | |
# EDIT | |
$rootMagentoDir = __DIR__; | |
#$csvFile = $rootMagentoDir . '/update_skus.csv'; | |
$logFile = $rootMagentoDir . '/var/log/cli-operation.log'; | |
$dryRun = 0; | |
#################################################### | |
ini_set('display_errors', 1); | |
ini_set('display_startup_errors', 1); | |
error_reporting(E_ALL); | |
// Capture warning / notice as exception | |
set_error_handler('mw_exceptions_error_handler'); | |
function mw_exceptions_error_handler($severity, $message, $filename, $lineno) | |
{ | |
if (error_reporting() == 0) { | |
return; | |
} | |
if (error_reporting() & $severity) { | |
throw new ErrorException($message, 0, $severity, $filename, $lineno); | |
} | |
} | |
function _log($data, $includeSep = false) | |
{ | |
global $logFile; | |
$fileName = $logFile; | |
if ($includeSep) { | |
$separator = str_repeat('=', 70); | |
file_put_contents($fileName, $separator . PHP_EOL, FILE_APPEND | LOCK_EX); | |
} | |
file_put_contents($fileName, $data . PHP_EOL, FILE_APPEND | LOCK_EX); | |
} | |
function _getConnection() | |
{ | |
global $obj; | |
$resource = $obj->get('Magento\Framework\App\ResourceConnection'); | |
return $resource->getConnection(); | |
} | |
function _getSkus() | |
{ | |
$connection = _getConnection(); | |
$sql = "SELECT sku, entity_id FROM catalog_product_entity WHERE created_in = 1;"; | |
return $connection->fetchPairs($sql); | |
} | |
function _readCsvRows($csvFile) | |
{ | |
$rows = []; | |
$fileHandle = fopen($csvFile, 'r'); | |
while(($row = fgetcsv($fileHandle, 0, ',', '"', '"')) !== false) { | |
$rows[] = $row; | |
} | |
fclose($fileHandle); | |
return $rows; | |
} | |
require $rootMagentoDir . '/app/bootstrap.php'; | |
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER); | |
$obj = $bootstrap->getObjectManager(); | |
$state = $obj->get('Magento\Framework\App\State'); | |
$state->setAreaCode('crontab'); | |
try { | |
$message = 'Success::Operation Completed.'; | |
#_log($message); | |
echo $message . PHP_EOL; | |
} catch (Exception $e) { | |
$message = 'Exception::' . $e->getMessage(); | |
#_log($message); | |
echo $message . PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment