Skip to content

Commit

Permalink
Merge pull request #564 from mailchimp/issue561-2.3
Browse files Browse the repository at this point in the history
closes #561 for magento 2.3
  • Loading branch information
gonzaloebiz authored Jan 22, 2019
2 parents 34d54dc + 9bad438 commit a6502d1
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 5 deletions.
77 changes: 77 additions & 0 deletions Controller/Campaign/Check.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* MailChimp Magento Component
*
* @category Ebizmarts
* @package MailChimp
* @author Ebizmarts Team <info@ebizmarts.com>
* @copyright Ebizmarts (http://ebizmarts.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @date: 1/19/19 12:36 PM
* @file: Check.php
*/

namespace Ebizmarts\MailChimp\Controller\Campaign;

use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;


class Check extends \Magento\Framework\App\Action\Action
{
/**
* @var \Ebizmarts\MailChimp\Helper\Data
*/
protected $_helper;
/**
* @var ResultFactory
*/
protected $_resultFactory;
protected $_storeManager;

/**
* Get constructor.
* @param Context $context
* @param \Ebizmarts\MailChimp\Helper\Data $helper
*/
public function __construct(
Context $context,
\Ebizmarts\MailChimp\Helper\Data $helper,
\Magento\Store\Model\StoreManagerInterface $storeManager
) {

parent::__construct($context);
$this->_resultFactory = $context->getResultFactory();
$this->_helper = $helper;
$this->_storeManager = $storeManager;
}

public function execute()
{
$param = $this->getRequest()->getParams();
$mc_cid = null;
if(key_exists('mc_cid', $param)) {
$mc_cid = $param['mc_cid'];
$magentoStoreId = $this->_storeManager->getStore()->getId();
$api = $this->_helper->getApi($magentoStoreId);
try {
$campaign =$api->campaigns->get($mc_cid);
$mailchimpList = $this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_LIST,$magentoStoreId);
if($mailchimpList == $campaign['recipients']['list_id']) {
$valid = 1;
} else {
$valid = 0;
}
} catch(\Exception $e) {
$valid = 0;
}
}
else {
$valid = 0;
}
$resultJson = $this->_resultFactory->create(ResultFactory::TYPE_JSON);
$resultJson->setData(['valid' => $valid]);
return $resultJson;

}
}
3 changes: 2 additions & 1 deletion view/frontend/templates/catcher.phtml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<div id="monkey_campaign" style="display:none;" data-mage-init='{"campaigncatcher":{}}'>
<div id="monkey_campaign" style="display:none;"
data-mage-init='{"campaigncatcher":{"checkCampaignUrl": "<?php echo $this->getUrl('mailchimp/campaign/check');?>"}}'>
</div>
26 changes: 22 additions & 4 deletions view/frontend/web/js/campaigncatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,25 @@
*/
define(
[
'jquery',
'mage/cookies'
'jquery'
],
function ($) {
"use strict";

$.widget('mage.campaigncatcher', {
"options": {
"checkCampaignUrl": ""
},

_init: function () {
var self = this;
$(document).ready(function () {
var path = location;
var urlparams = null;
var isGet = path.search.search('&');
var mc_cid = null;
var isMailchimp = false;
var checkCampaignUrl = self.options.checkCampaignUrl;
if(isGet > 0) {
urlparams = path.search.substr(1).split('&');
for (var i = 0; i < urlparams.length; i++) {
Expand Down Expand Up @@ -58,8 +63,21 @@ define(
}
}
if (mc_cid && !isMailchimp) {
$.mage.cookies.set('mailchimp_campaign_id' , mc_cid);
$.mage.cookies.set('mailchimp_landing_page', location);
$.ajax({
url: checkCampaignUrl,
data: {'form_key': window.FORM_KEY,'mc_cid': mc_cid},
type: 'GET',
dataType: 'json',
showLoader: false
}).done(function (data) {
if (data.valid==0) {
$.mage.cookies.clear('mailchimp_campaign_id');
$.mage.cookies.set('mailchimp_landing_page', location);
} else if (data.valid==1) {
$.mage.cookies.set('mailchimp_campaign_id' , mc_cid);
$.mage.cookies.set('mailchimp_landing_page', location);
}
});
}

if (isMailchimp) {
Expand Down

0 comments on commit a6502d1

Please sign in to comment.