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

Cart rules optimizer #110

Draft
wants to merge 3 commits 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
3 changes: 3 additions & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ services:
class: FOP\Console\Commands\About\AboutVersion
tags: [ console.command ]

fop.console.cart_rules.remove_outdated.command:
class: FOP\Console\Commands\CartRules\CartRulesRemoveOutdated
tags: [ console.command ]

imports:
- { resource: overriders.yml }
93 changes: 93 additions & 0 deletions src/Commands/CartRules/CartRulesRemoveOutdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?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\CartRules;

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\Style\SymfonyStyle;

class CartRulesRemoveOutdated extends Command
{
/**
* {@inheritdoc}
*/
protected function configure(): void
{
$this->setName('fop:cart-rules:remove-outdated')
->setDescription('Clear and optimize your Prestashop Cart rules')
->setHelp('Clear your Prestashop from useless row in cart rules table');

$this->addUsage('--from=[days] (30 for example)');
$this->addOption('days', null, InputOption::VALUE_OPTIONAL, '', 30);
}

/**
* {@inheritdoc}
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$days = (int) $input->getOption('days');

if (!is_int($days)) {
$io->error('Incorrect days parameter. Please type a number. Days given: ' . $days);

return 1;
}

$total = $this->clearCartRules($days, $output);

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

return 0;
}

/**
* {@inheritdoc}
*/
private function clearCartRules(int $days = 30, OutputInterface $output): int
{
$instance = Db::getInstance();

$date = new \DateTime('now');
$date->sub(new \DateInterval('P' . $days . 'D'));
$from_date = $date->format('Y-m-d');
$output->write('Delete all cart rules with finish or inactive before ' . $from_date);

$instance->delete('cart_rule_combination', 'id_cart_rule_1
IN (SELECT id_cart_rule FROM `' . _DB_PREFIX_ . 'cart_rule` WHERE `date_to` < "' . $from_date . '" OR active = 0 OR quantity = 0)
OR `id_cart_rule_2` IN (SELECT id_cart_rule FROM `' . _DB_PREFIX_ . 'cart_rule` WHERE `date_to` < "' . $from_date . '" OR active = 0 OR quantity = 0)');

$instance->delete('cart_rule_product_rule_group', 'id_cart_rule IN (SELECT id_cart_rule FROM `ps_cart_rule` WHERE `date_to` < "' . $from_date . '" OR active = 0 OR quantity = 0)');
$instance->delete('cart_rule_product_rule', 'NOT EXISTS (SELECT 1 FROM `' . _DB_PREFIX_ . 'cart_rule_product_rule_group` WHERE `' . _DB_PREFIX_ . 'cart_rule_product_rule`.`id_product_rule_group` = `' . _DB_PREFIX_ . 'cart_rule_product_rule_group`.`id_product_rule_group`)');
$instance->delete('cart_rule_product_rule_value', 'NOT EXISTS (SELECT 1 FROM `' . _DB_PREFIX_ . 'cart_rule_product_rule` WHERE `' . _DB_PREFIX_ . 'cart_rule_product_rule_value`.`id_product_rule` = `' . _DB_PREFIX_ . 'cart_rule_product_rule`.`id_product_rule`)');
$instance->delete('cart_rule', '`date_to` < "' . $from_date . '" OR active = 0 OR quantity = 0');

return $instance->affected_rows();
}
}