Opt out when subscription unchecked

This commit adds an action when customer unsubscribes to WooCommerce segment on checkout and opt outs Automate Woo customer.

[MAILPOET-4230]
This commit is contained in:
Brezo Cordero
2023-05-04 00:06:15 -05:00
committed by Aschepikov
parent 00e5dea5b4
commit c0ffcbac9b
4 changed files with 45 additions and 19 deletions

View File

@@ -32,7 +32,10 @@ class AutomateWooHooks {
class_exists('AutomateWoo\Customer') && method_exists('AutomateWoo\Customer', 'opt_out');
}
public function getAutomateWooCustomer(string $email): ?\AutomateWoo\Customer {
/**
* @return \AutomateWoo\Customer|false
*/
public function getAutomateWooCustomer(string $email) {
// AutomateWoo\Customer_Factory::get_by_email() returns false if customer is not found
// Second parameter is set to false to prevent creating new customer if not found
return \AutomateWoo\Customer_Factory::get_by_email($email, false);
@@ -43,18 +46,14 @@ class AutomateWooHooks {
return;
}
$this->wp->addAction(SubscriberEntity::HOOK_SUBSCRIBER_STATUS_CHANGED, [$this, 'maybeOptOutSubscriber'], 10, 1);
$this->wp->addAction('mailpoet_woocommerce_segment_unsubscribed', [$this, 'optOutSubscriber'], 10, 1);
}
public function maybeOptOutSubscriber(int $subscriberId): void {
public function optOutSubscriber($subscriber): void {
if (!$this->isAutomateWooActive() || !$this->areMethodsAvailable()) {
return;
}
$subscriber = $this->subscribersRepository->findOneById($subscriberId);
if (!$subscriber || !$subscriber->getEmail() || $subscriber->getStatus() !== SubscriberEntity::STATUS_UNSUBSCRIBED) {
return;
}
$automateWooCustomer = $this->getAutomateWooCustomer($subscriber->getEmail());
if (!$automateWooCustomer) {
return;
@@ -62,4 +61,13 @@ class AutomateWooHooks {
$automateWooCustomer->opt_out();
}
public function maybeOptOutSubscriber(int $subscriberId): void {
$subscriber = $this->subscribersRepository->findOneById($subscriberId);
if (!$subscriber || !$subscriber->getEmail() || $subscriber->getStatus() !== SubscriberEntity::STATUS_UNSUBSCRIBED) {
return;
}
$this->optOutSubscriber($subscriber);
}
}