Skip to content

Commit

Permalink
Merge branch 'feature/bstepien/GRINTEGRAT-3052' into 'master'
Browse files Browse the repository at this point in the history
Feature/bstepien/grintegrat 3052

See merge request integrations/magento!88
  • Loading branch information
Bartłomiej Stępień committed Apr 9, 2019
2 parents 59b1239 + 29d1428 commit 38bcba5
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
use Magento\Framework\ObjectManagerInterface;

/**
* Class SubscribeFromRegister
* Class CustomerSubscribed
* @package GetResponse\GetResponseIntegration\Observer
*/
class SubscribeFromRegister implements ObserverInterface
class CustomerSubscribed implements ObserverInterface
{
/** @var ObjectManagerInterface */
protected $_objectManager;
Expand Down Expand Up @@ -54,22 +54,35 @@ public function __construct(
$this->contactCustomFieldsCollectionFactory = $contactCustomFieldsCollectionFactory;
}

/**
* @param Observer $observer
* @return $this
*/
public function execute(Observer $observer)
{
$registrationSettings = $this->subscribeViaRegistrationService->getSettings();
try {

if (!$registrationSettings->isEnabled()) {
return $this;
}
if (!$observer->getEvent()->getSubscriber()->hasDataChanges()) {
return $this;
}

$registrationSettings = $this->subscribeViaRegistrationService->getSettings();

$customerData = $observer->getEvent()->getCustomer();
$subscriber = $this->repository->loadSubscriberByEmail($customerData->getEmail());
if (!$registrationSettings->isEnabled()) {
return $this;
}

if ($subscriber->isSubscribed()) {
$subscriber = $this->repository->loadSubscriberByEmail($observer->getEvent()->getSubscriber()->getSubscriberEmail());
$tete = $subscriber->isStatusChanged();
$test = $observer->getEvent()->getSubscriber()->isStatusChanged();

if (!$subscriber->isSubscribed()) {
return $this;
}

$customerData = $this->_objectManager->create('Magento\Customer\Model\Customer');
$customerData->setWebsiteId($subscriber->getStoreId());
$customerData->loadByEmail($subscriber->getSubscriberEmail());

if ($customerData->isEmpty()) {
return $this;
}

/** @var Customer $customer */
$customer = $this->repository->loadCustomer($customerData->getId());
Expand All @@ -80,19 +93,17 @@ public function execute(Observer $observer)
$registrationSettings->isUpdateCustomFieldsEnalbed()
);

try {
$this->contactService->addContact(
$customerData->getEmail(),
$customerData->getFirstname(),
$customerData->getLastname(),
$registrationSettings->getCampaignId(),
$registrationSettings->getCycleDay(),
$contactCustomFieldsCollection,
$registrationSettings->isUpdateCustomFieldsEnalbed()
);
} catch (ApiException $e) {
} catch (GetresponseApiException $e) {
}
$this->contactService->addContact(
$customerData->getEmail(),
$customerData->getFirstname(),
$customerData->getLastname(),
$registrationSettings->getCampaignId(),
$registrationSettings->getCycleDay(),
$contactCustomFieldsCollection,
$registrationSettings->isUpdateCustomFieldsEnalbed()
);
} catch (ApiException $e) {
} catch (GetresponseApiException $e) {
}

return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
use Magento\Framework\ObjectManagerInterface;

/**
* Class SubscribeFromOrder
* Class CustomerSubscribedFromOrder
* @package GetResponse\GetResponseIntegration\Observer
*/
class SubscribeFromOrder implements ObserverInterface
class CustomerSubscribedFromOrder implements ObserverInterface
{
/** @var ObjectManagerInterface */
protected $_objectManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use GetResponse\GetResponseIntegration\Domain\GetResponse\Contact\ContactCustomFieldsCollectionFactory;
use GetResponse\GetResponseIntegration\Domain\GetResponse\Contact\ContactService;
use GetResponse\GetResponseIntegration\Domain\GetResponse\SubscribeViaRegistration\SubscribeViaRegistrationService;
use GetResponse\GetResponseIntegration\Domain\Magento\NewsletterSettingsFactory;
use GetResponse\GetResponseIntegration\Domain\Magento\Repository;
use GrShareCode\Api\Exception\GetresponseApiException;
use Magento\Customer\Model\Customer;
Expand All @@ -13,10 +14,10 @@
use Magento\Framework\ObjectManagerInterface;

/**
* Class SubscribeUpdate
* Class SubscriberUnsubscribed
* @package GetResponse\GetResponseIntegration\Observer
*/
class SubscribeSettingsUpdate implements ObserverInterface
class SubscriberOrCustomerUnsubscribed implements ObserverInterface
{
/** @var ObjectManagerInterface */
protected $_objectManager;
Expand Down Expand Up @@ -56,43 +57,22 @@ public function __construct(
public function execute(Observer $observer)
{
try {

if (!$observer->getEvent()->getSubscriber()->hasDataChanges()) {
return $this;
}

$registrationSettings = $this->subscribeViaRegistrationService->getSettings();
$newsletterSettings = NewsletterSettingsFactory::createFromArray($this->repository->getNewsletterSettings());

if (!$registrationSettings->isEnabled()) {
if (!$registrationSettings->isEnabled() && !$newsletterSettings->isEnabled()) {
return $this;
}

$subscriber = $this->repository->loadSubscriberByEmail($observer->getEvent()->getSubscriber()->getSubscriberEmail());

if ($subscriber->isSubscribed()) {
$customerData = $this->_objectManager->create('Magento\Customer\Model\Customer');
$customerData->setWebsiteId($observer->getEvent()->getSubscriber()->getStoreId());
$customerData->loadByEmail($observer->getEvent()->getSubscriber()->getSubscriberEmail());

/** @var Customer $customer */
$customer = $this->repository->loadCustomer($customerData->getId());

$contactCustomFieldsCollection = $this->contactCustomFieldsCollectionFactory->createForCustomer(
$customer,
$this->subscribeViaRegistrationService->getCustomFieldMappingSettings(),
$registrationSettings->isUpdateCustomFieldsEnalbed()
);

$this->contactService->addContact(
$customerData->getEmail(),
$customerData->getFirstname(),
$customerData->getLastname(),
$registrationSettings->getCampaignId(),
$registrationSettings->getCycleDay(),
$contactCustomFieldsCollection,
$registrationSettings->isUpdateCustomFieldsEnalbed()
);
} else {
$this->contactService->removeContact(
$observer->getEvent()
->getSubscriber()
->getSubscriberEmail()
);
if (!$subscriber->isSubscribed()) {
$this->contactService->removeContact($subscriber->getSubscriberEmail());
}
} catch (ApiException $e) {
} catch (GetresponseApiException $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
use Magento\Newsletter\Model\Subscriber;

/**
* Class SubscribeFromNewsletter
* Class SubscriberSubscribed
* @package GetResponse\GetResponseIntegration\Observer
*/
class SubscribeFromNewsletter implements ObserverInterface
class SubscriberSubscribed implements ObserverInterface
{
/** @var ObjectManagerInterface */
protected $objectManager;
Expand Down Expand Up @@ -54,6 +54,10 @@ public function __construct(
*/
public function execute(EventObserver $observer)
{
if (!$observer->getEvent()->getSubscriber()->hasDataChanges()) {
return $this;
}

$newsletterSettings = NewsletterSettingsFactory::createFromArray(
$this->repository->getNewsletterSettings()
);
Expand All @@ -67,7 +71,8 @@ public function execute(EventObserver $observer)
/** @var Subscriber $subscriber */
$subscriber = $observer->getEvent()->getSubscriber();

if ($subscriber->getCustomerId() > 0) {
// This is use case only for subscribers who are not customers
if (0 !== $subscriber->getCustomerId()) {
return $this;
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "getresponse/magento2",
"description": "GetResponse Integration for Magento2",
"version": "20.3.9",
"version": "20.3.10",
"license": "OSL-3.0",
"minimum-stability": "stable",
"type": "magento2-module",
Expand Down
15 changes: 0 additions & 15 deletions etc/events.xml

This file was deleted.

16 changes: 7 additions & 9 deletions etc/frontend/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,28 @@

<!-- ONE PAGE SUCCESS -->
<event name="checkout_onepage_controller_success_action">
<observer name="new_subscriber_after_submit" instance="GetResponse\GetResponseIntegration\Observer\SubscribeFromOrder"/>
<observer name="new_subscriber_after_submit" instance="GetResponse\GetResponseIntegration\Observer\CustomerSubscribedFromOrder"/>
<observer name="create_ecommerce_order" instance="GetResponse\GetResponseIntegration\Observer\CreateOrderHandler"/>

</event>

<!-- NORMAL CHECKOUT SUCCESS -->
<event name="sales_order_save_after">
<observer name="new_subscriber_after_submit" instance="GetResponse\GetResponseIntegration\Observer\SubscribeFromOrder"/>
<observer name="new_subscriber_after_submit" instance="GetResponse\GetResponseIntegration\Observer\CustomerSubscribedFromOrder"/>
</event>

<!-- NORMAL CHECKOUT SUCCESS -->
<event name="sales_order_save_commit_after">
<observer name="update_ecommerce_order" instance="GetResponse\GetResponseIntegration\Observer\UpdateOrderHandler"/>
</event>

<!-- REGISTRATION SUCCESS -->
<event name="customer_register_success">
<observer name="new_subscriber_after_submit" instance="GetResponse\GetResponseIntegration\Observer\SubscribeFromRegister"/>
</event>

<event name="checkout_cart_save_after">
<observer name="add_cart_to_getresponse" instance="GetResponse\GetResponseIntegration\Observer\CreateCartHandler"/>
</event>
<event name="newsletter_subscriber_save_before">
<observer name="new_subscriber_from_newsletter" instance="GetResponse\GetResponseIntegration\Observer\SubscribeFromNewsletter"/>

<event name="newsletter_subscriber_save_commit_after">
<observer name="customer_subscribed" instance="GetResponse\GetResponseIntegration\Observer\CustomerSubscribed" />
<observer name="new_subscriber_from_newsletter" instance="GetResponse\GetResponseIntegration\Observer\SubscriberSubscribed"/>
<observer name="subscriber_or_customer_unsubscribed" instance="GetResponse\GetResponseIntegration\Observer\SubscriberOrCustomerUnsubscribed" />
</event>
</config>
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="GetResponse_GetResponseIntegration" setup_version="20.3.9">
<module name="GetResponse_GetResponseIntegration" setup_version="20.3.10">
<sequence>
<module name="Magento_Quote" />
</sequence>
Expand Down

0 comments on commit 38bcba5

Please sign in to comment.