Today we are going to talk about how can you create new discount type in Magento. We had a requirement from our client to create new discount type i.e. cashback which allows their customers to get percentage discount back on their next order. For example -: Customer buys £100 worth of goods in the first order so he/she gets qualified for 10% cashback discount on their second order which means he/she will get £10 off. If you are interested in buying this module then you can buy by clicking on the following link Cash Back Discount
Here is the step by step guide to create new discount type in Magento -:
Step 1 – Create a new module under your local folder, in our case under /app/local/Scommerce/Discount
Step 2 – Register the module in etc/modules directory either in Scommerce_All.xml or Scommerce_Discount.xml by using the following code
<br> <Scommerce_Discount><br> <active>true</active><br> <codePool>local</codePool><br> </Scommerce_Discount><br>
Step 3 – Create config.xml under etc directory of your module /app/local/Scommerce/Discount/etc/config.xml by using the following code
<br> <?xml version="1.0"?><br> <config><br> <modules><br> <Scommerce_Discount><br> <version>1.0.0</version><br> </Scommerce_Discount><br> </modules><br> <global><br> <models><br> <scommerce_discount><br> <class>Scommerce_Discount_Model</class><br> </cashback><br> <salesrule><br> <rewrite><br> <validator>Scommerce_Discount_Model_Validator</validator><br> </rewrite><br> </salesrule><br> </models><br> </global><br> <adminhtml><br> <events><br> <adminhtml_block_salesrule_actions_prepareform><br> <observers><br> <scommerce_discount><br> <class>scommerce_discount/observer</class><br> <method>prepareForm</method><br> </scommerce_discount><br> </observers><br> </adminhtml_block_salesrule_actions_prepareform><br> </events><br> </adminhtml><br> </config><br>
In the above step, the key thing to notice is the adminhtml_block_salesrule_actions_prepareform event which we are going to call to set new discount type in admin.
Step 4 – Here we are going to create our observer class under Model directory of our module app/code/local/Scommerce/Discount/Model/Observer.php.
This class will help creating new discount type in admin under catalog pricing rule
<br> <?php<br> class Scommerce_Discount_Model_Observer<br> {<br> public function prepareForm(Varien_Event_Observer $observer)<br> {<br> $form = $observer->getForm();<br> $simpleAction = $form->getElement('simple_action');<br> $values = $simpleAction->getValues();<br> $values[] = array(<br> 'value' => 'newdiscount',<br> 'label' => 'new discount percentage',<br> );<br> $simpleAction->setValues($values);<br> }<br> }<br>
Step 5 – This is the last step and also the most important step of all. This is where you are going to write your new discount logic for discount type newdiscount
<br> <?php<br> class Scommerce_Discount_Model_Validator extends Mage_SalesRule_Model_Validator<br> {<br> /**<br> * Quote item discount calculation process<br> * @param Mage_Sales_Model_Quote_Item_Abstract $item<br> * @return Mage_SalesRule_Model_Validator<br> */<br> public function process(Mage_Sales_Model_Quote_Item_Abstract $item)<br> {<br> .....<br> case 'newdiscount':<br> //YOUR NEW DISCOUNT LOGIC WILL GO HERE<br> $quoteAmount = $quote->getStore()->convertPrice($rule->getDiscountAmount());<br> $discountAmount = $qty*($itemPrice-$quoteAmount);<br> $baseDiscountAmount= $qty*($baseItemPrice-$rule->getDiscountAmount());<br> break;<br> .....<br> $item->setAppliedRuleIds(join(',',$appliedRuleIds));<br> $address->setAppliedRuleIds($this->mergeIds($address->getAppliedRuleIds(), $appliedRuleIds));<br> $quote->setAppliedRuleIds($this->mergeIds($quote->getAppliedRuleIds(), $appliedRuleIds));<br> return $this;<br> }<br> }<br>
That’s it, now you have new discount type created to use.
Hope this article helped you in some way. Please leave us your comment and let us know what do you think? Thanks.
![Adding New Variables to window.checkoutConfig on Magento 2 checkout](https://whowillcare.net/wp-content/uploads/2023/02/adding-new-discount-type-in-magento.png)