Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

closes #1866 for magento 2.2 #1870

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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';
Expand Down
44 changes: 41 additions & 3 deletions Model/Api/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class Order
protected $_counter;

protected $_batchId;
protected $modifiedOrder = false;

/**
* @param \Ebizmarts\MailChimp\Helper\Data $helper
Expand Down Expand Up @@ -159,6 +160,7 @@ protected function _getModifiedOrders($magentoStoreId)
{
$batchArray = [];
$mailchimpStoreId = $this->_helper->getConfigValue(\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]);
Expand Down Expand Up @@ -198,7 +200,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);
Expand Down Expand Up @@ -235,6 +241,7 @@ protected function _getNewOrders($magentoStoreId)
{
$batchArray = [];
$mailchimpStoreId = $this->_helper->getConfigValue(\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]);
Expand Down Expand Up @@ -277,7 +284,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);
Expand Down Expand Up @@ -318,12 +329,19 @@ protected function _getNewOrders($magentoStoreId)
* @return string
* @throws \Exception
*/
protected function GeneratePOSTPayload(\Magento\Sales\Model\Order $order, $mailchimpStoreId, $magentoStoreId, $isModifiedOrder = false)
protected function GeneratePOSTPayload(\Magento\Sales\Model\Order $order, $mailchimpStoreId, $magentoStoreId, $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()) {
Expand Down Expand Up @@ -725,6 +743,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)) {
Expand Down
16 changes: 16 additions & 0 deletions Model/Config/Source/CampaignAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Ebizmarts\MailChimp\Model\Config\Source;

class CampaignAction implements \Magento\Framework\Option\ArrayInterface
{
private $actions = [
['value' => 'sent', 'label' => 'Sent'],
['value' => 'open', 'label' => 'Open'],
['value' => 'click', 'label' => 'Click']
];
public function toOptionArray()
{
return $this->actions;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"require" : {
"magento/framework": "101.0.*",
"ebizmarts/mailchimp-lib": ">=3.0.37",
"ebizmarts/mailchimp-lib": ">=3.0.38",
"ext-json": "*"
}
}
10 changes: 10 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,16 @@
<field id="*/*/active">1</field>
</depends>
</field>
<field id="campaign_action" translate="label" type="multiselect" sortOrder="155" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Campaign Attribution Action</label>
<source_model>Ebizmarts\MailChimp\Model\Config\Source\CampaignAction</source_model>
<can_be_empty>1</can_be_empty>
<size>3</size>
<depends>
<field id="*/*/active">1</field>
</depends>
<comment>Order attribution based on customer actions</comment>
</field>
<field id="reset_errors_retry" translate="button_label" type="button" sortOrder="160" showInDefault="1" showInWebsite="1" showInStore="1">
<button_label>Reset Errors and retry</button_label>
<frontend_model>Ebizmarts\MailChimp\Block\Adminhtml\System\Config\ResetErrors</frontend_model>
Expand Down
11 changes: 11 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<mailchimp>
<ecommerce>
<campaign_action>sent,open,click</campaign_action>
</ecommerce>
</mailchimp>
</default>
</config>
1 change: 1 addition & 0 deletions view/adminhtml/web/js/configapikey.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ define(
if (ecommerceEnabled == 0 && abandonedCartEnabled == 1) {
self._changeAbandonedCart();
}
$('#mailchimp_ecommerce_campaign_action').attr('size',3);
},
_changeEcommerce: function () {
var self = this;
Expand Down