|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * For the full copyright and license information, please view the |
| 4 | + * docs/licenses/LICENSE.txt file that was distributed with this source code. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace PrestaShop\PrestaShop\Adapter\CartRule; |
| 10 | + |
| 11 | +use CartRule; |
| 12 | +use Db; |
| 13 | +use PrestaShop\PrestaShop\Core\FeatureFlag\FeatureFlagSettings; |
| 14 | +use PrestaShop\PrestaShop\Core\FeatureFlag\FeatureFlagStateCheckerInterface; |
| 15 | +use PrestaShopCollection; |
| 16 | + |
| 17 | +/** |
| 18 | + * Handles disabling of cart rules when their eligibility conditions are removed |
| 19 | + * (e.g. a customer or customer group is deleted). |
| 20 | + */ |
| 21 | +class CartRuleDisablerService |
| 22 | +{ |
| 23 | + public function __construct( |
| 24 | + private readonly FeatureFlagStateCheckerInterface $featureFlagStateChecker, |
| 25 | + ) { |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * On customer deletion: disable cart rules that were restricted to this customer. |
| 30 | + * Resets the customer restriction so the discount is no longer tied to the deleted customer, |
| 31 | + * and disables it so the merchant can review and re-enable it manually. |
| 32 | + * |
| 33 | + * @param int $customerId |
| 34 | + */ |
| 35 | + public function disableCartRulesThatHadCustomer(int $customerId): bool |
| 36 | + { |
| 37 | + if (!$this->featureFlagStateChecker->isEnabled(FeatureFlagSettings::FEATURE_FLAG_DISCOUNT)) { |
| 38 | + return true; |
| 39 | + } |
| 40 | + |
| 41 | + if (empty($customerId)) { |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + $cartRules = new PrestaShopCollection('CartRule'); |
| 46 | + $cartRules->where('id_customer', '=', $customerId); |
| 47 | + |
| 48 | + $result = true; |
| 49 | + foreach ($cartRules as $cartRule) { |
| 50 | + $cartRule->id_customer = 0; |
| 51 | + $cartRule->active = false; |
| 52 | + $result = $cartRule->update() && $result; |
| 53 | + } |
| 54 | + |
| 55 | + return $result; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * On group deletion: disable cart rules that had only this group as their restriction. |
| 60 | + * Clears the group restriction and disables the discount so the merchant can review it. |
| 61 | + * Must be called BEFORE the group rows are removed from the cart_rule_group table. |
| 62 | + * |
| 63 | + * @param int $groupId |
| 64 | + */ |
| 65 | + public function disableCartRulesThatHadOnlyGroup(int $groupId): bool |
| 66 | + { |
| 67 | + if (!$this->featureFlagStateChecker->isEnabled(FeatureFlagSettings::FEATURE_FLAG_DISCOUNT)) { |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + if (empty($groupId)) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + $prefix = _DB_PREFIX_; |
| 76 | + $db = Db::getInstance(); |
| 77 | + |
| 78 | + $cartRuleIds = $db->executeS( |
| 79 | + 'SELECT crg.`id_cart_rule` |
| 80 | + FROM `' . $prefix . 'cart_rule_group` crg |
| 81 | + INNER JOIN `' . $prefix . 'cart_rule` cr ON cr.`id_cart_rule` = crg.`id_cart_rule` AND cr.`group_restriction` = 1 |
| 82 | + WHERE crg.`id_group` = ' . (int) $groupId . ' |
| 83 | + AND crg.`id_cart_rule` IN ( |
| 84 | + SELECT `id_cart_rule` FROM `' . $prefix . 'cart_rule_group` GROUP BY `id_cart_rule` HAVING COUNT(*) = 1 |
| 85 | + )' |
| 86 | + ); |
| 87 | + |
| 88 | + if (empty($cartRuleIds)) { |
| 89 | + return true; |
| 90 | + } |
| 91 | + |
| 92 | + $result = true; |
| 93 | + foreach ($cartRuleIds as $row) { |
| 94 | + $cartRule = new CartRule((int) $row['id_cart_rule']); |
| 95 | + $cartRule->group_restriction = false; |
| 96 | + $cartRule->active = false; |
| 97 | + $result = $cartRule->update() && $result; |
| 98 | + } |
| 99 | + |
| 100 | + return $result; |
| 101 | + } |
| 102 | +} |
0 commit comments