From fc73a08dcff54d773734ac5f9ae43ba2343e8cbc Mon Sep 17 00:00:00 2001 From: gonzalo Date: Thu, 22 Feb 2024 13:01:36 -0300 Subject: [PATCH] closes #1866 for magento 2.3 --- Helper/Data.php | 1 + Model/Api/Order.php | 47 +++++++++++++++++++++++--- Model/Config/Source/CampaignAction.php | 16 +++++++++ composer.json | 2 +- etc/adminhtml/system.xml | 10 ++++++ etc/config.xml | 11 ++++++ view/adminhtml/web/js/configapikey.js | 1 + 7 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 Model/Config/Source/CampaignAction.php create mode 100644 etc/config.xml diff --git a/Helper/Data.php b/Helper/Data.php index 94b9329e..5b369435 100755 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -50,6 +50,7 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper const XML_MAGENTO_MAIL = 'mailchimp/general/magentoemail'; const XML_SEND_PROMO = 'mailchimp/ecommerce/send_promo'; const XML_INCLUDING_TAXES = 'mailchimp/ecommerce/including_taxes'; + const XML_CAMPAIGN_ACTION = 'mailchimp/ecommerce/campaign_action'; const XML_POPUP_FORM = 'mailchimp/general/popup_form'; const XML_POPUP_URL = 'mailchimp/general/popup_url'; const XML_CLEAN_ERROR_MONTHS = 'mailchimp/ecommerce/clean_errors_months'; diff --git a/Model/Api/Order.php b/Model/Api/Order.php index c8594cd8..3c2c41fa 100644 --- a/Model/Api/Order.php +++ b/Model/Api/Order.php @@ -83,8 +83,8 @@ class Order protected $_chimpSyncEcommerce; protected $_firstDate; protected $_counter; - protected $_batchId; + protected $modifiedOrder = false; /** * @param \Ebizmarts\MailChimp\Helper\Data $helper @@ -163,6 +163,7 @@ protected function _getModifiedOrders($magentoStoreId) \Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE, $magentoStoreId ); + $isSynced = $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_IS_SYNC, $magentoStoreId); $modifiedOrders = $this->_getCollection(); // select orders for the current Magento store id $modifiedOrders->addFieldToFilter('store_id', ['eq' => $magentoStoreId]); @@ -204,7 +205,11 @@ protected function _getModifiedOrders($magentoStoreId) } } - $orderJson = $this->generatePOSTPayload($order, $mailchimpStoreId, $magentoStoreId, true); + $orderJson = $this->generatePOSTPayload($order, $mailchimpStoreId, $magentoStoreId, true, $isSynced); + if ($this->modifiedOrder) { + $order->save(); + $this->modifiedOrder = false; + } if ($orderJson!==false) { if (!empty($orderJson)) { $this->_helper->modifyCounter(\Ebizmarts\MailChimp\Helper\Data::ORD_MOD); @@ -243,6 +248,7 @@ protected function _getNewOrders($magentoStoreId) \Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE, $magentoStoreId ); + $isSynced = $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_IS_SYNC, $magentoStoreId); $newOrders = $this->_getCollection(); // select carts for the current Magento store id $newOrders->addFieldToFilter('store_id', ['eq' => $magentoStoreId]); @@ -285,7 +291,11 @@ protected function _getNewOrders($magentoStoreId) $this->_counter++; } } - $orderJson = $this->generatePOSTPayload($order, $mailchimpStoreId, $magentoStoreId); + $orderJson = $this->generatePOSTPayload($order, $mailchimpStoreId, $magentoStoreId, false, $isSynced); + if ($this->modifiedOrder) { + $order->save(); + $this->modifiedOrder = false; + } if ($orderJson!==false) { if (!empty($orderJson)) { $this->_helper->modifyCounter(\Ebizmarts\MailChimp\Helper\Data::ORD_NEW); @@ -329,12 +339,20 @@ protected function generatePOSTPayload( \Magento\Sales\Model\Order $order, $mailchimpStoreId, $magentoStoreId, - $isModifiedOrder = false + $isModifiedOrder, + $isSynced ) { $data = []; $data['id'] = $order->getIncrementId(); if ($order->getMailchimpCampaignId()) { $data['campaign_id'] = $order->getMailchimpCampaignId(); + } elseif ($isSynced) { + if ($campaignId = $this->getCampaign($magentoStoreId, $order->getCustomerEmail())) { + $data['campaign_id'] = $campaignId; + $order->setMailchimpCampaignId($campaignId); + $order->setMailchimpFlag(1); + $this->modifiedOrder = true; + } } if ($order->getMailchimpLandingPage()) { @@ -738,7 +756,26 @@ protected function _getPromoData(\Magento\Sales\Model\Order $order) } return $promo; } - + protected function getCampaign($store, $email) + { + $campaign_id = null; + $api = $this->_helper->getApi($store); + $actions = $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_CAMPAIGN_ACTION, $store); + try { + $activity = $api->lists->members->memberActivity->get($this->_helper->getDefaultList($store), md5($email), null, null, $actions); + if ($activity) { + foreach ($activity['activity'] as $act) { + if (key_exists('action', $act) && key_exists('campaign_id', $act) && $act['campaign_id']) { + $campaign_id = $act['campaign_id']; + break; + } + } + } + } catch (\Mailchimp_Error $e) { + $this->_helper->log("No activity for $email"); + } + return $campaign_id; + } protected function _updateOrder($storeId, $entityId, $sync_delta = null, $sync_error = null, $sync_modified = null) { if (!empty($sync_error)) { diff --git a/Model/Config/Source/CampaignAction.php b/Model/Config/Source/CampaignAction.php new file mode 100644 index 00000000..c03ee626 --- /dev/null +++ b/Model/Config/Source/CampaignAction.php @@ -0,0 +1,16 @@ + 'sent', 'label' => 'Sent'], + ['value' => 'open', 'label' => 'Open'], + ['value' => 'click', 'label' => 'Click'] + ]; + public function toOptionArray() + { + return $this->actions; + } +} diff --git a/composer.json b/composer.json index a7b8344f..08d6c947 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ }, "require" : { "magento/framework": "102.0.*", - "ebizmarts/mailchimp-lib": ">=3.0.37", + "ebizmarts/mailchimp-lib": ">=3.0.38", "ext-json": "*" } } diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 7b8b9e5b..803eae0c 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -229,6 +229,16 @@ 1 + + + Ebizmarts\MailChimp\Model\Config\Source\CampaignAction + 1 + 3 + + 1 + + Order attribution based on customer actions + Reset Errors and retry Ebizmarts\MailChimp\Block\Adminhtml\System\Config\ResetErrors diff --git a/etc/config.xml b/etc/config.xml new file mode 100644 index 00000000..590d6640 --- /dev/null +++ b/etc/config.xml @@ -0,0 +1,11 @@ + + + + + + sent,open,click + + + + diff --git a/view/adminhtml/web/js/configapikey.js b/view/adminhtml/web/js/configapikey.js index 69f49d51..0db14efe 100644 --- a/view/adminhtml/web/js/configapikey.js +++ b/view/adminhtml/web/js/configapikey.js @@ -85,6 +85,7 @@ define( if (ecommerceEnabled == 0 && abandonedCartEnabled == 1) { self._changeAbandonedCart(); } + $('#mailchimp_ecommerce_campaign_action').attr('size',3); }, _changeEcommerce: function () { var self = this;