Adding new discount type in Magento

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>
&lt;Scommerce_Discount&gt;<br> &lt;active&gt;true&lt;/active&gt;<br> &lt;codePool&gt;local&lt;/codePool&gt;<br>
&lt;/Scommerce_Discount&gt;<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>
&lt;?xml version="1.0"?&gt;<br>
&lt;config&gt;<br> &lt;modules&gt;<br> &lt;Scommerce_Discount&gt;<br> &lt;version&gt;1.0.0&lt;/version&gt;<br> &lt;/Scommerce_Discount&gt;<br> &lt;/modules&gt;<br> &lt;global&gt;<br> &lt;models&gt;<br> &lt;scommerce_discount&gt;<br> &lt;class&gt;Scommerce_Discount_Model&lt;/class&gt;<br> &lt;/cashback&gt;<br> &lt;salesrule&gt;<br> &lt;rewrite&gt;<br> &lt;validator&gt;Scommerce_Discount_Model_Validator&lt;/validator&gt;<br> &lt;/rewrite&gt;<br> &lt;/salesrule&gt;<br> &lt;/models&gt;<br> &lt;/global&gt;<br> &lt;adminhtml&gt;<br> &lt;events&gt;<br> &lt;adminhtml_block_salesrule_actions_prepareform&gt;<br> &lt;observers&gt;<br> &lt;scommerce_discount&gt;<br> &lt;class&gt;scommerce_discount/observer&lt;/class&gt;<br> &lt;method&gt;prepareForm&lt;/method&gt;<br> &lt;/scommerce_discount&gt;<br> &lt;/observers&gt;<br> &lt;/adminhtml_block_salesrule_actions_prepareform&gt;<br> &lt;/events&gt;<br> &lt;/adminhtml&gt;<br>
&lt;/config&gt;<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>
&lt;?php<br>
class Scommerce_Discount_Model_Observer<br>
{<br> public function prepareForm(Varien_Event_Observer $observer)<br> {<br> $form = $observer-&gt;getForm();<br> $simpleAction = $form-&gt;getElement('simple_action');<br> $values = $simpleAction-&gt;getValues();<br> $values[] = array(<br> 'value' =&gt; 'newdiscount',<br> 'label' =&gt; 'new discount percentage',<br> );<br> $simpleAction-&gt;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>
&lt;?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-&gt;getStore()-&gt;convertPrice($rule-&gt;getDiscountAmount());<br> $discountAmount = $qty*($itemPrice-$quoteAmount);<br> $baseDiscountAmount= $qty*($baseItemPrice-$rule-&gt;getDiscountAmount());<br> break;<br> .....<br> $item-&gt;setAppliedRuleIds(join(',',$appliedRuleIds));<br> $address-&gt;setAppliedRuleIds($this-&gt;mergeIds($address-&gt;getAppliedRuleIds(), $appliedRuleIds));<br> $quote-&gt;setAppliedRuleIds($this-&gt;mergeIds($quote-&gt;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

Discover more from WHO WILL CARE eCommerce

Subscribe now to keep reading and get access to the full archive.

Continue reading