I have my module with my custom discount and it's OK.
config.xml:
<sales>
<quote>
<totals>
<aver>
<class>Dani_Prueba_Model_Total_Aver</class>
<after>subtotal</after>
</aver>
</totals>
</quote>
</sales>
My module:
<?php
class Dani_Prueba_Model_Total_Aver extends Mage_Sales_Model_Quote_Address_Total_Abstract{
public function collect(Mage_Sales_Model_Quote_Address $address){
$baseDiscount = 2.5;
$discount = Mage::app()->getStore()->convertPrice($baseDiscount);
$address->setCustomDiscount($baseDiscount);
$address->setBaseGrandTotal($address->getBaseGrandTotal() - $baseDiscount);
$address->setGrandTotal($address->getGrandTotal() - $discount);
return $this;
}
public function fetch(Mage_Sales_Model_Quote_Address $address){
$this->setCode('aver');
$amount = $address->getCustomDiscount();
if ($amount != 0){
$address->addTotal(array(
'code' => $this->getCode(),
'title' => 'Custom Discount',
'value' => $amount
));
}
return $this;
}
}
This it is OK and when I add a product to cart, automatically apply my custom discount.
But now I need do it with a button. When I add products to cart not apply discount and have the correct total. But when I click a button, apply my custom discount, and with other button "Cancel", cancel the discount. I need some similar like the function a coupon code.
How I do it??
To do this, you will need to add another attribute/column to the
sales/quote
table (and possiblysales/order
table).So, in your install script, execute this (I included the sales/order table/entity attribute as well):
Then, in your controller, do something like this:
Or, the opposite, in your removeAction:
And, then finally, in your total model, modify it to be this:
Joseph Maxwell, thank you for the great solution! However,
This code should be changed to the following one
Without GET word I have faced the following error:
and spent some time until I realized the reason of the error.
As a followup to the accepted answer, I'd like to point out that people (like me from the past) who are having trouble getting their custom totals reflected on invoices will need to add a few more things.
First, your column needs to be added to the
sales/invoice
table, as well.Second, Mage_Sales uses Convert objects to propagate data from quotes to orders to invoices. To get
your_custom_total
included in this flow, add a couple config hooks:Now your total will be included when a quote is converted to an order, and that order to an invoice.
However, even though your total is saved on the invoice, the invoice does not know how that total affects its grand total. Thus, an invoice total model similar to the quote address total model needs to be added.
The config hook:
And the class:
You can mirror the above for converting invoices to credit memos as well, if your total is applicable.
This link http://magento.ikantam.com/qa/how-add-discount-total-magento explains how to rewrite adminhtml blocks to show your total on the backend.