The support forum is in archive (read-only) mode and access is limited to members with a purchase history.

For technical support, please visit our support page.

General questions or topics can also be discussed on the ExpressionEngine StackExchange.

   
 
Access CartThrob class methods from extension hook?
Posted: 29 April 2011 09:16 PM   [ Ignore ]
Is a Really Great Dancer
RankRank
Total Posts:  67
Joined  2010-04-01

I notice that as it stands, the extension hooks aren’t getting passed references to the CartThrob object in CT2 like they were in CT1. Is there another way to access a method of either the Cartthrob or Cartthrob_cart object?

Specifically, I want to allow only one single item to be in the cart at any time, so I want to call the Cartthrob->clear_cart() or Carthrob_cart->clear() method at the beginning of the cartthrob_add_to_cart_start hook.

I’ve tried this as a workaround, but to no avail:

$this->EE->cartthrob_session->userdata['items'''

Any suggestions would be rad.

[ Edited: 17 August 2011 10:01 AM by Chris Newton ]
Profile
 
 
Posted: 29 April 2011 09:27 PM   [ Ignore ]   [ # 1 ]
Administrator
Avatar
RankRankRank
Total Posts:  3541
Joined  2009-05-29

The developer docs haven’t been done yet, but you can access the cart object (and it’s full api) like so:

$this->EE->cartthrob->cart->clear(); 

Part of the transition to CT1 was making the cart object oriented, so you interact with the cart object(s) rather than a raw session array. The session array in CT2 is used only to restore & save the state of the cart at the beginning and end of each page load, and modifying it directly is not supported anymore.

Profile
 
 
Posted: 29 April 2011 09:29 PM   [ Ignore ]   [ # 2 ]
Is a Really Great Dancer
RankRank
Total Posts:  67
Joined  2010-04-01

That is just excellent to hear. Should have tried that before. Thanks.

Profile
 
 
Posted: 17 August 2011 12:30 AM   [ Ignore ]   [ # 3 ]
Has a Great Personality
Rank
Total Posts:  50
Joined  2010-08-17

Hey guys,

Just going to add my two cents in here (yes, I know this thread is from a few months ago).

I’ve had the need to empty the cart in an extension hook (and a plugin) when certain conditions are met.  Unfortunately just using the ->clear() function didn’t work for me.  To properly clear the cart, I had to do the full:

$this->EE->cartthrob->cart->clear()
->
clear_coupon_codes()
->
clear_shipping_info();

$this->EE->cartthrob->cart->clear_customer_info();
 
$this->EE->cartthrob->cart->save(); 

I’m running EE 2.1.3 - but am running the latest CT version (2.0385).

I thought I would let you know, just in case you thought this was a) bad for me to do b) a bug in CT.

Cheers.

Andrew.

Profile
 
 
Posted: 17 August 2011 12:35 AM   [ Ignore ]   [ # 4 ]
Absolute Heartthrob!
Avatar
RankRankRankRankRank
Total Posts:  10369
Joined  2008-09-29

It’s not a bug. We kinda classify the cart, as different from the coupon codes, shipping info, and customer info. But to your point, yeah, that’s the way to destroy everything.

 Signature 
Profile
 
 
Posted: 17 August 2011 12:38 AM   [ Ignore ]   [ # 5 ]
Has a Great Personality
Rank
Total Posts:  50
Joined  2010-08-17

Thanks Chris.  Didn’t actually think it was a bug… just really wanted to know that this was the best way to kill off the whole cart.  Good to clarify.

smile

Profile