Support

Home Forums Event Espresso Premium Call to a member function add_ticket_to_cart() on null

Call to a member function add_ticket_to_cart() on null

Posted: December 4, 2018 at 1:06 pm


terabit

December 4, 2018 at 1:06 pm

Hi,

We just purchase the plugin month ago and would like to do some experiments on how to list tickets and add the ticket to the cart using RPC without using Ticket_Selector.

However, when I use add_ticket_to_cart(), I got an error.
– Call to a member function add_ticket_to_cart() on null –

$cart = EE_Registry::instance()->CART;
$purchased_ticket = EEM_Ticket::instance()->get_one_by_ID($ticket_id);
$cart->add_ticket_to_cart($purchased_ticket, 1);

Please help and thanks


Josh

  • Support Staff

December 4, 2018 at 1:12 pm

Hi,

May I ask where is $ticket_id set and does it set a valid ticket ID?


terabit

December 4, 2018 at 1:55 pm

Hi Josh,

Thanks for the reply. it is a valid ticket id.
below is the way I retrieve the ticket id.

$event = EEM_Event::instance()->get_one_by_ID(4196);
$tickets = $event->tickets();
foreach($tickets as $key => $ticket){
}

Thanks


Josh

  • Support Staff

December 4, 2018 at 2:07 pm

That does not show where you’ve set the $ticket_id though. Have you done a var_dump() on $ticket_id?

An aside: There are a few things to watch out for when getting tickets from an event: You’ll want to avoid deleted tickets, tickets not on sale yet, tickets that are past their sale date, and so on.

Since it’s not clear what’s going on from the few lines of code you’ve posted here, maybe what you can do is post the entirety of the code you’re working with into a GitHub gist. Hopefully you already have an account on GitHub. If you do not, it’s free to sign up. Then, once you have an account, you can post a gist on GitHub, then link here.

If the code you’re working with is more than a few files, you could upload it to a repository instead.

This way we can take a look to get have a better idea of what’s missing and leading up to the resulting NULL value.


terabit

December 5, 2018 at 9:15 am

Thank you, I will post some code on GitHub, and then will ask you again. thank you for the quick support. I am very appreciated.

Thanks.


terabit

December 5, 2018 at 12:05 pm

This reply has been marked as private.


Tony

  • Support Staff

December 5, 2018 at 12:17 pm

Hi there,

Can you recheck that link please, it doesn’t appear to be valid.


Josh

  • Support Staff

December 5, 2018 at 12:18 pm

Hi,

I’m afraid the URL in the link you posted,
http://add_ticket_to_cart()%20on%20null,
is invalid.


terabit

December 5, 2018 at 12:26 pm

This reply has been marked as private.


Josh

  • Support Staff

December 5, 2018 at 12:43 pm

Ok, thanks.

If you put a var_dump() right after this line:
$ticket_id = $_GET['params']['ticket_id'];
e.g.
var_dump($ticket_id);
is anything displayed?


terabit

December 5, 2018 at 12:53 pm

yes, it is.

line 11 or line 16 without if condition is causing the same error.

Thanks,


Josh

  • Support Staff

December 5, 2018 at 1:15 pm

OK, but what’s displayed with the var_dump()? Is it a valid ticket ID?

Also, if you do a var_dump() for $cart, do you get a valid cart object?

As an aside, I think you’ll also need to hook into wp_ajax_nopriv to allow non-logged in users to add tickets to the cart.

e.g.
add_action( 'wp_ajax_nopriv_add_ticket_to_cart', 'add_ticket_to_cart' );


terabit

December 5, 2018 at 1:45 pm

HI Josh

Thank for the reply, it returns Null when I do the var_dump for the $cart.
I did try the initialize the $cart outside of the ajax function, it returns the cart object.
I replace
$cart = EE_Registry::instance()->CART;
to
$cart = EE_Registry::instance()->load_core( 'Cart' );

it does return the cart object. however, it just skips the function add_ticket_to_cart(). there is no ticket being added to the cart.

Thank and please help.


Josh

  • Support Staff

December 5, 2018 at 2:20 pm

Hi,

It sounds like a loading issue, where the other required items related to the cart are not available when your AJAX call happens. With that, I’m afraid there isn’t much further I can do to help you with your coding project. I can point you to the Multi Event Registration plugin, that has similar work in it where it adds quantities of tickets to the cart. Have you checked the source of the Multi Event Registration plugin?

You must be logged in to reply to this support post. Sign In or Register for an Account

Support forum for Event Espresso 3 and Event Espresso 4.
Documentation for EE3 and EE4
Documentation for Event Espresso 3

Documentation for Event Espresso 4

Status: publish

Updated by  Josh 1 week, 4 days ago ago

Topic Tags

Tagged: 

Notifications

This topic is: not resolved
Do NOT follow this link or you will be banned from the site!