time-dependent-shopping-cart-price-rules - Version 1.0.0

Version Notes

Time dependent Shopping cart price rules

Download this release

Release Info

Developer Orange35
Extension time-dependent-shopping-cart-price-rules
Version 1.0.0
Comparing to
See all releases


Version 1.0.0

app/code/community/Orange35/SalesruleTime/Model/Observer.php ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Orange35_SalesruleTime_Model_Observer{
3
+ public function adminhtmlPromoQuoteEditTabMainPrepareForm($observer){
4
+ $form = $observer->getData("form");
5
+ $fs = $form->getElement("base_fieldset");
6
+ $dateFormatIso = Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
7
+ foreach($fs->getElements() as $element){
8
+ if($element->getName() == "to_date" || $element->getName() == "from_date"){
9
+ $element->setData("input_format", Varien_Date::DATETIME_INTERNAL_FORMAT);
10
+ $element->setData("format", $dateFormatIso);
11
+ $element->setData("time", true);
12
+ }
13
+ }
14
+ $model = Mage::registry('current_promo_quote_rule');
15
+ $form->setValues($model->getData());
16
+ }
17
+ }
app/code/community/Orange35/SalesruleTime/Model/Resource/SalesRule/Rule/Collection.php ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Orange35_SalesruleTime_Model_Resource_SalesRule_Rule_Collection extends Mage_SalesRule_Model_Resource_Rule_Collection {
3
+ public function addWebsiteGroupDateFilter($websiteId, $customerGroupId, $now = null)
4
+ {
5
+ if (!$this->getFlag('website_group_date_filter')) {
6
+ if (is_null($now)) {
7
+ $now = Mage::getModel('core/date')->date('Y-m-d H:i:s');
8
+ }
9
+
10
+ $this->addWebsiteFilter($websiteId);
11
+
12
+ $entityInfo = $this->_getAssociatedEntityInfo('customer_group');
13
+ $connection = $this->getConnection();
14
+ $this->getSelect()
15
+ ->joinInner(
16
+ array('customer_group_ids' => $this->getTable($entityInfo['associations_table'])),
17
+ $connection->quoteInto(
18
+ 'main_table.' . $entityInfo['rule_id_field']
19
+ . ' = customer_group_ids.' . $entityInfo['rule_id_field']
20
+ . ' AND customer_group_ids.' . $entityInfo['entity_id_field'] . ' = ?',
21
+ (int)$customerGroupId
22
+ ),
23
+ array()
24
+ )
25
+ ->where('from_date is null or from_date <= ?', $now)
26
+ ->where('to_date is null or to_date >= ?', $now);
27
+
28
+ $this->addIsActiveFilter();
29
+
30
+ $this->setFlag('website_group_date_filter', true);
31
+ }
32
+
33
+ return $this;
34
+ }
35
+ }
app/code/community/Orange35/SalesruleTime/Model/Rule/Rule.php ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Orange35_SalesruleTime_Model_Rule_Rule extends Mage_Rule_Model_Rule {
3
+ protected function _convertFlatToRecursive(array $data)
4
+ {
5
+ $arr = array();
6
+ foreach ($data as $key => $value) {
7
+ if (($key === 'conditions' || $key === 'actions') && is_array($value)) {
8
+ foreach ($value as $id=>$data) {
9
+ $path = explode('--', $id);
10
+ $node =& $arr;
11
+ for ($i=0, $l=sizeof($path); $i<$l; $i++) {
12
+ if (!isset($node[$key][$path[$i]])) {
13
+ $node[$key][$path[$i]] = array();
14
+ }
15
+ $node =& $node[$key][$path[$i]];
16
+ }
17
+ foreach ($data as $k => $v) {
18
+ $node[$k] = $v;
19
+ }
20
+ }
21
+ } else {
22
+ /**
23
+ * Convert dates into Zend_Date
24
+ */
25
+ if (in_array($key, array('from_date', 'to_date')) && $value) {
26
+ $value = Mage::app()->getLocale()->date(
27
+ $value,
28
+ Varien_Date::DATETIME_INTERNAL_FORMAT,
29
+ null,
30
+ false
31
+ );
32
+ }
33
+ $this->setData($key, $value);
34
+ }
35
+ }
36
+
37
+ return $arr;
38
+ }
39
+ }
app/code/community/Orange35/SalesruleTime/Model/SalesRule/Rule.php ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Orange35_SalesruleTime_Model_SalesRule_Rule extends Mage_SalesRule_Model_Rule {
3
+ protected function _convertFlatToRecursive(array $data)
4
+ {
5
+ $arr = array();
6
+ foreach ($data as $key => $value) {
7
+ if (($key === 'conditions' || $key === 'actions') && is_array($value)) {
8
+ foreach ($value as $id=>$data) {
9
+ $path = explode('--', $id);
10
+ $node =& $arr;
11
+ for ($i=0, $l=sizeof($path); $i<$l; $i++) {
12
+ if (!isset($node[$key][$path[$i]])) {
13
+ $node[$key][$path[$i]] = array();
14
+ }
15
+ $node =& $node[$key][$path[$i]];
16
+ }
17
+ foreach ($data as $k => $v) {
18
+ $node[$k] = $v;
19
+ }
20
+ }
21
+ } else {
22
+ /**
23
+ * Convert dates into Zend_Date
24
+ */
25
+ if (in_array($key, array('from_date', 'to_date')) && $value) {
26
+ $value = Mage::app()->getLocale()->date(
27
+ $value,
28
+ Varien_Date::DATETIME_INTERNAL_FORMAT,
29
+ null,
30
+ false
31
+ );
32
+ }
33
+ $this->setData($key, $value);
34
+ }
35
+ }
36
+
37
+ return $arr;
38
+ }
39
+ }
app/code/community/Orange35/SalesruleTime/controllers/Adminhtml/Promo/QuoteController.php ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ require_once Mage::getModuleDir('controllers', 'Mage_Adminhtml') . DS . 'Promo/QuoteController.php';
3
+ class Orange35_SalesruleTime_Adminhtml_Promo_QuoteController extends Mage_Adminhtml_Promo_QuoteController {
4
+
5
+ public function generateAction()
6
+ {
7
+ if (!$this->getRequest()->isAjax()) {
8
+ $this->_forward('noRoute');
9
+ return;
10
+ }
11
+ $result = array();
12
+ $this->_initRule();
13
+
14
+ /** @var $rule Mage_SalesRule_Model_Rule */
15
+ $rule = Mage::registry('current_promo_quote_rule');
16
+
17
+ if (!$rule->getId()) {
18
+ $result['error'] = Mage::helper('salesrule')->__('Rule is not defined');
19
+ } else {
20
+ try {
21
+ $data = $this->getRequest()->getParams();
22
+ if (!empty($data['to_date'])) {
23
+ $data = array_merge($data, $this->_filterDateTime($data, array('to_date')));
24
+ }
25
+
26
+ /** @var $generator Mage_SalesRule_Model_Coupon_Massgenerator */
27
+ $generator = $rule->getCouponMassGenerator();
28
+ if (!$generator->validateData($data)) {
29
+ $result['error'] = Mage::helper('salesrule')->__('Not valid data provided');
30
+ } else {
31
+ $generator->setData($data);
32
+ $generator->generatePool();
33
+ $generated = $generator->getGeneratedCount();
34
+ $this->_getSession()->addSuccess(Mage::helper('salesrule')->__('%s Coupon(s) have been generated', $generated));
35
+ $this->_initLayoutMessages('adminhtml/session');
36
+ $result['messages'] = $this->getLayout()->getMessagesBlock()->getGroupedHtml();
37
+ }
38
+ } catch (Mage_Core_Exception $e) {
39
+ $result['error'] = $e->getMessage();
40
+ } catch (Exception $e) {
41
+ $result['error'] = Mage::helper('salesrule')->__('An error occurred while generating coupons. Please review the log and try again.');
42
+ Mage::logException($e);
43
+ }
44
+ }
45
+ $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
46
+ }
47
+
48
+ public function saveAction()
49
+ {
50
+ if ($this->getRequest()->getPost()) {
51
+ try {
52
+ /** @var $model Mage_SalesRule_Model_Rule */
53
+ $model = Mage::getModel('salesrule/rule');
54
+ Mage::dispatchEvent(
55
+ 'adminhtml_controller_salesrule_prepare_save',
56
+ array('request' => $this->getRequest()));
57
+ $data = $this->getRequest()->getPost();
58
+ $data = $this->_filterDateTime($data, array('from_date', 'to_date'));
59
+ $id = $this->getRequest()->getParam('rule_id');
60
+ if ($id) {
61
+ $model->load($id);
62
+ if ($id != $model->getId()) {
63
+ Mage::throwException(Mage::helper('salesrule')->__('Wrong rule specified.'));
64
+ }
65
+ }
66
+
67
+ $session = Mage::getSingleton('adminhtml/session');
68
+
69
+ $validateResult = $model->validateData(new Varien_Object($data));
70
+ if ($validateResult !== true) {
71
+ foreach($validateResult as $errorMessage) {
72
+ $session->addError($errorMessage);
73
+ }
74
+ $session->setPageData($data);
75
+ $this->_redirect('*/*/edit', array('id'=>$model->getId()));
76
+ return;
77
+ }
78
+
79
+ if (isset($data['simple_action']) && $data['simple_action'] == 'by_percent'
80
+ && isset($data['discount_amount'])) {
81
+ $data['discount_amount'] = min(100,$data['discount_amount']);
82
+ }
83
+ if (isset($data['rule']['conditions'])) {
84
+ $data['conditions'] = $data['rule']['conditions'];
85
+ }
86
+ if (isset($data['rule']['actions'])) {
87
+ $data['actions'] = $data['rule']['actions'];
88
+ }
89
+ unset($data['rule']);
90
+ $model->loadPost($data);
91
+
92
+ $useAutoGeneration = (int)!empty($data['use_auto_generation']);
93
+ $model->setUseAutoGeneration($useAutoGeneration);
94
+
95
+ $session->setPageData($model->getData());
96
+
97
+ $model->save();
98
+ $session->addSuccess(Mage::helper('salesrule')->__('The rule has been saved.'));
99
+ $session->setPageData(false);
100
+ if ($this->getRequest()->getParam('back')) {
101
+ $this->_redirect('*/*/edit', array('id' => $model->getId()));
102
+ return;
103
+ }
104
+ $this->_redirect('*/*/');
105
+ return;
106
+ } catch (Mage_Core_Exception $e) {
107
+ $this->_getSession()->addError($e->getMessage());
108
+ $id = (int)$this->getRequest()->getParam('rule_id');
109
+ if (!empty($id)) {
110
+ $this->_redirect('*/*/edit', array('id' => $id));
111
+ } else {
112
+ $this->_redirect('*/*/new');
113
+ }
114
+ return;
115
+
116
+ } catch (Exception $e) {
117
+ $this->_getSession()->addError(
118
+ Mage::helper('catalogrule')->__('An error occurred while saving the rule data. Please review the log and try again.'));
119
+ Mage::logException($e);
120
+ Mage::getSingleton('adminhtml/session')->setPageData($data);
121
+ $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('rule_id')));
122
+ return;
123
+ }
124
+ }
125
+ $this->_redirect('*/*/');
126
+ }
127
+ }
app/code/community/Orange35/SalesruleTime/etc/config.xml ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Orange35_SalesruleTime>
5
+ <version>1.0.0</version>
6
+ </Orange35_SalesruleTime>
7
+ </modules>
8
+ <global>
9
+ <models>
10
+ <orange35_salesruletime>
11
+ <class>Orange35_SalesruleTime_Model</class>
12
+ <resourceModel>orange35_salesruletime_resource</resourceModel>
13
+ </orange35_salesruletime>
14
+ <orange35_salesruletime_resource>
15
+ <class>Orange35_SalesruleTime_Model_Resource</class>
16
+ </orange35_salesruletime_resource>
17
+ <salesrule>
18
+ <rewrite>
19
+ <rule>Orange35_SalesruleTime_Model_SalesRule_Rule</rule>
20
+ </rewrite>
21
+ </salesrule>
22
+ <rule>
23
+ <rewrite>
24
+ <rule>Orange35_SalesruleTime_Model_Rule_Rule</rule>
25
+ </rewrite>
26
+ </rule>
27
+ <salesrule_resource>
28
+ <rewrite>
29
+ <rule_collection>Orange35_SalesruleTime_Model_Resource_SalesRule_Rule_Collection</rule_collection>
30
+ </rewrite>
31
+ </salesrule_resource>
32
+ </models>
33
+ <blocks>
34
+ <orange35_salesruletime>
35
+ <class>Orange35_SalesruleTime_Block</class>
36
+ </orange35_salesruletime>
37
+ </blocks>
38
+ <resources>
39
+ <orange35_salesruleTime_setup>
40
+ <setup>
41
+ <module>Orange35_SalesruleTime</module>
42
+ <class>Mage_Catalog_Model_Resource_Setup</class>
43
+ </setup>
44
+ </orange35_salesruleTime_setup>
45
+ </resources>
46
+ </global>
47
+ <adminhtml>
48
+ <events>
49
+ <adminhtml_promo_quote_edit_tab_main_prepare_form>
50
+ <observers>
51
+ <orange35_salesruletime_quote_edit_tab_main_prepare_form>
52
+ <type>singleton</type>
53
+ <class>orange35_salesruletime/observer</class>
54
+ <method>adminhtmlPromoQuoteEditTabMainPrepareForm</method>
55
+ </orange35_salesruletime_quote_edit_tab_main_prepare_form>
56
+ </observers>
57
+ </adminhtml_promo_quote_edit_tab_main_prepare_form>
58
+ </events>
59
+ </adminhtml>
60
+ <admin>
61
+ <routers>
62
+ <adminhtml>
63
+ <args>
64
+ <modules>
65
+ <orange35_salesruletime before="Mage_Adminhtml">Orange35_SalesruleTime_Adminhtml</orange35_salesruletime>
66
+ </modules>
67
+ </args>
68
+ </adminhtml>
69
+ </routers>
70
+ </admin>
71
+ </config>
app/code/community/Orange35/SalesruleTime/sql/orange35_salesruleTime_setup/install-1.0.0.php ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
1
+ <?php
2
+ $installer = $this;
3
+ $installer->startSetup();
4
+
5
+ $adapter = $installer->getConnection();
6
+ $adapter->modifyColumn($installer->getTable('salesrule/rule'), "from_date", Varien_Db_Ddl_Table::TYPE_DATETIME);
7
+ $adapter->modifyColumn($installer->getTable('salesrule/rule'), "to_date", Varien_Db_Ddl_Table::TYPE_DATETIME);
8
+ $installer->endSetup();
app/etc/modules/Orange35_SalesruleTime.xml ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Orange35_SalesruleTime>
5
+ <active>true</active>
6
+ <codePool>community</codePool>
7
+ <depends>
8
+ <Mage_Rule />
9
+ <Mage_SalesRule />
10
+ </depends>
11
+ </Orange35_SalesruleTime>
12
+ </modules>
13
+ </config>
package.xml ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>time-dependent-shopping-cart-price-rules</name>
4
+ <version>1.0.0</version>
5
+ <stability>stable</stability>
6
+ <license>OSL</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>Time dependent Shopping cart price rules</summary>
10
+ <description>Time dependent Shopping cart price rules</description>
11
+ <notes>Time dependent Shopping cart price rules</notes>
12
+ <authors><author><name>Orange35</name><user>Orange35</user><email>support@orange35.com</email></author></authors>
13
+ <date>2015-02-17</date>
14
+ <time>15:41:18</time>
15
+ <contents><target name="mageetc"><dir name="modules"><file name="Orange35_SalesruleTime.xml" hash="4f18084f275960c82fa2d91cbd5641c2"/></dir></target><target name="magecommunity"><dir name="Orange35"><dir name="SalesruleTime"><dir name="Model"><file name="Observer.php" hash="688066f6312c42b5188c800b4d1e05df"/><dir name="Resource"><dir name="SalesRule"><dir name="Rule"><file name="Collection.php" hash="413bae8331063ec0eb59234b4e8ac653"/></dir></dir></dir><dir name="Rule"><file name="Rule.php" hash="1703742932c6a3a0c3ef835547b957bf"/></dir><dir name="SalesRule"><file name="Rule.php" hash="f95a9f7f1ae03bd2286a6d2a247ea97a"/></dir></dir><dir name="controllers"><dir name="Adminhtml"><dir name="Promo"><file name="QuoteController.php" hash="454768fcf1c4179b69a23fcf79b6dc20"/></dir></dir></dir><dir name="etc"><file name="config.xml" hash="34eef31e21d39db596af3a23646dbccc"/></dir><dir name="sql"><dir name="orange35_salesruleTime_setup"><file name="install-1.0.0.php" hash="8680618998944226375b96d8abaa6f92"/></dir></dir></dir></dir></target></contents>
16
+ <compatible/>
17
+ <dependencies><required><php><min>5.2.0</min><max>6.0.0</max></php></required></dependencies>
18
+ </package>