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:
committed by
Aschepikov
parent
00e5dea5b4
commit
c0ffcbac9b
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -223,6 +223,7 @@ class Subscription {
|
||||
$signupConfirmation = $this->settings->get('signup_confirmation');
|
||||
|
||||
if (!$checkoutOptin) {
|
||||
$this->wp->doAction('mailpoet_woocommerce_segment_unsubscribed', $subscriber);
|
||||
// Opt-in is disabled or checkbox is unchecked
|
||||
$this->subscriberSegmentRepository->unsubscribeFromSegments($subscriber, [$wcSegment]);
|
||||
|
||||
|
@ -77,4 +77,20 @@ class WooCheckoutAutomateWooSubscriptionsCest {
|
||||
$i->see($customerEmail, '.automatewoo-content');
|
||||
$i->seeConfirmationEmailWasReceived();
|
||||
}
|
||||
|
||||
public function checkoutOptInCheckedAndUnchecked(\AcceptanceTester $i) {
|
||||
$this->settingsFactory->withWooCommerceCheckoutOptinEnabled();
|
||||
$this->settingsFactory->withConfirmationEmailEnabled();
|
||||
$customerEmail = 'woo_guest_check@example.com';
|
||||
$i->orderProductWithRegistration($this->product, $customerEmail, true);
|
||||
$i->login();
|
||||
$i->checkSubscriberStatusAndLists($customerEmail, SubscriberEntity::STATUS_UNCONFIRMED, ['WooCommerce Customers']);
|
||||
$i->amOnPage('/wp-admin/admin.php?page=automatewoo-opt-ins');
|
||||
$i->see($customerEmail, '.automatewoo-content');
|
||||
$i->logout();
|
||||
$i->orderProductWithoutRegistration($this->product, $customerEmail, false);
|
||||
$i->login();
|
||||
$i->amOnPage('/wp-admin/admin.php?page=automatewoo-opt-ins');
|
||||
$i->dontSee($customerEmail, '.automatewoo-content');
|
||||
}
|
||||
}
|
||||
|
@ -41,13 +41,16 @@ class AutomateWooHooksTest extends \MailPoetTest {
|
||||
}
|
||||
return false;
|
||||
},
|
||||
'addAction' => function($name, $callback) {
|
||||
expect($name)->equals(SubscriberEntity::HOOK_SUBSCRIBER_STATUS_CHANGED);
|
||||
},
|
||||
'addAction' => Expected::exactly(2),
|
||||
]);
|
||||
$subscribersRepository = $this->createMock(SubscribersRepository::class);
|
||||
$automateWooHooks = new AutomateWooHooks($subscribersRepository, $wp);
|
||||
$automateWooHooks->setup();
|
||||
|
||||
$automateWooHooksPartialMock = $this->getMockBuilder(AutomateWooHooks::class)
|
||||
->setConstructorArgs([$this->subscribersRepository, $wp])
|
||||
->onlyMethods(['areMethodsAvailable'])
|
||||
->getMock();
|
||||
$automateWooHooksPartialMock->expects($this->once())->method('areMethodsAvailable')->willReturn(true);
|
||||
|
||||
$automateWooHooksPartialMock->setup();
|
||||
}
|
||||
|
||||
public function testOptsOutUnsubscribedSubscriber() {
|
||||
@ -58,12 +61,10 @@ class AutomateWooHooksTest extends \MailPoetTest {
|
||||
|
||||
$automateWooHooksPartialMock = $this->getMockBuilder(AutomateWooHooks::class)
|
||||
->setConstructorArgs([$this->subscribersRepository, $this->wp])
|
||||
->onlyMethods(['getAutomateWooCustomer'])
|
||||
->onlyMethods(['optOutSubscriber'])
|
||||
->getMock();
|
||||
|
||||
$automateWooCustomer = $this->make(new \AutomateWoo\Customer, ['opt_out' => Expected::once(function() {
|
||||
})]);
|
||||
$automateWooHooksPartialMock->expects($this->once())->method('getAutomateWooCustomer')->willReturn($automateWooCustomer);
|
||||
$automateWooHooksPartialMock->expects($this->once())->method('optOutSubscriber');
|
||||
|
||||
$automateWooHooksPartialMock->maybeOptOutSubscriber((int)$unsubscribedSubscriber->getId());
|
||||
}
|
||||
@ -76,9 +77,9 @@ class AutomateWooHooksTest extends \MailPoetTest {
|
||||
|
||||
$automateWooHooksPartialMock = $this->getMockBuilder(AutomateWooHooks::class)
|
||||
->setConstructorArgs([$this->subscribersRepository, $this->wp])
|
||||
->onlyMethods(['getAutomateWooCustomer'])
|
||||
->onlyMethods(['optOutSubscriber'])
|
||||
->getMock();
|
||||
$automateWooHooksPartialMock->expects($this->never())->method('getAutomateWooCustomer');
|
||||
$automateWooHooksPartialMock->expects($this->never())->method('optOutSubscriber');
|
||||
|
||||
$automateWooHooksPartialMock->maybeOptOutSubscriber((int)$subscribedSubscriber->getId());
|
||||
}
|
||||
|
Reference in New Issue
Block a user