Skip to content

Commit

Permalink
Merge branch 'develop' into webhook-error-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Santiagoebizmarts authored Jul 25, 2017
2 parents 39d027a + 27df18f commit cdf991c
Show file tree
Hide file tree
Showing 11 changed files with 337 additions and 16 deletions.
123 changes: 123 additions & 0 deletions app/code/community/Ebizmarts/MailChimp/Block/Checkout/Subscribe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

/**
* Checkout subscribe checkbox block renderer
*
* @category Ebizmarts
* @package Ebizmarts_MageMonkey
* @author Ebizmarts Team <[email protected]>
* @license http://opensource.org/licenses/osl-3.0.php
*/
class Ebizmarts_MailChimp_Block_Checkout_Subscribe extends Mage_Core_Block_Template
{

protected $_lists = array();
protected $_info = array();
protected $_myLists = array();
protected $_generalList = array();
protected $_form;
protected $_api;
protected $helper;
protected $storeId;

public function __construct()
{
parent::__construct();
$this->helper = Mage::helper('mailchimp');
$this->storeId = Mage::app()->getStore()->getId();
}

/**
* Render block HTML
*
* @return string
*/
protected function _toHtml()
{
$helper = $this->helper;
$storeId = $this->storeId;

$alreadySubscribed = Mage::getModel('newsletter/subscriber')
->loadByEmail($this->getQuote()->getCustomerEmail())
->isSubscribed();

if ($helper->isCheckoutSubscribeEnabled($storeId) && !$alreadySubscribed) {
return parent::_toHtml();
} else {
return '';
}
}

/**
* Retrieve current quote object from session
*
* @return Mage_Sales_Model_Quote
*/
protected function getQuote()
{
return Mage::getSingleton('checkout/session')
->getQuote();
}

protected function getCurrentCheckoutSubscribeValue()
{
return $this->helper->getCheckoutSubscribeValue($this->storeId);
}

protected function isForceHidden($currentValue = null)
{
if (!$currentValue) {
$currentValue = $this->getCurrentCheckoutSubscribeValue();
}
return ($currentValue == Ebizmarts_MailChimp_Model_System_Config_Source_Checkoutsubscribe::FORCE_HIDDEN);
}

protected function isForceVisible($currentValue)
{
return ($currentValue == Ebizmarts_MailChimp_Model_System_Config_Source_Checkoutsubscribe::FORCE_VISIBLE);
}

protected function isCheckedByDefault($currentValue)
{
return ($currentValue == Ebizmarts_MailChimp_Model_System_Config_Source_Checkoutsubscribe::CHECKED_BY_DEFAULT);
}

public function isForceEnabled()
{
$currentValue = $this->getCurrentCheckoutSubscribeValue();
if ($this->isForceHidden($currentValue) || $this->isForceVisible($currentValue)) {
return true;
}
return false;
}

public function isChecked()
{
$currentValue = $this->getCurrentCheckoutSubscribeValue();
if ($this->isCheckedByDefault($currentValue) || $this->isForceVisible($currentValue)) {
return true;
}
return false;
}

public function addToPostOnLoad()
{
return ($this->isChecked() || $this->isForceHidden());
}

/**
* Get list data from MC
*
* @return array
*/
public function getGeneralList()
{
$storeId = $this->storeId;
$helper = $this->helper;
$listId = $helper->getGeneralList($storeId);

//@Todo add support for intetest groups

return $listId;
}
}
10 changes: 10 additions & 0 deletions app/code/community/Ebizmarts/MailChimp/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -1872,4 +1872,14 @@ protected function getApiStores()
{
return Mage::getModel('mailchimp/api_stores');
}

public function getCheckoutSubscribeValue($scopeId, $scope = 'stores')
{
return $this->getConfigValueForScope(Ebizmarts_MailChimp_Model_Config::GENERAL_CHECKOUT_SUBSCRIBE, $scopeId, $scope);
}

