Activated_Newsletter_Coupon - Version 1.0.0

Version Notes

Initial release

Download this release

Release Info

Developer Activated Apps
Extension Activated_Newsletter_Coupon
Version 1.0.0
Comparing to
See all releases


Version 1.0.0

app/code/community/Activated/NewsletterCoupon/Helper/Data.php ADDED
@@ -0,0 +1,13 @@
1
+ <?php
2
+ class Activated_NewsletterCoupon_Helper_Data extends Mage_Core_Helper_Abstract
3
+ {
4
+ /**
5
+ * Check if module is enabled
6
+ */
7
+
8
+ public function isEnabled()
9
+ {
10
+ return (bool) Mage::getStoreConfigFlag('newslettercoupon/view/enabled');
11
+ }
12
+ }
13
+ ?>
app/code/community/Activated/NewsletterCoupon/Model/NewsletterCoupon.php ADDED
@@ -0,0 +1,25 @@
1
+ <?php
2
+ class Activated_NewsletterCoupon_Model_NewsletterCoupon
3
+ {
4
+ protected function _construct()
5
+ {
6
+ $this->_init('newslettercoupon/newslettercoupon');
7
+ }
8
+
9
+ public function toOptionArray()
10
+ {
11
+ return $this->createRuleArray();
12
+ }
13
+
14
+ private function createRuleArray()
15
+ {
16
+ $rules = Mage::getModel('salesrule/rule')->getCollection();
17
+ $array = array();
18
+
19
+ foreach ($rules as $rule) {
20
+ $array[] = array('value' => $rule->getRuleId(), 'label' => $rule->getName());
21
+ }
22
+
23
+ return $array;
24
+ }
25
+ }
app/code/community/Activated/NewsletterCoupon/Model/Subscriber.php ADDED
@@ -0,0 +1,149 @@
1
+ <?php
2
+ class Activated_NewsletterCoupon_Model_Subscriber extends Mage_Newsletter_Model_Subscriber
3
+ {
4
+ const XML_PATH_NEWSLETTERCOUPON_VIEW_TEMPLATE = 'newslettercoupon/view/template';
5
+ const XML_PATH_NEWSLETTERCOUPON_VIEW_CONFIRMTEMPLATE = 'newslettercoupon/view/confirmtemplate';
6
+ const XML_PATH_NEWSLETTERCOUPON_VIEW_IDENTITY = 'newslettercoupon/view/identity';
7
+ /**
8
+ * Subscribes by email
9
+ *
10
+ * @param string $email
11
+ * @throws Exception
12
+ * @return int
13
+ */
14
+ public function subscribe($email, $coupon_code = null)
15
+ {
16
+ $this->loadByEmail($email);
17
+ $customerSession = Mage::getSingleton('customer/session');
18
+
19
+ if(!$this->getId()) {
20
+ $this->setSubscriberConfirmCode($this->randomSequence());
21
+ }
22
+
23
+ $isConfirmNeed = (Mage::getStoreConfig(self::XML_PATH_CONFIRMATION_FLAG) == 1) ? true : false;
24
+ $isOwnSubscribes = false;
25
+ $ownerId = Mage::getModel('customer/customer')
26
+ ->setWebsiteId(Mage::app()->getStore()->getWebsiteId())
27
+ ->loadByEmail($email)
28
+ ->getId();
29
+ $isSubscribeOwnEmail = $customerSession->isLoggedIn() && $ownerId == $customerSession->getId();
30
+
31
+ if (!$this->getId() || $this->getStatus() == self::STATUS_UNSUBSCRIBED
32
+ || $this->getStatus() == self::STATUS_NOT_ACTIVE
33
+ ) {
34
+ if ($isConfirmNeed === true) {
35
+ // if user subscribes own login email - confirmation is not needed
36
+ $isOwnSubscribes = $isSubscribeOwnEmail;
37
+ if ($isOwnSubscribes == true){
38
+ $this->setStatus(self::STATUS_SUBSCRIBED);
39
+ } else {
40
+ $this->setStatus(self::STATUS_NOT_ACTIVE);
41
+ }
42
+ } else {
43
+ $this->setStatus(self::STATUS_SUBSCRIBED);
44
+ }
45
+ $this->setSubscriberEmail($email);
46
+ }
47
+
48
+ if ($isSubscribeOwnEmail) {
49
+ $this->setStoreId($customerSession->getCustomer()->getStoreId());
50
+ $this->setCustomerId($customerSession->getCustomerId());
51
+ } else {
52
+ $this->setStoreId(Mage::app()->getStore()->getId());
53
+ $this->setCustomerId(0);
54
+ }
55
+
56
+ $this->setIsStatusChanged(true);
57
+
58
+ try {
59
+ $this->save();
60
+ if ($isConfirmNeed === true
61
+ && $isOwnSubscribes === false
62
+ ) {
63
+ if (Mage::helper('newslettercoupon')->isEnabled() && $coupon_code != null) {
64
+ $this->sendConfirmationCouponEmail($coupon_code);
65
+ } else {
66
+ $this->sendConfirmationRequestEmail();
67
+ }
68
+ } else {
69
+ if (Mage::helper('newslettercoupon')->isEnabled() && $coupon_code != null) {
70
+ $this->sendCouponEmail($coupon_code);
71
+ } else {
72
+ $this->sendConfirmationSuccessEmail();
73
+ }
74
+ }
75
+
76
+ return $this->getStatus();
77
+ } catch (Exception $e) {
78
+ throw new Exception($e->getMessage());
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Sends out coupon success email
84
+ *
85
+ * @return Mage_Newsletter_Model_Subscriber
86
+ */
87
+ public function sendCouponEmail($coupon_code)
88
+ {
89
+ if ($this->getImportMode()) {
90
+ return $this;
91
+ }
92
+
93
+ if(!Mage::getStoreConfig(self::XML_PATH_NEWSLETTERCOUPON_VIEW_TEMPLATE)
94
+ || !Mage::getStoreConfig(self::XML_PATH_NEWSLETTERCOUPON_VIEW_IDENTITY)
95
+ ) {
96
+ return $this;
97
+ }
98
+
99
+ $translate = Mage::getSingleton('core/translate');
100
+ /* @var $translate Mage_Core_Model_Translate */
101
+ $translate->setTranslateInline(false);
102
+
103
+ $email = Mage::getModel('core/email_template');
104
+
105
+ $email->sendTransactional(
106
+ Mage::getStoreConfig(self::XML_PATH_NEWSLETTERCOUPON_VIEW_TEMPLATE),
107
+ Mage::getStoreConfig(self::XML_PATH_NEWSLETTERCOUPON_VIEW_IDENTITY),
108
+ $this->getEmail(),
109
+ $this->getName(),
110
+ array('subscriber'=>$this, 'coupon_code'=>$coupon_code)
111
+ );
112
+
113
+ $translate->setTranslateInline(true);
114
+
115
+ return $this;
116
+ }
117
+
118
+ public function sendConfirmationCouponEmail($coupon_code)
119
+ {
120
+ if ($this->getImportMode()) {
121
+ return $this;
122
+ }
123
+
124
+ if(!Mage::getStoreConfig(self::XML_PATH_NEWSLETTERCOUPON_VIEW_CONFIRMTEMPLATE)
125
+ || !Mage::getStoreConfig(self::XML_PATH_NEWSLETTERCOUPON_VIEW_IDENTITY)
126
+ ) {
127
+ return $this;
128
+ }
129
+
130
+ $translate = Mage::getSingleton('core/translate');
131
+ /* @var $translate Mage_Core_Model_Translate */
132
+ $translate->setTranslateInline(false);
133
+
134
+ $email = Mage::getModel('core/email_template');
135
+
136
+ $email->sendTransactional(
137
+ Mage::getStoreConfig(self::XML_PATH_NEWSLETTERCOUPON_VIEW_CONFIRMTEMPLATE),
138
+ Mage::getStoreConfig(self::XML_PATH_NEWSLETTERCOUPON_VIEW_IDENTITY),
139
+ $this->getEmail(),
140
+ $this->getName(),
141
+ array('subscriber'=>$this, 'coupon_code'=>$coupon_code)
142
+ );
143
+
144
+ $translate->setTranslateInline(true);
145
+
146
+ return $this;
147
+ }
148
+ }
149
+ ?>
app/code/community/Activated/NewsletterCoupon/controllers/SubscriberController.php ADDED
@@ -0,0 +1,73 @@
1
+ <?php
2
+ require_once("Mage/Newsletter/controllers/SubscriberController.php");
3
+
4
+ class Activated_NewsletterCoupon_SubscriberController extends Mage_Newsletter_SubscriberController
5
+ {
6
+ public function newAction()
7
+ {
8
+ if ($this->getRequest()->isPost() && $this->getRequest()->getPost('email')) {
9
+ $session = Mage::getSingleton('core/session');
10
+ $customerSession = Mage::getSingleton('customer/session');
11
+ $email = (string) $this->getRequest()->getPost('email');
12
+
13
+ // Coupon code generator
14
+ $ruleId = Mage::getStoreConfig('newslettercoupon/view/rule');
15
+ $rule = Mage::getModel('salesrule/rule')->load($ruleId);
16
+ $coupon = Mage::getModel('salesrule/coupon');
17
+ $enabled = Mage::helper('newslettercoupon')->isEnabled();
18
+ $ruleType = $rule->getUseAutoGeneration();
19
+
20
+ try {
21
+ if (!Zend_Validate::is($email, 'EmailAddress')) {
22
+ Mage::throwException($this->__('Please enter a valid email address.'));
23
+ }
24
+
25
+ if (Mage::getStoreConfig(Mage_Newsletter_Model_Subscriber::XML_PATH_ALLOW_GUEST_SUBSCRIBE_FLAG) != 1 &&
26
+ !$customerSession->isLoggedIn()) {
27
+ Mage::throwException($this->__('Sorry, but administrator denied subscription for guests. Please <a href="%s">register</a>.', Mage::helper('customer')->getRegisterUrl()));
28
+ }
29
+
30
+ $ownerId = Mage::getModel('customer/customer')
31
+ ->setWebsiteId(Mage::app()->getStore()->getWebsiteId())
32
+ ->loadByEmail($email)
33
+ ->getId();
34
+ if ($ownerId !== null && $ownerId != $customerSession->getId()) {
35
+ Mage::throwException($this->__('This email address is already assigned to another user.'));
36
+ }
37
+
38
+ // Generate and save coupon code
39
+ if ($enabled) {
40
+ if ($ruleType == 1) {
41
+ $generator = $rule->getCouponCodeGenerator();
42
+ $coupon_code = $generator->generateCode();
43
+ $coupon->setId(null)
44
+ ->setRuleId($ruleId)
45
+ ->setType(Mage_SalesRule_Helper_Coupon::COUPON_TYPE_SPECIFIC_AUTOGENERATED)
46
+ ->setCode($coupon_code)
47
+ ->save();
48
+
49
+ $coupon->save();
50
+ } else {
51
+ $coupon_code = $rule->getPrimaryCoupon()->getCode();
52
+ }
53
+ }
54
+
55
+ $status = Mage::getModel('newsletter/subscriber')->subscribe($email, $coupon_code);
56
+ if ($status == Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE) {
57
+ $session->addSuccess($this->__('Confirmation request has been sent.'));
58
+ }
59
+ else {
60
+ $session->addSuccess($this->__('Thank you for your subscription.'));
61
+ }
62
+ }
63
+ catch (Mage_Core_Exception $e) {
64
+ $session->addException($e, $this->__('There was a problem with the subscription: %s', $e->getMessage()));
65
+ }
66
+ catch (Exception $e) {
67
+ $session->addException($e, $this->__('There was a problem with the subscription.'));
68
+ }
69
+ }
70
+ $this->_redirectReferer();
71
+ }
72
+ }
73
+ ?>
app/code/community/Activated/NewsletterCoupon/etc/adminhtml.xml ADDED
@@ -0,0 +1,23 @@
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <acl>
4
+ <resources>
5
+ <admin>
6
+ <children>
7
+ <system>
8
+ <children>
9
+ <config>
10
+ <children>
11
+ <newslettercoupon>
12
+ <title>Newsletter Coupon</title>
13
+ </newslettercoupon>
14
+ </children>
15
+ </config>
16
+ </children>
17
+ </system>
18
+ </children>
19
+ </admin>
20
+ </resources>
21
+ </acl>
22
+ </config>
23
+
app/code/community/Activated/NewsletterCoupon/etc/config.xml ADDED
@@ -0,0 +1,62 @@
1
+ <?xml version="1.0" ?>
2
+ <config>
3
+ <modules>
4
+ <Activated_NewsletterCoupon>
5
+ <version>1.0.0</version>
6
+ </Activated_NewsletterCoupon>
7
+ </modules>
8
+ <global>
9
+ <models>
10
+ <newslettercoupon>
11
+ <class>Activated_NewsletterCoupon_Model</class>
12
+ </newslettercoupon>
13
+ <newsletter>
14
+ <rewrite>
15
+ <subscriber>Activated_NewsletterCoupon_Model_Subscriber</subscriber>
16
+ </rewrite>
17
+ </newsletter>
18
+ </models>
19
+ <helpers>
20
+ <newslettercoupon>
21
+ <class>Activated_NewsletterCoupon_Helper</class>
22
+ </newslettercoupon>
23
+ </helpers>
24
+ <template>
25
+ <email>
26
+ <newslettercoupon_view_template translate="label" module="newslettercoupon">
27
+ <label>Newsletter Coupon</label>
28
+ <file>newslettercoupon/newslettercoupon_view_template.html</file>
29
+ <type>html</type>
30
+ </newslettercoupon_view_template>
31
+ </email>
32
+ </template>
33
+ <template>
34
+ <email>
35
+ <newslettercoupon_view_confirmtemplate translate="label" module="newslettercoupon">
36
+ <label>Confirmation Newsletter Coupon</label>
37
+ <file>newslettercoupon/newslettercoupon_view_confirmtemplate.html</file>
38
+ <type>html</type>
39
+ </newslettercoupon_view_confirmtemplate>
40
+ </email>
41
+ </template>
42
+ </global>
43
+ <default>
44
+ <newslettercoupon>
45
+ <view>
46
+ <template>newsletter_coupon</template>
47
+ </view>
48
+ </newslettercoupon>
49
+ </default>
50
+ <frontend>
51
+ <routers>
52
+ <newsletter>
53
+ <use>standard</use>
54
+ <args>
55
+ <modules>
56
+ <Activated_NewsletterCoupon before="Mage_Newsletter">Activated_NewsletterCoupon</Activated_NewsletterCoupon>
57
+ </modules>
58
+ </args>
59
+ </newsletter>
60
+ </routers>
61
+ </frontend>
62
+ </config>
app/code/community/Activated/NewsletterCoupon/etc/system.xml ADDED
@@ -0,0 +1,78 @@
1
+ <?xml version="1.0" ?>
2
+ <config>
3
+ <tabs>
4
+ <activated_extensions translate="label">
5
+ <label>Activated Extensions</label>
6
+ <sort_order>100</sort_order>
7
+ </activated_extensions>
8
+ </tabs>
9
+ <sections>
10
+ <newslettercoupon>
11
+ <class>separator-top</class>
12
+ <label>Newsletter Coupon</label>
13
+ <tab>activated_extensions</tab>
14
+ <frontend_type>text</frontend_type>
15
+ <sort_order>500</sort_order>
16
+ <show_in_default>1</show_in_default>
17
+ <show_in_website>1</show_in_website>
18
+ <show_in_store>1</show_in_store>
19
+ <groups>
20
+ <view translate="label">
21
+ <label>General Settings</label>
22
+ <frontend_type>text</frontend_type>
23
+ <sort_order>10</sort_order>
24
+ <show_in_default>1</show_in_default>
25
+ <show_in_website>1</show_in_website>
26
+ <show_in_store>1</show_in_store>
27
+ <fields>
28
+ <enabled translate="label">
29
+ <label>Enabled</label>
30
+ <frontend_type>select</frontend_type>
31
+ <source_model>adminhtml/system_config_source_yesno</source_model>
32
+ <sort_order>10</sort_order>
33
+ <show_in_default>1</show_in_default>
34
+ <show_in_website>1</show_in_website>
35
+ <show_in_store>1</show_in_store>
36
+ </enabled>
37
+ <rule translate="label">
38
+ <label>Coupon</label>
39
+ <frontend_type>select</frontend_type>
40
+ <source_model>newslettercoupon/newslettercoupon</source_model>
41
+ <sort_order>10</sort_order>
42
+ <show_in_default>1</show_in_default>
43
+ <show_in_website>1</show_in_website>
44
+ <show_in_store>1</show_in_store>
45
+ </rule>
46
+ <template translate="label">
47
+ <label>Email Template</label>
48
+ <frontend_type>select</frontend_type>
49
+ <source_model>adminhtml/system_config_source_email_template</source_model>
50
+ <sort_order>20</sort_order>
51
+ <show_in_default>1</show_in_default>
52
+ <show_in_website>1</show_in_website>
53
+ <show_in_store>1</show_in_store>
54
+ </template>
55
+ <confirmtemplate translate="label">
56
+ <label>Confirmation Email Template</label>
57
+ <frontend_type>select</frontend_type>
58
+ <source_model>adminhtml/system_config_source_email_template</source_model>
59
+ <sort_order>20</sort_order>
60
+ <show_in_default>1</show_in_default>
61
+ <show_in_website>1</show_in_website>
62
+ <show_in_store>1</show_in_store>
63
+ </confirmtemplate>
64
+ <identity translate="label">
65
+ <label>Newsletter Coupon Sender</label>
66
+ <frontend_type>select</frontend_type>
67
+ <source_model>adminhtml/system_config_source_email_identity</source_model>
68
+ <sort_order>30</sort_order>
69
+ <show_in_default>1</show_in_default>
70
+ <show_in_website>1</show_in_website>
71
+ <show_in_store>1</show_in_store>
72
+ </identity>
73
+ </fields>
74
+ </view>
75
+ </groups>
76
+ </newslettercoupon>
77
+ </sections>
78
+ </config>
app/etc/modules/Activated_NewsletterCoupon.xml ADDED
@@ -0,0 +1,10 @@
1
+ <?xml version="1.0" ?>
2
+ <config>
3
+ <modules>
4
+ <Activated_NewsletterCoupon>
5
+ <active>true</active>
6
+ <codePool>community</codePool>
7
+ <version>1.0.0</version>
8
+ </Activated_NewsletterCoupon>
9
+ </modules>
10
+ </config>
app/locale/en_US/template/email/newslettercoupon/newslettercoupon_view_confirmtemplate.html ADDED
@@ -0,0 +1,45 @@
1
+ <!--@subject Newsletter subscription confirmation and coupon @-->
2
+ <!--@vars
3
+ {"store url=\"\"":"Store Url",
4
+ "var logo_url":"Email Logo Image Url",
5
+ "var logo_alt":"Email Logo Image Alt",
6
+ "htmlescape var=$customer.name":"Customer Name",
7
+ "var subscriber.getConfirmationLink()":"Subscriber Confirmation Url",
8
+ "var coupon_code":"Coupon Code"}
9
+ @-->
10
+ <!--@styles
11
+ body,td { color:#2f2f2f; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; }
12
+ @-->
13
+
14
+ <body style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">
15
+ <div style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">
16
+ <table cellspacing="0" cellpadding="0" border="0" height="100%" width="100%">
17
+ <tr>
18
+ <td align="center" valign="top" style="padding:20px 0 20px 0">
19
+ <!-- [ header starts here] -->
20
+ <table bgcolor="FFFFFF" cellspacing="0" cellpadding="10" border="0" width="650" style="border:1px solid #E0E0E0;">
21
+ <tr>
22
+ <td valign="top">
23
+ <a href="{{store url=""}}" style="color:#1E7EC8;"><img src="{{var logo_url}}" alt="{{var logo_alt}}" border="0"/></a>
24
+ </td>
25
+ </tr>
26
+ <!-- [ middle starts here] -->
27
+ <tr>
28
+ <td valign="top">
29
+ <h1 style="font-size:22px; font-weight:normal; line-height:22px; margin:0 0 11px 0;"">Thank you for subscribing to our newsletter.</h1>
30
+ <div style="border:1px solid #E0E0E0; font-size:12px; line-height:16px; margin:0; padding:13px 18px; background:#F9F9F9;">
31
+ <p>Your coupon code has arrived: <b>{{var coupon_code}}</b>.</p>
32
+ <p>To begin receiving the newsletter, you must first confirm your subscription by clicking on the following link:<br />
33
+ <a href="{{var subscriber.getConfirmationLink()}}" style="color:#1E7EC8;">{{var subscriber.getConfirmationLink()}}</a><p>
34
+ </div>
35
+ </td>
36
+ </tr>
37
+ <tr>
38
+ <td bgcolor="#EAEAEA" align="center" style="background:#EAEAEA; text-align:center;"><center><p style="font-size:12px; margin:0;">Thank you again, <strong>{{var store.getFrontendName()}}</strong></p></center></td>
39
+ </tr>
40
+ </table>
41
+ </td>
42
+ </tr>
43
+ </table>
44
+ </div>
45
+ </body>
app/locale/en_US/template/email/newslettercoupon/newslettercoupon_view_template.html ADDED
@@ -0,0 +1,8 @@
1
+ <!--@subject Newsletter subscription success and coupon @-->
2
+ <!--@vars
3
+ {"var coupon_code":"Coupon Code"}
4
+ @-->
5
+ <div>
6
+ <p>Newsletter subscription success</p>
7
+ <p>Your coupon code: {{var coupon_code}}</p>
8
+ </div>
package.xml ADDED
@@ -0,0 +1,18 @@
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>Activated_Newsletter_Coupon</name>
4
+ <version>1.0.0</version>
5
+ <stability>stable</stability>
6
+ <license>EULA</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>Send coupons on newsletter sign up.</summary>
10
+ <description>Using Activated Newsletter Coupon, you can create an incentive for your customers to sign up to your newsletter. Whether a static coupon or auto-generated, this module can handle it.</description>
11
+ <notes>Initial release</notes>
12
+ <authors><author><name>Activated Apps</name><user>Owner</user><email>admin@activatedapps.com</email></author></authors>
13
+ <date>2015-09-14</date>
14
+ <time>07:09:28</time>
15
+ <contents><target name="magecommunity"><dir name="Activated"><dir name="NewsletterCoupon"><dir name="Helper"><file name="Data.php" hash="0bb7b9adbc08ad3cdf4fa1c3dd27dd54"/></dir><dir name="Model"><file name="NewsletterCoupon.php" hash="0af5b77c98412eb9be5138abdb7b8037"/><file name="Subscriber.php" hash="0b9eb71bfabaefd2b06939a6723b98c0"/></dir><dir name="controllers"><file name="SubscriberController.php" hash="5721379c9a0cab1c0cf807baa9db43d7"/></dir><dir name="etc"><file name="adminhtml.xml" hash="85b6f9a442c568ffe80699a63e618d1d"/><file name="config.xml" hash="ef8c1d0b3e4e309ace626f9172bc504a"/><file name="system.xml" hash="7d7e8c2309f5c73906242db443954b1b"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Activated_NewsletterCoupon.xml" hash="8332639258432e7a5293e71902bd613c"/></dir></target><target name="magelocale"><dir name="en_US"><dir name="template"><dir name="email"><dir name="newslettercoupon"><file name="newslettercoupon_view_confirmtemplate.html" hash="f0e8df583ee9a8bc8fcfe3fe9515a3bc"/><file name="newslettercoupon_view_template.html" hash="d3b9d07b0d0b9f6fa08abea5a222a3a8"/></dir></dir></dir></dir></target></contents>
16
+ <compatible/>
17
+ <dependencies><required><php><min>5.4.0</min><max>5.6.12</max></php></required></dependencies>
18
+ </package>