File: //var/www/innodrive/migrations/search_fixer.php
<?php
/**
* @var $wpdb;
*
*/
//define('APPLICATION_ENV', 'development');
define( 'SHORTINIT', true );
define( 'BASE_PATH', __DIR__ . '/../public/' );
require(BASE_PATH . 'wp-load.php' );
require(BASE_PATH . 'wp-content/plugins/custom-search/custom-search.php');
require(BASE_PATH . 'wp-content/plugins/import/class/Product.php');
require(BASE_PATH . 'wp-content/plugins/import/class/Brand.php');
require(BASE_PATH . 'wp-content/plugins/import/class/Category.php');
$searchResults = $wpdb->get_results('SELECT ts.* FROM top_suggestions ts
WHERE ts.type = "products"');
$updated = $removed = 0;
$wpdb->query('DELETE FROM top_suggestions WHERE url IS NULL OR title IS NULL');
if (count($searchResults)) {
echo 'Current search results in products: ' . count($searchResults) . PHP_EOL;
foreach($searchResults as $row) {
if (!empty($row->title)) {
$sku = mb_strpos($row->title, ' ') ? mb_substr($row->title, 0, mb_strpos($row->title, ' ')) : $row->title;
$products = Product::search($sku, $row->lang);
if (!empty($products[0])) {
$wpdb->update('top_suggestions', [
'url' => $products[0]['url'],
], [
'id' => $row->id
]);
$updated++;
} else {
$wpdb->delete('top_suggestions', [
'id' => $row->id
]);
$removed++;
}
} else {
$wpdb->delete('top_suggestions', [
'id' => $row->id
]);
$removed++;
}
}
}
echo PHP_EOL;
echo 'Updated: ' . $updated . PHP_EOL;
echo 'Removed: ' . $removed . PHP_EOL;
echo PHP_EOL;
echo 'THE END' . PHP_EOL;