AntonioLoiaconoDesign_CalculateDiscount - Version 1.0.0

Version Notes

Fixed minor bugs

Download this release

Release Info

Developer Antonio Loiacono Design
Extension AntonioLoiaconoDesign_CalculateDiscount
Version 1.0.0
Comparing to
See all releases


Version 1.0.0

app/code/community/AntonioLoiaconoDesign/CalculateDiscount/Block/Config/Adminhtml/Form/Field/Discount.php ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class AntonioLoiaconoDesign_CalculateDiscount_Block_Config_Adminhtml_Form_Field_Discount
3
+ extends Mage_Core_Block_Html_Select
4
+ {
5
+ public function _toHtml()
6
+ {
7
+ // specify the attribute code
8
+ $discount = Mage::getStoreConfig('antonioloiaconodesign_calculatediscount/general/attributeofdiscount');
9
+ $config = Mage::getModel('eav/config');
10
+ $attribute = $config->getAttribute(Mage_Catalog_Model_Product::ENTITY, $discount);
11
+ $options = $attribute->getSource()->getAllOptions();
12
+ foreach ($options as $option) {
13
+ $this->addOption($option['value'], $option['label']);
14
+ }
15
+ return parent::_toHtml();
16
+ }
17
+
18
+ public function setInputName($value)
19
+ {
20
+ return $this->setName($value);
21
+ }
22
+ }
app/code/community/AntonioLoiaconoDesign/CalculateDiscount/Block/Config/DiscountConditions.php ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class AntonioLoiaconoDesign_CalculateDiscount_Block_Config_DiscountConditions
3
+ extends Mage_Adminhtml_Block_System_Config_Form_Field_Array_Abstract
4
+ {
5
+ protected $_itemRenderer;
6
+
7
+ public function _prepareToRender()
8
+ {
9
+ $discount = ucfirst(str_replace('_',' ', Mage::getStoreConfig('antonioloiaconodesign_calculatediscount/general/attributeofdiscount')));
10
+ $this->addColumn('discount_option', array(
11
+ 'label' => Mage::helper('calculatediscount')->__(($discount != '' ? $discount . ' Option' : 'Attribute Option')),
12
+ 'renderer' => $this->_getRenderer(),
13
+ ));
14
+ $this->addColumn('from_percent', array(
15
+ 'label' => Mage::helper('calculatediscount')->__('From %'),
16
+ 'style' => 'width:100px',
17
+ ));
18
+ $this->addColumn('to_percent', array(
19
+ 'label' => Mage::helper('calculatediscount')->__('To %'),
20
+ 'style' => 'width:100px',
21
+ ));
22
+
23
+ $this->_addAfter = false;
24
+ $this->_addButtonLabel = Mage::helper('calculatediscount')->__('Add Condition');
25
+ }
26
+
27
+ protected function _getRenderer()
28
+ {
29
+ if (!$this->_itemRenderer) {
30
+ $this->_itemRenderer = $this->getLayout()->createBlock(
31
+ 'calculatediscount/config_adminhtml_form_field_discount', '',
32
+ array('is_render_to_js_template' => true)
33
+ );
34
+ }
35
+ return $this->_itemRenderer;
36
+ }
37
+
38
+ protected function _prepareArrayRow(Varien_Object $row)
39
+ {
40
+ $row->setData(
41
+ 'option_extra_attr_' . $this->_getRenderer()
42
+ ->calcOptionHash($row->getData('discount_option')),
43
+ 'selected="selected"'
44
+ );
45
+ }
46
+ }
app/code/community/AntonioLoiaconoDesign/CalculateDiscount/Helper/Data.php ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
1
+ <?php
2
+ class AntonioLoiaconoDesign_CalculateDiscount_Helper_Data extends Mage_Core_Helper_Abstract
3
+ {
4
+ }
5
+
app/code/community/AntonioLoiaconoDesign/CalculateDiscount/Model/Observer.php ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class AntonioLoiaconoDesign_CalculateDiscount_Model_Observer
4
+ {
5
+ /**
6
+ * Add a new massaction to export products to CSV
7
+ * @param Varien_Event_Observer $observer
8
+ */
9
+ public function addMassCalculate(Varien_Event_Observer $observer)
10
+ {
11
+ $block = $observer->getEvent()->getBlock();
12
+
13
+ $block->getMassactionBlock()->addItem('calculatediscount', array(
14
+ 'label' => 'Calculate Discount',
15
+ 'confirm' => $block->__('Are you sure you want to calculates discount of the selected listing(s)?'),
16
+ 'url' => $block->getUrl('adminhtml/calculatediscount/massCalculate', array(
17
+ 'store' => Mage::app()->getRequest()->getParam('store')
18
+ ))
19
+ ));
20
+ }
21
+
22
+ public function calculateDiscountAfterProductSave($observer)
23
+ {
24
+ $product = $observer->getProduct();
25
+ $discountAttribute = Mage::getStoreConfig('antonioloiaconodesign_calculatediscount/general/attributeofdiscount');
26
+
27
+ if ($product->getFinalPrice() < $product->getPrice()) {
28
+
29
+ $discountPer = floor(100 - ($product->getFinalPrice() / $product->getPrice()) * 100);
30
+ $discountConditions = Mage::getStoreConfig('antonioloiaconodesign_calculatediscount/general/discountconditions');
31
+ if ($discountConditions) {
32
+ $discountConditions = unserialize($discountConditions);
33
+ if (is_array($discountConditions)) {
34
+
35
+ foreach ($discountConditions as $discountConditionsRow) {
36
+ $discountOption = $discountConditionsRow['discount_option'];
37
+ $fromPercent = $discountConditionsRow['from_percent'];
38
+ $toPercent = $discountConditionsRow['to_percent'];
39
+
40
+ if (filter_var($discountPer, FILTER_VALIDATE_INT, array(
41
+ "options" => array(
42
+ "min_range" => $fromPercent,
43
+ "max_range" => $toPercent
44
+ )
45
+ )) === false) {
46
+ continue;
47
+ } else {
48
+ Mage::getSingleton('catalog/product_action')->updateAttributes(array(
49
+ $product->getId()
50
+ ), array(
51
+ $discountAttribute => $discountOption['value']
52
+ ), 0);
53
+ }
54
+
55
+ }
56
+ }
57
+
58
+ }
59
+
60
+ }else{
61
+ Mage::getSingleton('catalog/product_action')->updateAttributes(array(
62
+ $product->getId()
63
+ ), array(
64
+ $discountAttribute => ''
65
+ ), 0);
66
+
67
+ }
68
+
69
+ }
70
+
71
+ }
app/code/community/AntonioLoiaconoDesign/CalculateDiscount/controllers/Adminhtml/CalculatediscountController.php ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class AntonioLoiaconoDesign_CalculateDiscount_Adminhtml_CalculatediscountController extends Mage_Adminhtml_Controller_Action
3
+ {
4
+ protected function _isAllowed()
5
+ {
6
+ return Mage::getSingleton('admin/session')->isAllowed('catalog/products');
7
+ }
8
+
9
+ public function massCalculateAction()
10
+ {
11
+ $productIds = $this->getRequest()->getParam('product');
12
+ $store = $this->_getStore();
13
+ if (!is_array($productIds)) {
14
+ $this->_getSession()->addError($this->__('Please select product(s).'));
15
+ $this->_redirect('adminhtml/catalog_product/index');
16
+ } else {
17
+ try {
18
+ $collection = Mage::getResourceModel('catalog/product_collection')->addFieldToFilter('entity_id', array(
19
+ $productIds
20
+ ))->addAttributeToSelect('*');
21
+
22
+ foreach ($collection as $_product) {
23
+
24
+ $product = Mage::getModel('catalog/product')->load($_product->getId());
25
+ $discountAttribute = Mage::getStoreConfig('antonioloiaconodesign_calculatediscount/general/attributeofdiscount');
26
+
27
+ if ($product->getFinalPrice() < $product->getPrice()) {
28
+
29
+ $discountPer = floor(100 - ($product->getFinalPrice() / $product->getPrice()) * 100);
30
+ $discountConditions = Mage::getStoreConfig('antonioloiaconodesign_calculatediscount/general/discountconditions');
31
+ if ($discountConditions) {
32
+ $discountConditions = unserialize($discountConditions);
33
+ if (is_array($discountConditions)) {
34
+
35
+ foreach ($discountConditions as $discountConditionsRow) {
36
+ $discountOption = $discountConditionsRow['discount_option'];
37
+ $fromPercent = $discountConditionsRow['from_percent'];
38
+ $toPercent = $discountConditionsRow['to_percent'];
39
+
40
+ if (filter_var($discountPer, FILTER_VALIDATE_INT, array(
41
+ "options" => array(
42
+ "min_range" => $fromPercent,
43
+ "max_range" => $toPercent
44
+ )
45
+ )) === false) {
46
+ continue;
47
+ } else {
48
+ Mage::getSingleton('catalog/product_action')->updateAttributes(array(
49
+ $product->getId()
50
+ ), array(
51
+ $discountAttribute => $discountOption['value']
52
+ ), 0);
53
+ }
54
+
55
+ }
56
+ }
57
+
58
+ }
59
+
60
+ }else{
61
+ Mage::getSingleton('catalog/product_action')->updateAttributes(array(
62
+ $product->getId()
63
+ ), array(
64
+ $discountAttribute => ''
65
+ ), 0);
66
+
67
+ }
68
+
69
+ }
70
+
71
+ }
72
+ catch (Exception $e) {
73
+ $this->_getSession()->addError($e->getMessage());
74
+ $this->_redirect('adminhtml/catalog_product/index');
75
+ }
76
+
77
+ $this->_getSession()->addSuccess($this->__('It was calculated the discount of %d product(s).', count($collection)));
78
+ $this->_redirect('adminhtml/catalog_product/index');
79
+
80
+ }
81
+
82
+ }
83
+
84
+ protected function _getStore()
85
+ {
86
+ $storeId = (int) $this->getRequest()->getParam('store', 0);
87
+ return Mage::app()->getStore($storeId);
88
+ }
89
+ }
app/code/community/AntonioLoiaconoDesign/CalculateDiscount/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
+ <antonioloiaconodesign_calculatediscount translate="title" module="calculatediscount">
12
+ <title>AntonioLoiaconoDesign - CalculateDiscount</title>
13
+ <sort_order>0</sort_order>
14
+ </antonioloiaconodesign_calculatediscount>
15
+ </children>
16
+ </config>
17
+ </children>
18
+ </system>
19
+ </children>
20
+ </admin>
21
+ </resources>
22
+ </acl>
23
+ </config>
app/code/community/AntonioLoiaconoDesign/CalculateDiscount/etc/config.xml ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <config>
3
+ <modules>
4
+ <AntonioLoiaconoDesign_CalculateDiscount>
5
+ <version>1.0.0</version>
6
+ </AntonioLoiaconoDesign_CalculateDiscount>
7
+ </modules>
8
+ <global>
9
+ <blocks>
10
+ <calculatediscount>
11
+ <class>AntonioLoiaconoDesign_CalculateDiscount_Block</class>
12
+ </calculatediscount>
13
+ </blocks>
14
+ <models>
15
+ <calculatediscount>
16
+ <class>AntonioLoiaconoDesign_CalculateDiscount_Model</class>
17
+ </calculatediscount>
18
+ </models>
19
+ <helpers>
20
+ <calculatediscount>
21
+ <class>AntonioLoiaconoDesign_CalculateDiscount_Helper</class>
22
+ </calculatediscount>
23
+ </helpers>
24
+ </global>
25
+ <admin>
26
+ <routers>
27
+ <adminhtml>
28
+ <args>
29
+ <modules>
30
+ <AntonioLoiaconoDesign_CalculateDiscount before="Mage_Adminhtml">AntonioLoiaconoDesign_CalculateDiscount_Adminhtml</AntonioLoiaconoDesign_CalculateDiscount>
31
+ </modules>
32
+ </args>
33
+ </adminhtml>
34
+ </routers>
35
+ </admin>
36
+ <adminhtml>
37
+ <events>
38
+ <!-- event triggered after the original massaction items are added -->
39
+ <adminhtml_catalog_product_grid_prepare_massaction>
40
+ <observers>
41
+ <antonioloiaconodesign_calculatediscount_add>
42
+ <type>singleton</type>
43
+ <class>calculatediscount/observer</class>
44
+ <method>addMassCalculate</method>
45
+ </antonioloiaconodesign_calculatediscount_add>
46
+ </observers>
47
+ </adminhtml_catalog_product_grid_prepare_massaction>
48
+
49
+ <catalog_product_save_after>
50
+ <observers>
51
+ <calculatediscount>
52
+ <type>singleton</type>
53
+ <class>calculatediscount/observer</class>
54
+ <method>calculateDiscountAfterProductSave</method>
55
+ </calculatediscount>
56
+ </observers>
57
+ </catalog_product_save_after>
58
+
59
+ </events>
60
+ </adminhtml>
61
+ <phpunit>
62
+ <suite>
63
+ <modules>
64
+ <AntonioLoiaconoDesign_CalculateDiscount/>
65
+ </modules>
66
+ </suite>
67
+ </phpunit>
68
+ </config>
app/code/community/AntonioLoiaconoDesign/CalculateDiscount/etc/system.xml ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <config>
3
+ <tabs>
4
+ <antonioloiaconodesign translate="label" module="calculatediscount">
5
+ <label>ANTONIOLOIACONODESIGN.IT</label>
6
+ <sort_order>100</sort_order>
7
+ </antonioloiaconodesign>
8
+ </tabs>
9
+ <sections>
10
+ <antonioloiaconodesign_calculatediscount translate="label" module="calculatediscount">
11
+ <label>Calculate Discount</label>
12
+ <tab>antonioloiaconodesign</tab>
13
+ <sort_order>1000</sort_order>
14
+ <show_in_default>1</show_in_default>
15
+ <show_in_website>1</show_in_website>
16
+ <show_in_store>1</show_in_store>
17
+ <groups>
18
+ <general translate="label" module="calculatediscount">
19
+ <label>General</label>
20
+ <frontend_type>text</frontend_type>
21
+ <sort_order>1000</sort_order>
22
+ <show_in_default>1</show_in_default>
23
+ <show_in_website>1</show_in_website>
24
+ <show_in_store>1</show_in_store>
25
+ <fields>
26
+ <attributeofdiscount translate="label">
27
+ <label>Attribute of discount</label>
28
+ <comment>Write the name of the attribute used for the discount.</comment>
29
+ <frontend_type>text</frontend_type>
30
+ <sort_order>10</sort_order>
31
+ <show_in_default>1</show_in_default>
32
+ <show_in_website>1</show_in_website>
33
+ <show_in_store>1</show_in_store>
34
+ </attributeofdiscount>
35
+ <discountconditions translate="label">
36
+ <label>Discount conditions</label>
37
+ <comment>Before add a condition, save the attribute of discount.</comment>
38
+ <frontend_model>calculatediscount/config_discountConditions</frontend_model>
39
+ <backend_model>adminhtml/system_config_backend_serialized_array</backend_model>
40
+ <sort_order>20</sort_order>
41
+ <show_in_default>1</show_in_default>
42
+ <show_in_website>1</show_in_website>
43
+ <show_in_store>1</show_in_store>
44
+ </discountconditions>
45
+ </fields>
46
+ </general>
47
+ </groups>
48
+ </antonioloiaconodesign_calculatediscount>
49
+ </sections>
50
+ </config>
app/etc/modules/AntonioLoiaconoDesign_CalculateDiscount.xml ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <AntonioLoiaconoDesign_CalculateDiscount>
5
+ <active>true</active>
6
+ <codePool>community</codePool>
7
+ <depends>
8
+ <Mage_Adminhtml/>
9
+ </depends>
10
+ </AntonioLoiaconoDesign_CalculateDiscount>
11
+ </modules>
12
+ </config>
package.xml ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>AntonioLoiaconoDesign_CalculateDiscount</name>
4
+ <version>1.0.0</version>
5
+ <stability>stable</stability>
6
+ <license uri="http://opensource.org/licenses/osl-3.0.php">OSL v3.0</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>The extension of the calculation discount will be useful to the one who will want to obtain a filter based on the percentage of discount in the frontend.</summary>
10
+ <description>The extension of the calculation discount lets choose an attribute used for the discount. For each option dropdown of the attribute it is possible to associate a range of percentages that will be visible in frontend if the attribute makes itself filterable with results.</description>
11
+ <notes>Fixed minor bugs</notes>
12
+ <authors><author><name>Antonio Loiacono Design</name><user>antonioloiaconodesign</user><email>a.loiacono@live.it</email></author></authors>
13
+ <date>2016-04-05</date>
14
+ <time>08:51:55</time>
15
+ <contents><target name="magecommunity"><dir name="AntonioLoiaconoDesign"><dir name="CalculateDiscount"><dir name="Block"><dir name="Config"><file name="DiscountConditions.php" hash="ea2c2594c7623d440d51daf3a3d3e210"/><dir name="Adminhtml"><dir name="Form"><dir name="Field"><file name="Discount.php" hash="04fdf4b428de3f987c282a1dde99f9d5"/></dir></dir></dir></dir></dir><dir name="controllers"><dir name="Adminhtml"><file name="CalculatediscountController.php" hash="577d106ba38fc5066b8feb447c8f35dc"/></dir></dir><dir name="etc"><file name="adminhtml.xml" hash="4034f1c2ab351864c50c09ae97dca679"/><file name="config.xml" hash="941fcc01980ffcf6815f21a31e7613be"/><file name="system.xml" hash="5f5cbd88bb1964e2d5a573d6d5ffaf56"/></dir><dir name="Helper"><file name="Data.php" hash="fcdff20954c5e5f57203236d3c90b204"/></dir><dir name="Model"><file name="Observer.php" hash="1cb74ee28e30d48766e624b0c9105e7a"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="AntonioLoiaconoDesign_CalculateDiscount.xml" hash="31047778a163b19ed5c8b2bfeea2d337"/></dir></target></contents>
16
+ <compatible/>
17
+ <dependencies><required><php><min>5.2.0</min><max>6.0.0</max></php></required></dependencies>
18
+ </package>