Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database cart optimizer #109

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,8 @@ services:
class: FOP\Console\Commands\ThemeResetLayout
tags:
- { name: console.command }

fop.console.optimize_cart.command:
class: FOP\Console\Commands\Optimize\CartOptimizer
tags:
- { name: console.command }
100 changes: 100 additions & 0 deletions src/Commands/Optimize/CartOptimizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* Copyright (c) Since 2020 Friends of Presta
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file docs/licenses/LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @author Friends of Presta <[email protected]>
* @copyright since 2020 Friends of Presta
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License ("AFL") v. 3.0
*/

namespace FOP\Console\Commands\Optimize;

use DateTime;
use Db;
use FOP\Console\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;

class CartOptimizer extends Command
{
private $from_date;
private $logs;

/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('fop:optimize:cart')
->setDescription('Clear and optimize your Prestashop')
->setHelp('Clear your Prestashop from useless row in cart table');

$this->addUsage('--from=[from-date] (format: Y-m-d, default: 1 month)');
$this->addOption('from-date', null, InputOption::VALUE_OPTIONAL);
}

/**
* {@inheritdoc}
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$helper = $this->getHelper('question');
$from_date = $input->getOption('from-date') ?? $helper->ask($input, $output, new Question('<question>Remove abandoned cart since ? Default 1 month, type date format "Y-m-d" to override.</question>'));

if (!$this->checkDate($from_date)) {
$io->error('Incorrect date parameter. Please type a date from format Y-m-d. Date given : ' . $from_date);
}

$this->clearCart();

if ($this->logs['cart'] === 0) {
$io->success('Cart already clean. No need more.');
} else {
$io->success('Cart clear successfully ! We delete ' . $this->logs['cart'] . ' cart(s) !');
}
}

/**
* {@inheritdoc}
*/
private function clearCart()
{
$instance = Db::getInstance();

$instance->delete('cart', 'id_cart NOT IN (SELECT id_cart FROM `'._DB_PREFIX_.'orders`) AND date_add < "' . pSQL($this->from_date) . '"');
$this->logs['cart'] = $instance->affected_rows();
}

/**
* @param $date
* @return bool
*/
private function checkDate($date)
{
if (is_null($date)) {
$this->from_date = date('Y-m-d', strtotime('-1 month'));
return true;
}

if ($date = DateTime::createFromFormat('Y-m-d', $date)) {
$this->from_date = $date->format('Y-m-d');
return true;
}

return false;
}
}