From 2a84e5c5c63410b95f65a47704766fd00e21bf19 Mon Sep 17 00:00:00 2001 From: Codencode Date: Wed, 26 Aug 2026 07:32:48 +0200 Subject: [PATCH] Fix newsletter subscription cleanup on customer update --- ps_emailsubscription.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/ps_emailsubscription.php b/ps_emailsubscription.php index c427202..c22e5d2 100644 --- a/ps_emailsubscription.php +++ b/ps_emailsubscription.php @@ -1007,14 +1007,29 @@ public function hookActionCustomerAccountAdd($params) public function hookActionObjectCustomerUpdateBefore($params) { + // Reload the customer to get the newsletter state currently persisted in the database. + // $params['object'] already contains the new value that is about to be saved. + // Comparing both states allows us to detect an actual subscription transition (0 -> 1) + // and avoid removing an existing email subscription during unrelated customer updates. $customer = new Customer($params['object']->id); $this->_origin_newsletter = (int) $customer->newsletter; - Db::getInstance()->execute('DELETE FROM ' . _DB_PREFIX_ . 'emailsubscription WHERE id_shop=' . (int) $params['object']->id_shop . ' AND email=\'' . pSQL($params['object']->email) . "'"); + // Only handle a real newsletter subscription transition (0 -> 1). + // Existing email subscriptions must be preserved for unrelated customer updates. + if ($this->_origin_newsletter || !$params['object']->newsletter) { + return; + } + + Db::getInstance()->execute( + 'DELETE FROM ' . _DB_PREFIX_ . 'emailsubscription + WHERE id_shop=' . (int) $params['object']->id_shop . ' + AND email=\'' . pSQL($params['object']->email) . "'" + ); } public function hookActionCustomerAccountUpdate($params) { + // Send newsletter-related emails only when the customer has just subscribed (0 -> 1). if ($this->_origin_newsletter || !$params['customer']->newsletter) { return; }