klaviyo / magento2-klaviyo

37 stars 51 forks source link

Fix issue with newsletter subscription #118

Closed ihor-sviziev closed 3 years ago

ihor-sviziev commented 3 years ago

Description: This PR continues https://github.com/klaviyo/magento2-klaviyo/pull/56 with the adoption of recent changes inside the module.

When adding a new newsletter to Magento via custom module, Observer NewsletterSubscribeObserver not called, but after saving newsletter to the Magento calls another Observer UserProfileNewsletterSubscribeObserver and try to get a customer by ID, Magento throws Exception because a customer with ID=0 doesn't exist.

The full exception looks like this:

{
   "class":"Magento\\Framework\\Exception\\NoSuchEntityException",
   "message":"No such entity with customerId = 0",
   "code":0,
   "file":"/var/www/html/vendor/magento/framework/Exception/NoSuchEntityException.php:50",
   "trace":[
      "/var/www/html/vendor/magento/module-customer/Model/CustomerRegistry.php:84",
      "/var/www/html/vendor/magento/module-customer/Model/ResourceModel/CustomerRepository.php:368",
      "/var/www/html/vendor/magento/framework/Interception/Interceptor.php:58",
      "/var/www/html/vendor/magento/framework/Interception/Interceptor.php:138",
      "/var/www/html/vendor/magento/framework/Interception/Interceptor.php:153",
      "/var/www/html/generated/code/Magento/Customer/Model/ResourceModel/CustomerRepository/Interceptor.php:52",
      "/var/www/html/vendor/klaviyo/magento2-extension/Observer/UserProfileNewsletterSubscribeObserver.php:33",
      "/var/www/html/vendor/magento/framework/Event/Invoker/InvokerDefault.php:88",
      "/var/www/html/vendor/magento/framework/Event/Invoker/InvokerDefault.php:74",
      "/var/www/html/vendor/magento/framework/Event/Manager.php:66",
      "/var/www/html/generated/code/Magento/Framework/Event/Manager/Proxy.php:95",
      "/var/www/html/vendor/magento/framework/Model/AbstractModel.php:667",
      "/var/www/html/vendor/magento/framework/Model/ExecuteCommitCallbacks.php:44",
      "/var/www/html/vendor/magento/framework/Interception/Interceptor.php:146",
      "/var/www/html/vendor/magento/framework/Interception/Interceptor.php:153",
      "/var/www/html/generated/code/Magento/Framework/DB/Adapter/Pdo/Mysql/Interceptor.php:26",
      "/var/www/html/vendor/magento/framework/Model/ResourceModel/AbstractResource.php:91",
      "/var/www/html/vendor/magento/framework/Model/ResourceModel/Db/AbstractDb.php:426",
      "/var/www/html/vendor/magento/framework/Model/AbstractModel.php:655",
      "/var/www/html/vendor/magento/module-newsletter/Model/Subscriber.php:497",
      "/var/www/html/vendor/magenest/module-popup/Controller/Popup/Subscriber.php:295",
      "/var/www/html/vendor/magenest/module-popup/Controller/Popup/Subscriber.php:235",
      "/var/www/html/vendor/magento/framework/Interception/Interceptor.php:58",
      "/var/www/html/vendor/magento/framework/Interception/Interceptor.php:138",
      "/var/www/html/vendor/magento/framework/Interception/Interceptor.php:153",
      "/var/www/html/generated/code/Magenest/Popup/Controller/Popup/Subscriber/Interceptor.php:26",
      "/var/www/html/vendor/magento/framework/App/Action/Action.php:108",
      "/var/www/html/vendor/magento/framework/Interception/Interceptor.php:58",
      "/var/www/html/vendor/magento/framework/Interception/Interceptor.php:138",
      "/var/www/html/vendor/magento/framework/Interception/Interceptor.php:153",
      "/var/www/html/generated/code/Magenest/Popup/Controller/Popup/Subscriber/Interceptor.php:39",
      "/var/www/html/vendor/magento/framework/App/FrontController.php:186",
      "/var/www/html/vendor/magento/framework/App/FrontController.php:118",
      "/var/www/html/vendor/magento/framework/Interception/Interceptor.php:58",
      "/var/www/html/vendor/magento/framework/Interception/Interceptor.php:138",
      "/var/www/html/vendor/magento/module-store/App/FrontController/Plugin/RequestPreprocessor.php:99",
      "/var/www/html/vendor/magento/framework/Interception/Interceptor.php:135",
      "/var/www/html/vendor/magento/module-page-cache/Model/App/FrontController/BuiltinPlugin.php:69",
      "/var/www/html/vendor/magento/framework/Interception/Interceptor.php:135",
      "/var/www/html/vendor/magento/framework/Interception/Interceptor.php:153",
      "/var/www/html/generated/code/Magento/Framework/App/FrontController/Interceptor.php:26",
      "/var/www/html/vendor/magento/framework/App/Http.php:116",
      "/var/www/html/vendor/magento/framework/App/Bootstrap.php:261",
      "/var/www/html/pub/index.php:40"
   ]
}

As a result, we have 2 issues:

  1. an event about a new newsletter isn't getting sent to Klaviyo.
  2. welcome email about the new subscription isn't getting sent

Related issues

Also fixes #19

Steps to reproduce:

  1. Configure "Welcome email" in the Klaviyo
  2. Create a new module for subscribe newsletter programmatically: https://magento.stackexchange.com/questions/150252/how-to-create-newsletter-subscriber-programmatically-magento2
  3. Try to subscribe to a newsletter.

Expected result: Email what was subscribed should get "Welcome email."

Actual result: "Welcome email" doesn't send

ihor-sviziev commented 3 years ago

Hi @klaviyojad @remstone7 Could you review this PR?