From efe6b36f70d3d3da13a3c4b873ac947008aa73e6 Mon Sep 17 00:00:00 2001 From: Hlavtox Date: Fri, 26 Apr 2024 16:11:27 +0200 Subject: [PATCH] Speedup and simplify country restriction getting --- classes/CartRule.php | 49 +++++++++++++++++--------------------------- 1 file changed, 19 insertions(+), 30 deletions(-) diff --git a/classes/CartRule.php b/classes/CartRule.php index ea57d8f8b655b..9057ba40624a0 100644 --- a/classes/CartRule.php +++ b/classes/CartRule.php @@ -478,43 +478,32 @@ public static function getCustomerCartRules( } } } - $result_bak = $result; - $result = []; - $country_restriction = false; - foreach ($result_bak as $key => $cart_rule) { + + /* + * Now, we check the country restrictions on this cart rule. + * The rule is will be displayed, if the customer has at least one + * address with country in the allowed list. + * + * If the customer has no addresses, we won't display anything. + */ + foreach ($result as $key => $cart_rule) { if ($cart_rule['country_restriction']) { - $country_restriction = true; - $countries = Db::getInstance()->executeS( - ' - SELECT `id_country` - FROM `' . _DB_PREFIX_ . 'address` - WHERE `id_customer` = ' . (int) $id_customer . ' - AND `deleted` = 0' + $validAddressExists = Db::getInstance()->getValue(' + SELECT crc.id_cart_rule + FROM ' . _DB_PREFIX_ . 'cart_rule_country crc + INNER JOIN ' . _DB_PREFIX_ . 'address a + ON a.id_customer = ' . (int) $id_customer . ' AND + a.deleted = 0 AND + a.id_country = crc.id_country + WHERE crc.id_cart_rule = ' . (int) $cart_rule['id_cart_rule'] ); - if (is_array($countries) && count($countries)) { - foreach ($countries as $country) { - $id_cart_rule = (bool) Db::getInstance()->getValue(' - SELECT crc.id_cart_rule - FROM ' . _DB_PREFIX_ . 'cart_rule_country crc - WHERE crc.id_cart_rule = ' . (int) $cart_rule['id_cart_rule'] . ' - AND crc.id_country = ' . (int) $country['id_country']); - if ($id_cart_rule) { - $result[] = $result_bak[$key]; - - break; - } - } + if (empty($validAddressExists)) { + unset($result[$key]); } - } else { - $result[] = $result_bak[$key]; } } - if (!$country_restriction) { - $result = $result_bak; - } - return $result; }