public function isCheckoutSubscribeEnabled($scopeId, $scope = 'stores')
{
return ($this->isMailChimpEnabled($scopeId, $scope) && $this->getCheckoutSubscribeValue($scopeId, $scope) != Ebizmarts_MailChimp_Model_System_Config_Source_Checkoutsubscribe::DISABLED);
}
}
1 change: 1 addition & 0 deletions app/code/community/Ebizmarts/MailChimp/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Ebizmarts_MailChimp_Model_Config
const GENERAL_LIST = 'mailchimp/general/list';
const GENERAL_OLD_LIST = 'mailchimp/general/old_list';
const GENERAL_LIST_CHANGED_SCOPES = 'mailchimp/general/list_changed_scopes';
const GENERAL_CHECKOUT_SUBSCRIBE = 'mailchimp/general/checkout_subscribe';
const GENERAL_MCSTOREID = 'mailchimp/general/storeid';
const GENERAL_MCISSYNCING = 'mailchimp/general/is_syicing';
const GENERAL_ECOMMMINSYNCDATEFLAG = 'mailchimp/general/mcminsyncdateflag';
Expand Down
10 changes: 10 additions & 0 deletions app/code/community/Ebizmarts/MailChimp/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,16 @@ public function loadCustomerToQuote(Varien_Event_Observer $observer)
*/
public function newOrder(Varien_Event_Observer $observer)
{
$post = Mage::app()->getRequest()->getPost('mailchimp_subscribe');
$helper = $this->makeHelper();
if (isset($post)) {
$order = $observer->getEvent()->getOrder();
$email = $order->getCustomerEmail();
$subscriber = $helper->loadListSubscriber($post, $email);
if ($subscriber) {
Mage::getModel('mailchimp/processWebhook')->subscribeMember($subscriber);
}
}
if(($this->_getLandingCookie())) {
Mage::getModel('core/cookie')->delete('mailchimp_landing_page');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ protected function _subscribe(array $data)
}
}

