From 1da8d3ed410a3295f3b2fb6c6d5d0f639b0f84f6 Mon Sep 17 00:00:00 2001 From: Santiago Date: Tue, 25 Jul 2017 13:52:53 -0300 Subject: [PATCH] Added checkout subscription. resolves #405 --- .../MailChimp/Block/Checkout/Subscribe.php | 123 ++++++++++++++++++ .../Ebizmarts/MailChimp/Helper/Data.php | 10 ++ .../Ebizmarts/MailChimp/Model/Config.php | 1 + .../Ebizmarts/MailChimp/Model/Observer.php | 10 ++ .../MailChimp/Model/ProcessWebhook.php | 2 +- .../Config/Source/Checkoutsubscribe.php | 35 +++++ .../Ebizmarts/MailChimp/etc/system.xml | 10 ++ .../default/layout/ebizmarts/mailchimp.xml | 14 +- .../mailchimp/checkout/subscribe.phtml | 102 +++++++++++++++ .../Ebizmarts/MailChimp/Helper/DataTest.php | 36 +++-- .../MailChimp/Model/ObserverTest.php | 10 +- 11 files changed, 337 insertions(+), 16 deletions(-) create mode 100644 app/code/community/Ebizmarts/MailChimp/Block/Checkout/Subscribe.php create mode 100644 app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/Checkoutsubscribe.php create mode 100644 app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/subscribe.phtml diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Subscribe.php b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Subscribe.php new file mode 100644 index 000000000..96e519d1f --- /dev/null +++ b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Subscribe.php @@ -0,0 +1,123 @@ + + * @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; + } +} diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index 762e58a55..106145d14 100755 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -1866,4 +1866,14 @@ public function addEntriesToArray($batchArray, $productData, $counter) } return array($batchArray, $counter); } + + 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); + } } diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Config.php b/app/code/community/Ebizmarts/MailChimp/Model/Config.php index 950ba00c1..350d40292 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Config.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Config.php @@ -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'; diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index 12544ba4b..e48759063 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -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'); } diff --git a/app/code/community/Ebizmarts/MailChimp/Model/ProcessWebhook.php b/app/code/community/Ebizmarts/MailChimp/Model/ProcessWebhook.php index 21474a9fd..fd82bca27 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/ProcessWebhook.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/ProcessWebhook.php @@ -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()); diff --git a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/Checkoutsubscribe.php b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/Checkoutsubscribe.php new file mode 100644 index 000000000..c075a0081 --- /dev/null +++ b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/Checkoutsubscribe.php @@ -0,0 +1,35 @@ + + * @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 --')) + ); + } +} \ No newline at end of file diff --git a/app/code/community/Ebizmarts/MailChimp/etc/system.xml b/app/code/community/Ebizmarts/MailChimp/etc/system.xml index 08818721e..3a92dc9cb 100755 --- a/app/code/community/Ebizmarts/MailChimp/etc/system.xml +++ b/app/code/community/Ebizmarts/MailChimp/etc/system.xml @@ -112,6 +112,16 @@ 1 + + + select + mailchimp/system_config_source_checkoutsubscribe + 53 + 1 + 1 + 1 + + mailchimp/adminhtml_system_config_form_field_mapfields diff --git a/app/design/frontend/base/default/layout/ebizmarts/mailchimp.xml b/app/design/frontend/base/default/layout/ebizmarts/mailchimp.xml index 0a37e2ba7..8ea478abc 100755 --- a/app/design/frontend/base/default/layout/ebizmarts/mailchimp.xml +++ b/app/design/frontend/base/default/layout/ebizmarts/mailchimp.xml @@ -1,6 +1,7 @@ + @@ -21,7 +22,9 @@ - + + + @@ -29,4 +32,13 @@ template="ebizmarts/mailchimp/popup/emailcatcher.phtml"> + + + + + + + + + diff --git a/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/subscribe.phtml b/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/subscribe.phtml new file mode 100644 index 000000000..0e6d16c81 --- /dev/null +++ b/app/design/frontend/base/default/template/ebizmarts/mailchimp/checkout/subscribe.phtml @@ -0,0 +1,102 @@ +isForceEnabled(); +$check = $this->isChecked(); +$hidden = $this->isForceHidden(); +$addToPostOnLoad = $this->addToPostOnLoad(); +$generalList = $this->getGeneralList(); +?> + +
style="display:none;"> + +
+

