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

Optimize cart rule performances #8402

Merged
merged 2 commits into from Nov 6, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 33 additions & 3 deletions classes/CartRule.php
Expand Up @@ -294,6 +294,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)
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this method you only check if cart rule starts or ends today
not if it is valid today
Is there a reason for this restriction ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to use the most generic query to make it easily cacheable

static $haveCartRuleToday = array();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomlev, something to say regarding that line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is not too annoying for the tests since we just have to change the idCustomer


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 @@ -319,15 +347,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