CustomerEmailNotificationPlugin.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace Dotdigitalgroup\Email\Plugin;
  3. /**
  4. * Disable customer email depending on settings value.
  5. *
  6. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  7. */
  8. class CustomerEmailNotificationPlugin
  9. {
  10. /**
  11. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  12. */
  13. public $scopeConfig;
  14. /**
  15. * @var \Magento\Store\Model\StoreManagerInterface
  16. */
  17. private $storeManager;
  18. /**
  19. * CustomerEmailNotificationPlugin constructor.
  20. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  21. */
  22. public function __construct(
  23. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  24. \Magento\Store\Model\StoreManagerInterface $storeManager
  25. ) {
  26. $this->scopeConfig = $scopeConfig;
  27. $this->storeManager = $storeManager;
  28. }
  29. /**
  30. * @param \Magento\Customer\Model\EmailNotificationInterface $emailNotification
  31. * @param callable $proceed
  32. * @param \Magento\Customer\Api\Data\CustomerInterface $customer
  33. * @param string $type
  34. * @param string $backUrl
  35. * @param string|int $storeId
  36. * @param string|null $sendemailStoreId
  37. *
  38. * @return mixed
  39. */
  40. public function aroundNewAccount(
  41. \Magento\Customer\Model\EmailNotificationInterface $emailNotification,
  42. callable $proceed,
  43. \Magento\Customer\Api\Data\CustomerInterface $customer,
  44. $type = \Magento\Customer\Model\EmailNotificationInterface::NEW_ACCOUNT_EMAIL_REGISTERED,
  45. $backUrl = '',
  46. $storeId = 0,
  47. $sendemailStoreId = null
  48. ) {
  49. if (! $storeId) {
  50. $storeId = $this->getWebsiteStoreId($customer, $sendemailStoreId);
  51. }
  52. if (! $this->scopeConfig->getValue(
  53. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_DISABLE_CUSTOMER_SUCCESS,
  54. 'store',
  55. $storeId
  56. )
  57. ) {
  58. return $proceed($customer, $type, $backUrl, $storeId, $sendemailStoreId);
  59. }
  60. }
  61. /**
  62. * Get either first store ID from a set website or the provided as default
  63. *
  64. * @param \Magento\Customer\Api\Data\CustomerInterface $customer
  65. * @param int|string|null $defaultStoreId
  66. * @return int
  67. */
  68. private function getWebsiteStoreId($customer, $defaultStoreId = null)
  69. {
  70. if ($customer->getWebsiteId() != 0 && empty($defaultStoreId)) {
  71. $storeIds = $this->storeManager->getWebsite($customer->getWebsiteId())->getStoreIds();
  72. $defaultStoreId = reset($storeIds);
  73. }
  74. return $defaultStoreId;
  75. }
  76. }