__('Newsletter Subscription'); ?>

+
+ getBlockHtml('formkey'); ?> +
+
    +
  • + checked="checked" type="checkbox" + onchange="addSubscribeToPost(this);" + name="list[][subscribed]" + id="mailchimp-trigger" + value="" + title="" + class="mailchimp-list-subscriber"/> + +
  • +
+
+ +
+ + diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php index eccf146df..fa94208e3 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Helper/DataTest.php @@ -11,8 +11,8 @@ public function setUp() public function testGetLastDateOfPurchase() { /** - * @var \Ebizmarts_MailChimp_Helper_Data $helperMock -*/ + * @var \Ebizmarts_MailChimp_Helper_Data $helperMock + */ $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) ->disableOriginalConstructor() ->setMethods(array('getOrderCollectionByCustomerEmail')) @@ -27,22 +27,40 @@ public function testGetLastDateOfPurchase() public function testCustomMergeFieldAlreadyExists() { /** - * @var \Ebizmarts_MailChimp_Helper_Data $helperMock -*/ + * @var \Ebizmarts_MailChimp_Helper_Data $helperMock + */ $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) ->disableOriginalConstructor() ->setMethods(array('getCustomMergeFields')) ->getMock(); - $helperMock->expects($this->once())->method('getCustomMergeFields')->with(0, "store") + $helperMock->expects($this->once())->method('getCustomMergeFields')->with(0, "default") ->willReturn( array( - array( - "value" => "FNAME" - ) + array( + "value" => "FNAME" + ) ) ); - $this->assertTrue($helperMock->customMergeFieldAlreadyExists("FNAME", 0, "store")); + $this->assertTrue($helperMock->customMergeFieldAlreadyExists("FNAME", 0, "default")); + } + + public function testIsCheckoutSubscribeEnabled() + { + /** + * @var \Ebizmarts_MailChimp_Helper_Data $helperMock + */ + $helperMock = $this->getMockBuilder(Ebizmarts_MailChimp_Helper_Data::class) + ->disableOriginalConstructor() + ->setMethods(array('isMailChimpEnabled', 'getCheckoutSubscribeValue')) + ->getMock(); + $helperMock->expects($this->once())->method('isMailChimpEnabled')->with(1, 'stores') + ->willReturn(true); + + $helperMock->expects($this->once())->method('getCheckoutSubscribeValue')->with(1, 'stores') + ->willReturn(Ebizmarts_MailChimp_Model_System_Config_Source_Checkoutsubscribe::NOT_CHECKED_BY_DEFAULT); + + $this->assertTrue($helperMock->isCheckoutSubscribeEnabled(1, "stores")); } } \ No newline at end of file diff --git a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php index 97e7de9a3..be3c809cb 100644 --- a/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php +++ b/dev/tests/mailchimp/tests/app/Ebizmarts/MailChimp/Model/ObserverTest.php @@ -10,8 +10,8 @@ public function setUp() public function testProductAttributeUpdateIsUsingCorrectStoreId() { /** - * @var \Ebizmarts_MailChimp_Model_Observer $modelMock -*/ + * @var \Ebizmarts_MailChimp_Model_Observer $modelMock + */ $modelMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Observer::class) ->disableOriginalConstructor() ->setMethods(array('changeStoreName', 'makeHelper', 'makeApiProducts')) @@ -30,7 +30,7 @@ public function testProductAttributeUpdateIsUsingCorrectStoreId() ->disableOriginalConstructor() ->setMethods(array('getProductIds', 'getStoreId')) ->getMock(); - $eventMock->expects($this->once())->method('getProductIds')->willReturn(array(12,34)); + $eventMock->expects($this->once())->method('getProductIds')->willReturn(array(12, 34)); $eventMock->expects($this->once())->method('getStoreId')->willReturn(0); $eventObserverMock = $this->makeEventObserverMock($eventMock, 2); @@ -45,8 +45,8 @@ public function testProductAttributeUpdateIsUsingCorrectStoreId() public function testSaveCampaignDataCallsCorrectFunctions() { /** - * @var \Ebizmarts_MailChimp_Model_Observer $modelMock -*/ + * @var \Ebizmarts_MailChimp_Model_Observer $modelMock + */ $modelMock = $this->getMockBuilder(Ebizmarts_MailChimp_Model_Observer::class) ->disableOriginalConstructor() ->setMethods(array("_getCampaignCookie", "_getLandingCookie"))