Skip to content

Instantly share code, notes, and snippets.

@MagePsycho
Last active September 9, 2023 11:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save MagePsycho/1425999a8a10dc87f06648ffa73aecd7 to your computer and use it in GitHub Desktop.
Save MagePsycho/1425999a8a10dc87f06648ffa73aecd7 to your computer and use it in GitHub Desktop.
<?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('sqli_exceptions_error_handler');
function sqli_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