2

How do I add an invoice fee to an order with my payment module? I guess this should be done during the checkout process through my payment method model. Perhaps I should create and add an item/product to the cart/quote/order object?

I don't know how to do any of these things though. Please help

2 Answers2

9

Although possible it is not for the feint-hearted. Here is a rough run-down of the steps to add a line to the totals area, which will then add your fee to the grand total.

In the config node <global><sales><quote><total> add a new entry (see app/code/core/Mage/Sales/etc/config.xml for more examples)

<paymentFee>
    <class>yourmodule/quote_address_total_paymentFee</class> <!-- A model -->
    <after>subtotal</after>
</paymentFee>

Also in the config.xml add the following to <global>...

<fieldsets>
    <sales_convert_quote>
        <payment_fee><to_order>*</to_order></payment_fee>
    </sales_convert_quote>
</fieldsets>

Create the model to calculate the fee.

class Your_Module_Model_Quote_Address_Total_Warranty extends Mage_Sales_Model_Quote_Address_Total_Abstract
{
    public function __construct()
    {
        $this->setCode('paymentFee');
    }

    public function collect(Mage_Sales_Model_Quote_Address $address)
    {
        // Check the payment method in use, if it is yours do...
        $address->setPaymentFee($fee);
        return $this;
    }

    public function fetch(Mage_Sales_Model_Quote_Address $address)
    {
        if ($address->getPaymentFee()) {
            $address->addTotal(array(
                'code'  => $this->getCode(),
                'title' => 'Your module payment message',
                'value' => $address->getPaymentFee()
            ));
        }
        return $this;
    }
}

In your module's setup, modify the sales_flat_quote and sales_flat_order tables to add a payment_fee column.

The <after> value in the config is responsible for determining the order of calculation, it can be a comma-separated list of totals' codes including "tax", "discount", etc. You may also specify a <before> value for the same purposes. The $address->addTotal in fetch() method will do the work of updating the grand total, which is what the customer will be charged. It is necessary to alter the quote and order tables so the fee you have charged is recorded and shown to the admin.

It is also possible to specify your own renderer if the default will not do, I have done this also but is even more complex.

Community
  • 1
  • 1
clockworkgeek
  • 37,650
  • 9
  • 89
  • 127
  • nice piece of code... I would recommend that you follow Ivan's advice (here: http://stackoverflow.com/questions/4378394/magento-adding-a-new-column-to-sales-flat-quote-item-and-sales-flat-order-item/4378681#4378681) for modifying the `sales_flat` tables in a non-SQL manner. – Jonathan Day Dec 23 '10 at 04:16
  • I've had to add totals lines more than once so have the method well documented. – clockworkgeek Dec 23 '10 at 10:03
  • No problem, I was just suggesting it for future readers of the answer :) – Jonathan Day Dec 24 '10 at 03:05
  • I've done all this. The invoice fee is included on the checkout page, and I've added the fields to the sales_flat_order and sales_flat_quote tables. However the quote/order wont store my invoice fee in those fields. (I'm adding the fee to the address object) – Troubled Magento user Jan 13 '11 at 10:36
  • Just realised I missed an important conversion part. The answer has been updated with it. – clockworkgeek Jan 13 '11 at 12:56
  • The configuration did nothing :( I'm using this code http://stackoverflow.com/questions/4378394/magento-adding-a-new-column-to-sales-flat-quote-item-and-sales-flat-order-item/4378681#4378681 to update the tables, with grid set to false – Troubled Magento user Jan 13 '11 at 13:40
  • Im sorry to bother you further, but this does not work for me! I've got the fieldset configuration you provided, and the tables are modified to include my invoice fee field. Also I'm using your Mage_Sales_Model_Quote_Address_Total_Abstract code. But the fields will not be assigned any value. However the invoice fee is displayed on the checkout page (but not on the order page in the backend) – Troubled Magento user Jan 24 '11 at 08:30
  • Another possibility (although not what I did in this occassion) is to listen for the event `sales_convert_quote_to_order` and update the order object; `$observer->getOrder()->setPaymentFee()` – clockworkgeek Jan 24 '11 at 12:31
  • With the sales_convert_quote_to_order observer I'm able to add data to the order (and database). But the fee is not included on the order page. The grand total is correct, but if I invoice the order, the invoice fee will be missing from the total paid amount – Troubled Magento user Jan 24 '11 at 13:48
  • Invoices are separate entities from orders and quotes which needs their own flat tables updating and another conversion (`sales_convert_order`) in fieldsets. Is this what you mean by "not included on the order page"? – clockworkgeek Jan 24 '11 at 14:15
  • Its included in the Grand Total on the order page (backend). But its not listed as a product/fee. And when I click the Invoice button the total paid wont include my invoice fee. So I guess, yes – Troubled Magento user Jan 24 '11 at 14:24
  • I guess its the "It is necessary to alter the quote and order tables so the fee you have charged is recorded and shown to the admin." step that I have not fully succeeded with. – Troubled Magento user Jan 24 '11 at 15:19
0

If you haven't worked with Magento's codebase before, I wouldn't recommend that you start with that, it's fairly complex. Using an extension is probably the safest option.

I haven't used it, but I believe this extension should do the job.

Standard disclaimer: I'm not affiliated with the vendor in any way. Caveat emptor.

Jonathan Day
  • 18,519
  • 10
  • 84
  • 137