Jump to content

  • You cannot start a new topic
  • You cannot reply to this topic

Modifying Cart Total Programatically Rate Topic   - - - - -

 
  • arfeen
  • Newbie
  • Trial users
  • Join Date: 03-Jan 18
  • 6 posts

Posted 10 July 2018 - 11:57 AM #1

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



 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 10657 posts

Posted 10 July 2018 - 04:32 PM #2

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.


EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.


 
  • arfeen
  • Newbie
  • Trial users
  • Join Date: 03-Jan 18
  • 6 posts

Posted 11 July 2018 - 07:12 AM #3

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.



 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 10657 posts

Posted 11 July 2018 - 06:29 PM #4

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().


EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.