Last active
January 25, 2023 10:26
-
-
Save acecconato/3e7294bd6b3230e02a6413f5d77f57a0 to your computer and use it in GitHub Desktop.
Useful scripts for Prestashop owners
This file contains 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 | |
/** | |
* @author - Mobiloweb - Anthony Cecconato | |
* @version 1.0 | |
* @cms Prestashop | |
* @ps_version 1.7+ | |
* | |
* Delete old users, without orders (+12month by default) by getting the /yoururl/cron/delete_old_users.php?s_key=SECRET url | |
*/ | |
if ($_GET['s_key'] !== 'SECRET') { | |
exit('Unauthorized'); | |
} | |
$countOnly = (bool)$_GET['count_only']; | |
require_once '../../config/config.inc.php'; // Be sure to have the good path | |
$rawCustomers = Customer::getCustomers(); | |
$count = 0; | |
foreach ($rawCustomers as $rawCustomer) { | |
$customer = new Customer((int)$rawCustomer['id_customer']); | |
$stats = $customer->getStats(); | |
$lastLoggedIn = ( ! $stats['last_visit']) ? null : strtotime($stats['last_visit']); | |
$creationDate = strtotime($customer->date_add); | |
$expTime = (new DateTime('now'))->modify('-12 months')->getTimestamp(); | |
if ($creationDate < $expTime) { | |
if ( ! $lastLoggedIn && $stats['nb_orders'] < 1) { | |
if ( ! $countOnly) { | |
$customer->delete(); | |
} | |
$count++; | |
} | |
if ($lastLoggedIn && $lastLoggedIn < $expTime && $stats['nb_orders'] < 1) { | |
if ( ! $countOnly) { | |
$customer->delete(); | |
} | |
$count++; | |
} | |
} | |
} | |
if ( ! $countOnly) { | |
$currDate = date("m-d-Y H:i:s"); | |
file_put_contents( | |
__DIR__ . '/history.log', | |
$currDate . ' - ' . 'Process ended: Deleted inactive users: ' . (string)$count . PHP_EOL, | |
FILE_APPEND | |
); | |
} | |
echo 'Process ended: Deleted or to delete inactive users: ' . (string)$count; |
This file contains 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 | |
/** | |
* Anthony Cecconato - Mobiloweb - January 2023 | |
* This script is used to merge the duplicated customers without loosing any orders, by getting the /yoururl/cron/fix_duplicated_users.php?s_key=SECRET url | |
*/ | |
require '../config/defines.inc.php'; | |
require '../config/config.inc.php'; | |
if ($_GET['s_key'] !== 'SECRET') { | |
header('HTTP/1.0 404 Not Found'); | |
exit(); | |
} | |
$duplicatedCustomers = array_filter(Customer::getCustomers(false), function ($customer) { | |
$email = (string)$customer['email'] ?? ''; | |
if ($customers = Customer::getCustomersByEmail($email)) { | |
if (count($customers) > 1) { | |
return true; | |
} | |
} | |
return false; | |
}); | |
$sortedArray = []; | |
foreach ($duplicatedCustomers as $duplicatedCustomer) { | |
if ( ! array_key_exists($duplicatedCustomer['email'], $sortedArray)) { | |
$sortedArray[$duplicatedCustomer['email']] = array_filter( | |
$duplicatedCustomers, | |
function ($dup) use ($duplicatedCustomer) { | |
if (strtolower($dup['email']) === strtolower($duplicatedCustomer['email'])) { | |
return true; | |
} | |
return false; | |
} | |
); | |
} | |
} | |
foreach ($sortedArray as $email => $duplications) { | |
$ids = array_map(function ($dup) { | |
return $dup['id_customer']; | |
}, $duplications); | |
$mainId = min($ids); | |
$orders = []; | |
foreach ($ids as $id) { | |
if (Order::getCustomerNbOrders($id)) { | |
$orders = array_merge( | |
$orders, | |
array_map(function ($order) { | |
return $order['id_order']; | |
}, Order::getCustomerOrders($id)) | |
); | |
} | |
} | |
foreach ($orders as $orderId) { | |
$order = new Order($orderId); | |
if (Validate::isLoadedObject($order)) { | |
$order->id_customer = $mainId; | |
$order->save(); | |
foreach ($ids as $id) { | |
if ($mainId === $id) { | |
continue; // skip | |
} | |
$customer = new Customer($id); | |
if (Validate::isLoadedObject($customer)) { | |
$customer->delete(); | |
} | |
} | |
} | |
} | |
} | |
echo 'END'; |
This file contains 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 | |
/** | |
* @author - Mobiloweb - Anthony Cecconato | |
* @version 1.0 | |
* @cms Prestashop | |
* @ps_version 1.7+ | |
* | |
* Simple crontab jobs to repair known issues for our websites | |
*/ | |
if ($_GET['s_key'] !== 'SECRET') { | |
exit('Unauthorized'); | |
} | |
require_once '../../config/config.inc.php'; | |
ini_set('display_errors', 'ON'); | |
function addLog($message) | |
{ | |
$currDate = date("m-d-Y H:i:s"); | |
file_put_contents('execution.log', $currDate.' - '.$message.PHP_EOL, FILE_APPEND); | |
echo $currDate.' - '.$message.'<br>'; | |
} | |
function repair_search_page_pack_sync_error() | |
{ | |
$query = " | |
UPDATE | |
"._DB_PREFIX_."product p | |
LEFT JOIN "._DB_PREFIX_."pack pa ON pa.id_product_item = p.id_product | |
SET | |
p.cache_is_pack = 0 | |
WHERE | |
p.cache_is_pack = 1 | |
AND pa.id_product_item IS NULL | |
"; | |
executeQuery($query); | |
} | |
function set_0_ean_to_null() | |
{ | |
$query = " | |
UPDATE | |
"._DB_PREFIX_."product p | |
SET | |
p.ean13 = NULL | |
WHERE | |
p.ean13 = '0' | |
"; | |
executeQuery($query); | |
} | |
function executeQuery($query) | |
{ | |
$db = Db::getInstance(); | |
$db->execute($query); | |
addLog(debug_backtrace()[1]['function'].' - '.$db->Affected_Rows().' row\'s affected'); | |
} | |
repair_search_page_pack_sync_error(); | |
set_0_ean_to_null(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment