Loading src/EventSubscriber/OrderMauticSubscriber.php +127 −73 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ namespace Drupal\mautic_paragraph_commerce\EventSubscriber; use Drupal\commerce_order\Entity\OrderInterface; use Drupal\mautic_paragraph_commerce\MauticCommerceApiInterface; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Queue\QueueFactory; Loading Loading @@ -129,70 +130,16 @@ class OrderMauticSubscriber implements EventSubscriberInterface { // Get the order entity. /** @var \Drupal\commerce_order\Entity\OrderInterface $order */ $order = $event->getEntity(); $marketing_terms = $order->getData('mautic_terms_and_conditions'); $marketing_list = []; $exclude_from_marketing_list = []; $applied_promotions = []; // Get list of promotions applied to this order. foreach ($order->collectAdjustments() as $adjustment) { if ($adjustment->getType() == 'promotion') { $applied_promotions[$adjustment->getSourceId()] = $adjustment->getLabel(); } } // List enabled mautic commerce conditions. $mautic_commerce_conditions = $this->entityTypeManager->getStorage('mautic_commerce_condition') ->loadByProperties(['status' => 1]); foreach ($mautic_commerce_conditions as $mautic_condition) { /** @var \Drupal\mautic_paragraph_commerce\MauticCommerceConditionInterface $mautic_condition */ $mautic_cond_settings = $mautic_condition->getSettings(); // Process mautic commerce conditions. if (!empty($mautic_cond_settings['commerce_promotions'])) { foreach ($mautic_cond_settings['commerce_promotions'] as $promotion) { if (array_key_exists($promotion, $applied_promotions)) { if ($mautic_condition->process($order)) { if ($mautic_cond_settings['exclude_from_marketing_terms'] == TRUE) { $exclude_from_marketing_list[$mautic_cond_settings['mautic_segment']] = $mautic_cond_settings['mautic_segment']; } else { $marketing_list[$mautic_cond_settings['mautic_segment']] = $mautic_cond_settings['mautic_segment']; } } } } } else { if ($mautic_condition->process($order)) { if ($mautic_cond_settings['exclude_from_marketing_terms'] == TRUE) { $exclude_from_marketing_list[$mautic_cond_settings['mautic_segment']] = $mautic_cond_settings['mautic_segment']; } else { $marketing_list[$mautic_cond_settings['mautic_segment']] = $mautic_cond_settings['mautic_segment']; } } } } // Merge segment lists. if ($marketing_terms == TRUE) { $final_segment_list = array_unique(array_merge($marketing_list, $exclude_from_marketing_list), SORT_REGULAR); } else { $final_segment_list = $exclude_from_marketing_list; } $segmet_list = $this->getMauticSegmentList($order); if ($mautic_config->get('mautic_segment_debug') == TRUE) { $this->logger->debug('Marketing Mautic segmnet list: <pre>@data</pre>', [ '@data' => print_r($marketing_list, TRUE), ]); $this->logger->debug('Excluded from Marketing Mautic segmnet list: <pre>@data</pre>', [ '@data' => print_r($exclude_from_marketing_list, TRUE), '@data' => print_r($segmet_list, TRUE), ]); } if (!empty($final_segment_list)) { if (!empty($segmet_list)) { // Get customer details. $email = $order->getEmail(); /** @var \Drupal\profile\Entity\ProfileInterface $billing_profile */ Loading @@ -209,6 +156,9 @@ class OrderMauticSubscriber implements EventSubscriberInterface { $countries = CountryManager::getStandardList(); $full_country_name = $countries[$country_code]->__toString(); // Get tags. $tags_list = $this->getMauticTags($order); $data = [ 'firstname' => $first_name ?? '', 'lastname' => $last_name ?? '', Loading @@ -216,11 +166,12 @@ class OrderMauticSubscriber implements EventSubscriberInterface { 'city' => $city ?? '', 'email' => $email, 'website' => $this->host, 'tags' => $tags_list, 'ipAddress' => $this->clientIp, 'overwriteWithBlank' => TRUE, ]; $queue_data['data'] = $data; $queue_data['segment_list'] = $final_segment_list; $queue_data['segment_list'] = $segmet_list; if ($mautic_config->get('mautic_segment_debug') == TRUE) { $this->logger->debug('Contact data before Mautic contact creation: <pre>@data</pre>', [ Loading @@ -236,11 +187,26 @@ class OrderMauticSubscriber implements EventSubscriberInterface { $this->queue->createItem($queue_data); } else { $this->addCustomerToSegment($data, $segmet_list); } } } /** * Create and add customer to Mautic segment. * * @param array $data * The customer data. * @param array $segment_list * The segment list. */ private function addCustomerToSegment(array $data, array $segment_list) { // Create Mautic contact from customer billing profile. $mautic_contact = $this->mauticCommerceApi->createContact($data); // Add contact to segments. foreach ($final_segment_list as $segment_id) { foreach ($segment_list as $segment_id) { if (isset($mautic_contact['contact']['id'])) { if ($mautic_contact['contact']['id']) { $response = $this->mauticCommerceApi->addContactToSegment($segment_id, $mautic_contact['contact']['id']); Loading @@ -256,8 +222,96 @@ class OrderMauticSubscriber implements EventSubscriberInterface { } } } /** * Set product title as Mautic tag. * * @param \Drupal\commerce_order\Entity\OrderInterface $order * The customer order. * * @return array * The list of tags. */ private function getMauticTags(OrderInterface $order) { $tags = []; /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */ foreach ($order->getItems() as $order_item) { // Get product variation. /** @var \Drupal\commerce_product\Entity\ProductInterface $product */ $product = $order_item->getPurchasedEntity()->getProduct(); $tags[] = $product->label(); } return $tags; } /** * Generate list of Mautic tags. * * @param \Drupal\commerce_order\Entity\OrderInterface $order * The customer order. * * @return array * The list of tags. */ private function getMauticSegmentList(OrderInterface $order) { $marketing_terms = $order->getData('mautic_terms_and_conditions'); $marketing_list = []; $exclude_from_marketing_list = []; $applied_promotions = []; // Get list of promotions applied to this order. foreach ($order->collectAdjustments() as $adjustment) { if ($adjustment->getType() === 'promotion') { $applied_promotions[$adjustment->getSourceId()] = $adjustment->getLabel(); } } // List enabled mautic commerce conditions. $mautic_commerce_conditions = $this->entityTypeManager->getStorage('mautic_commerce_condition') ->loadByProperties(['status' => 1]); foreach ($mautic_commerce_conditions as $mautic_condition) { /** @var \Drupal\mautic_paragraph_commerce\MauticCommerceConditionInterface $mautic_condition */ $mautic_cond_settings = $mautic_condition->getSettings(); // Process mautic commerce conditions. if (!empty($mautic_cond_settings['commerce_promotions'])) { foreach ($mautic_cond_settings['commerce_promotions'] as $promotion) { if (array_key_exists($promotion, $applied_promotions)) { if ($mautic_condition->process($order)) { if ($mautic_cond_settings['exclude_from_marketing_terms'] == TRUE) { $exclude_from_marketing_list[$mautic_cond_settings['mautic_segment']] = $mautic_cond_settings['mautic_segment']; } else { $marketing_list[$mautic_cond_settings['mautic_segment']] = $mautic_cond_settings['mautic_segment']; } } } } } else { if ($mautic_condition->process($order)) { if ($mautic_cond_settings['exclude_from_marketing_terms'] == TRUE) { $exclude_from_marketing_list[$mautic_cond_settings['mautic_segment']] = $mautic_cond_settings['mautic_segment']; } else { $marketing_list[$mautic_cond_settings['mautic_segment']] = $mautic_cond_settings['mautic_segment']; } } } } // Merge segment lists. if ($marketing_terms == TRUE) { $final_segment_list = array_unique(array_merge($marketing_list, $exclude_from_marketing_list), SORT_REGULAR); } else { $final_segment_list = $exclude_from_marketing_list; } return $final_segment_list; } } Loading
src/EventSubscriber/OrderMauticSubscriber.php +127 −73 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ namespace Drupal\mautic_paragraph_commerce\EventSubscriber; use Drupal\commerce_order\Entity\OrderInterface; use Drupal\mautic_paragraph_commerce\MauticCommerceApiInterface; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Queue\QueueFactory; Loading Loading @@ -129,70 +130,16 @@ class OrderMauticSubscriber implements EventSubscriberInterface { // Get the order entity. /** @var \Drupal\commerce_order\Entity\OrderInterface $order */ $order = $event->getEntity(); $marketing_terms = $order->getData('mautic_terms_and_conditions'); $marketing_list = []; $exclude_from_marketing_list = []; $applied_promotions = []; // Get list of promotions applied to this order. foreach ($order->collectAdjustments() as $adjustment) { if ($adjustment->getType() == 'promotion') { $applied_promotions[$adjustment->getSourceId()] = $adjustment->getLabel(); } } // List enabled mautic commerce conditions. $mautic_commerce_conditions = $this->entityTypeManager->getStorage('mautic_commerce_condition') ->loadByProperties(['status' => 1]); foreach ($mautic_commerce_conditions as $mautic_condition) { /** @var \Drupal\mautic_paragraph_commerce\MauticCommerceConditionInterface $mautic_condition */ $mautic_cond_settings = $mautic_condition->getSettings(); // Process mautic commerce conditions. if (!empty($mautic_cond_settings['commerce_promotions'])) { foreach ($mautic_cond_settings['commerce_promotions'] as $promotion) { if (array_key_exists($promotion, $applied_promotions)) { if ($mautic_condition->process($order)) { if ($mautic_cond_settings['exclude_from_marketing_terms'] == TRUE) { $exclude_from_marketing_list[$mautic_cond_settings['mautic_segment']] = $mautic_cond_settings['mautic_segment']; } else { $marketing_list[$mautic_cond_settings['mautic_segment']] = $mautic_cond_settings['mautic_segment']; } } } } } else { if ($mautic_condition->process($order)) { if ($mautic_cond_settings['exclude_from_marketing_terms'] == TRUE) { $exclude_from_marketing_list[$mautic_cond_settings['mautic_segment']] = $mautic_cond_settings['mautic_segment']; } else { $marketing_list[$mautic_cond_settings['mautic_segment']] = $mautic_cond_settings['mautic_segment']; } } } } // Merge segment lists. if ($marketing_terms == TRUE) { $final_segment_list = array_unique(array_merge($marketing_list, $exclude_from_marketing_list), SORT_REGULAR); } else { $final_segment_list = $exclude_from_marketing_list; } $segmet_list = $this->getMauticSegmentList($order); if ($mautic_config->get('mautic_segment_debug') == TRUE) { $this->logger->debug('Marketing Mautic segmnet list: <pre>@data</pre>', [ '@data' => print_r($marketing_list, TRUE), ]); $this->logger->debug('Excluded from Marketing Mautic segmnet list: <pre>@data</pre>', [ '@data' => print_r($exclude_from_marketing_list, TRUE), '@data' => print_r($segmet_list, TRUE), ]); } if (!empty($final_segment_list)) { if (!empty($segmet_list)) { // Get customer details. $email = $order->getEmail(); /** @var \Drupal\profile\Entity\ProfileInterface $billing_profile */ Loading @@ -209,6 +156,9 @@ class OrderMauticSubscriber implements EventSubscriberInterface { $countries = CountryManager::getStandardList(); $full_country_name = $countries[$country_code]->__toString(); // Get tags. $tags_list = $this->getMauticTags($order); $data = [ 'firstname' => $first_name ?? '', 'lastname' => $last_name ?? '', Loading @@ -216,11 +166,12 @@ class OrderMauticSubscriber implements EventSubscriberInterface { 'city' => $city ?? '', 'email' => $email, 'website' => $this->host, 'tags' => $tags_list, 'ipAddress' => $this->clientIp, 'overwriteWithBlank' => TRUE, ]; $queue_data['data'] = $data; $queue_data['segment_list'] = $final_segment_list; $queue_data['segment_list'] = $segmet_list; if ($mautic_config->get('mautic_segment_debug') == TRUE) { $this->logger->debug('Contact data before Mautic contact creation: <pre>@data</pre>', [ Loading @@ -236,11 +187,26 @@ class OrderMauticSubscriber implements EventSubscriberInterface { $this->queue->createItem($queue_data); } else { $this->addCustomerToSegment($data, $segmet_list); } } } /** * Create and add customer to Mautic segment. * * @param array $data * The customer data. * @param array $segment_list * The segment list. */ private function addCustomerToSegment(array $data, array $segment_list) { // Create Mautic contact from customer billing profile. $mautic_contact = $this->mauticCommerceApi->createContact($data); // Add contact to segments. foreach ($final_segment_list as $segment_id) { foreach ($segment_list as $segment_id) { if (isset($mautic_contact['contact']['id'])) { if ($mautic_contact['contact']['id']) { $response = $this->mauticCommerceApi->addContactToSegment($segment_id, $mautic_contact['contact']['id']); Loading @@ -256,8 +222,96 @@ class OrderMauticSubscriber implements EventSubscriberInterface { } } } /** * Set product title as Mautic tag. * * @param \Drupal\commerce_order\Entity\OrderInterface $order * The customer order. * * @return array * The list of tags. */ private function getMauticTags(OrderInterface $order) { $tags = []; /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */ foreach ($order->getItems() as $order_item) { // Get product variation. /** @var \Drupal\commerce_product\Entity\ProductInterface $product */ $product = $order_item->getPurchasedEntity()->getProduct(); $tags[] = $product->label(); } return $tags; } /** * Generate list of Mautic tags. * * @param \Drupal\commerce_order\Entity\OrderInterface $order * The customer order. * * @return array * The list of tags. */ private function getMauticSegmentList(OrderInterface $order) { $marketing_terms = $order->getData('mautic_terms_and_conditions'); $marketing_list = []; $exclude_from_marketing_list = []; $applied_promotions = []; // Get list of promotions applied to this order. foreach ($order->collectAdjustments() as $adjustment) { if ($adjustment->getType() === 'promotion') { $applied_promotions[$adjustment->getSourceId()] = $adjustment->getLabel(); } } // List enabled mautic commerce conditions. $mautic_commerce_conditions = $this->entityTypeManager->getStorage('mautic_commerce_condition') ->loadByProperties(['status' => 1]); foreach ($mautic_commerce_conditions as $mautic_condition) { /** @var \Drupal\mautic_paragraph_commerce\MauticCommerceConditionInterface $mautic_condition */ $mautic_cond_settings = $mautic_condition->getSettings(); // Process mautic commerce conditions. if (!empty($mautic_cond_settings['commerce_promotions'])) { foreach ($mautic_cond_settings['commerce_promotions'] as $promotion) { if (array_key_exists($promotion, $applied_promotions)) { if ($mautic_condition->process($order)) { if ($mautic_cond_settings['exclude_from_marketing_terms'] == TRUE) { $exclude_from_marketing_list[$mautic_cond_settings['mautic_segment']] = $mautic_cond_settings['mautic_segment']; } else { $marketing_list[$mautic_cond_settings['mautic_segment']] = $mautic_cond_settings['mautic_segment']; } } } } } else { if ($mautic_condition->process($order)) { if ($mautic_cond_settings['exclude_from_marketing_terms'] == TRUE) { $exclude_from_marketing_list[$mautic_cond_settings['mautic_segment']] = $mautic_cond_settings['mautic_segment']; } else { $marketing_list[$mautic_cond_settings['mautic_segment']] = $mautic_cond_settings['mautic_segment']; } } } } // Merge segment lists. if ($marketing_terms == TRUE) { $final_segment_list = array_unique(array_merge($marketing_list, $exclude_from_marketing_list), SORT_REGULAR); } else { $final_segment_list = $exclude_from_marketing_list; } return $final_segment_list; } }