Skip to content

Commit

Permalink
Merge pull request PrestaShop#8402 from jocel1/optimize-cart-rule
Browse files Browse the repository at this point in the history
CO: optimize cart rule performances
  • Loading branch information
toutantic authored Nov 6, 2017
2 parents a1d52c5 + 59012c2 commit 5574dd5
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions classes/CartRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,34 @@ public static function getIdByCode($code)
);
}

/**
* Check if some cart rules exists today for the given customer
*
* @param int $idCustomer
*
* @return bool
*/
public static function haveCartRuleToday($idCustomer)
{
static $haveCartRuleToday = array();

if (!isset($haveCartRuleToday[$idCustomer])) {
$sql = '(SELECT 1 FROM `' . _DB_PREFIX_ . 'cart_rule` ' .
'WHERE date_to >= "' . date('Y-m-d 00:00:00') .
'" AND date_to <= "' . date('Y-m-d 23:59:59') .
'" AND `id_customer` IN (0,' . (int)$idCustomer . ') LIMIT 1)';

$sql .= 'UNION ALL (SELECT 1 FROM `' . _DB_PREFIX_ . 'cart_rule` ' .
'WHERE date_from >= "' . date('Y-m-d 00:00:00') .
'" AND date_from <= "' . date('Y-m-d 23:59:59') .
'" AND `id_customer` IN (0,' . (int)$idCustomer . ') LIMIT 1) LIMIT 1';

$haveCartRuleToday[$idCustomer] = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
}

return !empty($haveCartRuleToday[$idCustomer]);
}

/**
* Get CartRules for the given Customer
*
Expand All @@ -326,15 +354,17 @@ public static function getCustomerCartRules(
$highlight_only = false
) {

if (!CartRule::isFeatureActive()) {
if (!CartRule::isFeatureActive()
|| !CartRule::haveCartRuleToday($id_customer)
) {
return array();
}

$sql_part1 = '* FROM `' . _DB_PREFIX_ . 'cart_rule` cr
LEFT JOIN `' . _DB_PREFIX_ . 'cart_rule_lang` crl ON (cr.`id_cart_rule` = crl.`id_cart_rule` AND crl.`id_lang` = ' . (int) $id_lang . ')';

$sql_part2 = ' AND cr.date_from < "' . date('Y-m-d H:i:s') . '"
AND cr.date_to > "' . date('Y-m-d H:i:s') . '"
$sql_part2 = ' AND cr.date_from < "' . date('Y-m-d H:i:59') . '"
AND cr.date_to > "' . date('Y-m-d H:i:59') . '"
' . ($active ? 'AND cr.`active` = 1' : '') . '
' . ($inStock ? 'AND cr.`quantity` > 0' : '');

Expand Down

0 comments on commit 5574dd5

Please sign in to comment.