File: //var/www/innodrive/migrations/import_power.php
<?php
if( php_sapi_name() !== 'cli' ) {
die("Meant to be run from command line");
}
if (!empty($argv[1])) {
define('APPLICATION_ENV', $argv[1]);
} else {
define('APPLICATION_ENV', 'production');
}
define( 'BASE_PATH', __DIR__ .'/../public/' );
define('WP_USE_THEMES', false);
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header, $wpdb;
require(BASE_PATH . 'wp-load.php');
require(BASE_PATH . 'wp-content/plugins/import/libs/SimpleXLSX.php');
require(BASE_PATH . 'wp-content/plugins/import/class/Product.php');
$file = BASE_PATH . '../migrations/motors_power.xlsx';
echo $file . PHP_EOL;
$done = $error = 0;
if ( $xlsx = SimpleXLSX::parse($file) ) {
$sheets = $xlsx->sheetNames();
foreach ( $sheets as $sheetId => $sheetName ) {
$rows = $xlsx->rows($sheetId);
foreach($rows as $row => $rowData) {
if ($row == 0) {
continue;
}
if (!empty($rowData[1]) && !empty($rowData[0])) {
$sku = $rowData[2];
$power = (float)$rowData[60];
$product = Product::getBySku($sku);
if ($product) {
$wpdb->update('params_motor', [
'power' => $power
], [
'product_id' => $product->id
]);
Product::updateMeta($product->id, 'power', $power);
echo ' - ' . $sku . ' - обновлено (' . $power . ')' . PHP_EOL;
$done++;
} else {
echo ' - ' . $sku . ' - не найден' . PHP_EOL;
$error++;
}
}
}
}
}
echo 'Обновлено: ' . $done . PHP_EOL;
echo 'Не найдено: ' . $error . PHP_EOL;