PlaceCreateAutomationStatus.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace Dotdigitalgroup\Email\Observer\Sales;
  3. /**
  4. * Customer and guest new order automation.
  5. */
  6. class PlaceCreateAutomationStatus implements \Magento\Framework\Event\ObserverInterface
  7. {
  8. /**
  9. * @var \Dotdigitalgroup\Email\Model\ResourceModel\Automation
  10. */
  11. private $automationResource;
  12. /**
  13. * @var \Dotdigitalgroup\Email\Helper\Data
  14. */
  15. private $helper;
  16. /**
  17. * @var \Magento\Store\Model\StoreManagerInterface
  18. */
  19. private $storeManager;
  20. /**
  21. * @var \Dotdigitalgroup\Email\Model\AutomationFactory
  22. */
  23. private $automationFactory;
  24. /**
  25. * PlaceCreateAutomationStatus constructor.
  26. *
  27. * @param \Dotdigitalgroup\Email\Model\AutomationFactory $automationFactory
  28. * @param \Dotdigitalgroup\Email\Helper\Data $data
  29. * @param \Dotdigitalgroup\Email\Model\ResourceModel\Automation $automationResource
  30. * @param \Magento\Store\Model\StoreManagerInterface $storeManagerInterface
  31. */
  32. public function __construct(
  33. \Dotdigitalgroup\Email\Model\AutomationFactory $automationFactory,
  34. \Dotdigitalgroup\Email\Helper\Data $data,
  35. \Dotdigitalgroup\Email\Model\ResourceModel\Automation $automationResource,
  36. \Magento\Store\Model\StoreManagerInterface $storeManagerInterface
  37. ) {
  38. $this->automationFactory = $automationFactory;
  39. $this->automationResource = $automationResource;
  40. $this->helper = $data;
  41. $this->storeManager = $storeManagerInterface;
  42. }
  43. /**
  44. * Execute method.
  45. *
  46. * @param \Magento\Framework\Event\Observer $observer
  47. *
  48. * @return $this
  49. */
  50. public function execute(\Magento\Framework\Event\Observer $observer)
  51. {
  52. $order = $observer->getEvent()->getOrder();
  53. $email = $order->getCustomerEmail();
  54. $website = $this->storeManager->getWebsite($order->getWebsiteId());
  55. $storeName = $this->storeManager->getStore($order->getStoreId())
  56. ->getName();
  57. //if api is not enabled
  58. if (!$this->helper->isEnabled($website)) {
  59. return $this;
  60. }
  61. //automation enrolment for order
  62. if ($order->getCustomerIsGuest()) {
  63. // guest to automation mapped
  64. $programType = 'XML_PATH_CONNECTOR_AUTOMATION_STUDIO_GUEST_ORDER';
  65. $automationType
  66. = \Dotdigitalgroup\Email\Model\Sync\Automation::AUTOMATION_TYPE_NEW_GUEST_ORDER;
  67. } else {
  68. // customer to automation mapped
  69. $programType = 'XML_PATH_CONNECTOR_AUTOMATION_STUDIO_ORDER';
  70. $automationType
  71. = \Dotdigitalgroup\Email\Model\Sync\Automation::AUTOMATION_TYPE_NEW_ORDER;
  72. }
  73. $programId = $this->helper->getAutomationIdByType(
  74. $programType,
  75. $order->getStoreId()
  76. );
  77. //the program is not mapped
  78. if (!$programId) {
  79. return $this;
  80. }
  81. try {
  82. $automation = $this->automationFactory->create()
  83. ->setEmail($email)
  84. ->setAutomationType($automationType)
  85. ->setEnrolmentStatus(\Dotdigitalgroup\Email\Model\Sync\Automation::AUTOMATION_STATUS_PENDING)
  86. ->setTypeId($order->getIncrementId())
  87. ->setWebsiteId($website->getId())
  88. ->setStoreName($storeName)
  89. ->setProgramId($programId);
  90. $this->automationResource->save($automation);
  91. } catch (\Exception $e) {
  92. $this->helper->debug((string)$e, []);
  93. }
  94. return $this;
  95. }
  96. }