Modifying Cart Total Programatically

Hello

I'm trying to implement a third part loyalty system and there I need to reduce the total price based on some coupon on loyalty card.

I have tried to do it in "fn_calculate_cart" hook and reduced the price programatically.

checkout.pre.php controller shows the correct total amount what I want, but in checkout.post.php, cart total remains original and that is why the order is placed with original amount and not the reduced amount which I did in the hook.


Is there anything I'm missing here ?

Here's my hook code


function fn_sample_addon_calculate_cart(&$cart, &$cart_products, &$auth) {

$total = $cart['total'];

$total = $total - 5; // hard code amount 5

$cart['total'] = $total;
$cart['amount'] = $total;

}

Thanks

Arfeen

First off you should be using hooks rather than modifying core files.

That being said, you need to either manage the data independently or map it into a promotion. If you map it into a promotion (hidden with a specific name), it will save you a lot of work and corner cases.

So your hook should trigger on your event and then apply the hidden promotion. You would then use a separate hook from the promotions functions to adjust the amount of the promotion BEFORE it is applied.

It should then work within the cart calculations and also hit all the other areas of the orders like invoices, paypal payments (whicha re also special), etc.

Thanks for the reply.

I'm, indeed, using hooks to manage this process. The example I pasted above was an example hook I made. I'm managing my loyalty system as a promotion and inserting it into cart as extra data. There are several logic behind this process.

But the question was about modifying cart object. Does the update of 'total' in cart object in the hook "calculate_cart" would be permanent until cart life ? or it gets reset somewhere ?

Hope Im able to make you my problem understand.

You can't just adjust total. Paypal and other things expect to be able to take the subtotal, taxes, shipping, etc. and arrive at the total. There's a special hook in the paypal stuff for when it calculates a total to get around this (because they just sum the products to get subtotal within paypal rather than relying on fn_calculate_cart().