Нет описания

HooksWooCommerce.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace MailPoet\Config;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Logging\LoggerFactory;
  5. use MailPoet\Segments\WooCommerce as WooCommerceSegment;
  6. use MailPoet\Statistics\Track\WooCommercePurchases;
  7. use MailPoet\Subscription\Registration;
  8. use MailPoet\WooCommerce\Settings as WooCommerceSettings;
  9. use MailPoet\WooCommerce\SubscriberEngagement;
  10. use MailPoet\WooCommerce\Subscription as WooCommerceSubscription;
  11. class HooksWooCommerce {
  12. /** @var WooCommerceSubscription */
  13. private $woocommerceSubscription;
  14. /** @var WooCommerceSegment */
  15. private $woocommerceSegment;
  16. /** @var WooCommerceSettings */
  17. private $woocommerceSettings;
  18. /** @var WooCommercePurchases */
  19. private $woocommercePurchases;
  20. /** @var Registration */
  21. private $subscriberRegistration;
  22. /** @var LoggerFactory */
  23. private $loggerFactory;
  24. /** @var SubscriberEngagement */
  25. private $subscriberEngagement;
  26. public function __construct(
  27. WooCommerceSubscription $woocommerceSubscription,
  28. WooCommerceSegment $woocommerceSegment,
  29. WooCommerceSettings $woocommerceSettings,
  30. WooCommercePurchases $woocommercePurchases,
  31. Registration $subscriberRegistration,
  32. LoggerFactory $loggerFactory,
  33. SubscriberEngagement $subscriberEngagement
  34. ) {
  35. $this->woocommerceSubscription = $woocommerceSubscription;
  36. $this->woocommerceSegment = $woocommerceSegment;
  37. $this->woocommerceSettings = $woocommerceSettings;
  38. $this->woocommercePurchases = $woocommercePurchases;
  39. $this->loggerFactory = $loggerFactory;
  40. $this->subscriberRegistration = $subscriberRegistration;
  41. $this->subscriberEngagement = $subscriberEngagement;
  42. }
  43. public function extendWooCommerceCheckoutForm() {
  44. try {
  45. $this->woocommerceSubscription->extendWooCommerceCheckoutForm();
  46. } catch (\Throwable $e) {
  47. $this->logError($e, 'WooCommerce Subscription');
  48. }
  49. }
  50. public function subscribeOnCheckout($orderId, $data) {
  51. try {
  52. $this->woocommerceSubscription->subscribeOnCheckout($orderId, $data);
  53. } catch (\Throwable $e) {
  54. $this->logError($e, 'WooCommerce Subscription');
  55. }
  56. }
  57. public function disableWooCommerceSettings() {
  58. try {
  59. $this->woocommerceSettings->disableWooCommerceSettings();
  60. } catch (\Throwable $e) {
  61. $this->logError($e, 'WooCommerce Settings');
  62. }
  63. }
  64. public function synchronizeRegisteredCustomer($wpUserId, $currentFilter = null) {
  65. try {
  66. $this->woocommerceSegment->synchronizeRegisteredCustomer($wpUserId, $currentFilter);
  67. } catch (\Throwable $e) {
  68. $this->logError($e, 'WooCommerce Sync');
  69. }
  70. }
  71. public function synchronizeGuestCustomer($orderId) {
  72. try {
  73. $this->woocommerceSegment->synchronizeGuestCustomer($orderId);
  74. } catch (\Throwable $e) {
  75. $this->logError($e, 'WooCommerce Sync');
  76. }
  77. }
  78. public function trackPurchase($id, $useCookies = true) {
  79. try {
  80. $this->woocommercePurchases->trackPurchase($id, $useCookies);
  81. } catch (\Throwable $e) {
  82. $this->logError($e, 'WooCommerce Purchases');
  83. }
  84. }
  85. public function extendForm() {
  86. try {
  87. $this->subscriberRegistration->extendForm();
  88. } catch (\Throwable $e) {
  89. $this->logError($e, 'WooCommerce Extend Form');
  90. }
  91. }
  92. public function onRegister($errors, string $userLogin, string $userEmail = null) {
  93. try {
  94. if (empty($errors->errors)) {
  95. $this->subscriberRegistration->onRegister($errors, $userLogin, $userEmail);
  96. }
  97. } catch (\Throwable $e) {
  98. $this->logError($e, 'WooCommerce on Register');
  99. }
  100. return $errors;
  101. }
  102. public function updateSubscriberEngagement($orderId) {
  103. try {
  104. $this->subscriberEngagement->updateSubscriberEngagement($orderId);
  105. } catch (\Throwable $e) {
  106. $this->logError($e, 'WooCommerce Update Subscriber Engagement');
  107. }
  108. }
  109. private function logError(\Throwable $e, $name) {
  110. $logger = $this->loggerFactory->getLogger($name);
  111. $logger->addError($e->getMessage(), [
  112. 'file' => $e->getFile(),
  113. 'line' => $e->getLine(),
  114. ]);
  115. }
  116. }