protected function subscribeMember($subscriber)
public function subscribeMember($subscriber)
{
$subscriber->setStatus(Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED);
$subscriber->setSubscriberConfirmCode($subscriber->randomSequence());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* Checkout subscribe available status options source
*
* @category Ebizmarts
* @package Ebizmarts_MailChimp
* @author Ebizmarts Team <[email protected]>
* @license http://opensource.org/licenses/osl-3.0.php
*/
class Ebizmarts_MailChimp_Model_System_Config_Source_Checkoutsubscribe
{
const DISABLED = 0;
const CHECKED_BY_DEFAULT = 1;
const NOT_CHECKED_BY_DEFAULT = 2;
const FORCE_HIDDEN = 3;
const FORCE_VISIBLE = 4;

/**
* Options getter
*
* @return array
*/
public function toOptionArray()
{
$helper = Mage::helper('mailchimp');
return array(
array('value' => self::CHECKED_BY_DEFAULT, 'label' => $helper->__('Enabled - Checked by default')),
array('value' => self::NOT_CHECKED_BY_DEFAULT, 'label' => $helper->__('Enabled - Not Checked by default')),
array('value' => self::FORCE_HIDDEN, 'label' => $helper->__('Enabled - Force subscription hidden')),
array('value' => self::FORCE_VISIBLE, 'label' => $helper->__('Enabled - Force subscription')),
array('value' => self::DISABLED, 'label' => $helper->__('-- Disabled --'))
);
}
}
10 changes: 10 additions & 0 deletions app/code/community/Ebizmarts/MailChimp/etc/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@
<show_in_store>1</show_in_store>
<comment><![CDATA[Warning, read the manual before change the list.]]></comment>
</reset_list>
<checkout_subscribe translate="label comment">
<label>Subscribe On Checkout</label>
<frontend_type>select</frontend_type>
<source_model>mailchimp/system_config_source_checkoutsubscribe</source_model>
<sort_order>53</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<comment><![CDATA[Show Newsletter Subscribe checkbox in the last Checkout Step (Order Review).]]></comment>
</checkout_subscribe>
<map_fields translate="label comment">
<label>Customer Fields Mapping</label>
<frontend_model>mailchimp/adminhtml_system_config_form_field_mapfields</frontend_model>
Expand Down
14 changes: 13 additions & 1 deletion app/design/frontend/base/default/layout/ebizmarts/mailchimp.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0"?>

<layout version="0.1.0">
<!-- Everywhere -->
<default>
<reference name="head">
<action method="addJs" ifconfig="mailchimp/general/active">
Expand All @@ -21,12 +22,23 @@
<script>ebizmarts/mailchimp/popup.css</script>
</action>
<block type="core/text" name="addMCJs">
<action method="setText"><text helper="mailchimp/getMCJs"></text></action>
<action method="setText">
<text helper="mailchimp/getMCJs"></text>
</action>
</block>
</reference>
<reference name="content">
<block type="mailchimp/popup_emailcatcher" name="emailcatcher"
template="ebizmarts/mailchimp/popup/emailcatcher.phtml"></block>
</reference>
</default>
<!-- Everywhere -->

<!-- Checkout -->
<checkout_onepage_review>
<reference name="checkout.onepage.review.info.items.after">
<block type="mailchimp/checkout_subscribe" name="mailchimp.subscribe" template="ebizmarts/mailchimp/checkout/subscribe.phtml"/>
</reference>
</checkout_onepage_review>
<!-- Checkout -->
</layout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
$force = $this->isForceEnabled();
$check = $this->isChecked();
$hidden = $this->isForceHidden();
$addToPostOnLoad = $this->addToPostOnLoad();
$generalList = $this->getGeneralList();
?>
<script type="text/javascript">
addSubscribeToPost = function (element) {

//save old clicked lists and then removes element
var subscribeValue = '';
var checkedLists = '';
<?php if (!$force) : ?>
if ($('mailchimp-subscribe')) {
checkedLists = $('mailchimp-subscribe').getValue();
subscribeValue = checkedLists + ',';
$('mailchimp-subscribe').remove();
}
<?php endif; ?>

var hidden = false;
<?php if ($hidden) : ?>
hidden = true;
<?php endif; ?>
//if checked add this element else remove it
if (element.checked || hidden) {
var inputer = new Element('input', {
name: "mailchimp_subscribe",
id: "mailchimp-subscribe",
value: subscribeValue + element.readAttribute('value'),
type: "hidden"
});
// var listValue = new Element('input', {
// name: element.readAttribute('name'),
// id: "subscribe-" + element.readAttribute('value'),
// value: element.readAttribute('value'),
// type: "hidden"
// });
try {
Element.insert(Form.findFirstElement(payment.form), inputer);
// Element.insert(Form.findFirstElement(payment.form), listValue);

} catch (notelem) {
$("co-payment-form").insert(inputer);
// $("co-payment-form").insert(listValue);
}
} else {
alert(element.readAttribute('value'));
var arrCheckedLists = checkedLists.split(',');
var pos = arrCheckedLists.indexOf(element.readAttribute('value'));
alert(pos);
if (pos != -1) {
arrCheckedLists.splice(pos, 1);
checkedLists = arrCheckedLists.join(',');
var inputer = new Element('input', {
name: "mailchimp_subscribe",
id: "mailchimp-subscribe",
value: checkedLists,
type: "hidden"
});
if (inputer.value) {
try {
Element.insert(Form.findFirstElement(payment.form), inputer);
} catch (notelem) {
$("co-payment-form").insert(inputer);
}
}
}
}

};
</script>
<div class="buttons-set"<?php if ($hidden) : ?> style="display:none;"<?php endif; ?>>
<!-- General Subscription -->
<div class="page-title">
<h1><?php echo $this->__('Newsletter Subscription'); ?></h1>
</div>
<?php echo $this->getBlockHtml('formkey'); ?>
<div class="mailchimp-multisubscribe">
<ul class="mailchimp-general-list">
<li class="listdata">
<input<?php if ($check): ?> checked="checked"<?php endif; ?> type="checkbox"
onchange="addSubscribeToPost(this);"
name="list[<?php echo $generalList; ?>][subscribed]"
id="mailchimp-trigger"
value="<?php echo $generalList; ?>"
title="<?php echo $generalList; ?>"
class="mailchimp-list-subscriber"/>
<label style="padding-left: 6px;"
for="mailchimp-trigger"><?php echo $this->__('General Subscription'); ?></label>
</li>
</ul>
</div>
<!-- General Subscription -->
</div>

<script type="text/javascript">
//If force subscription or checked by default set the elements as clicked
<?php if($addToPostOnLoad):?>addSubscribeToPost($('mailchimp-trigger'));
<?php endif; ?>
</script>
Loading

0 comments on commit cdf991c

Please sign in to comment.