Consider cart items for available tickets

Home Forums Ticket Products Event Tickets Plus Consider cart items for available tickets

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1603087
    dsouzaj86
    Participant

    Hi,

    I’m using tribe_events_count_available_tickets to retrieve the stock level for tickets. I was wondering if there is a similar function that also considers the items in the current users basic.

    For example, there is 1 ticket left for “an event” and I add it to my basket. The tribe_events_count_available_tickets function still returns 1, which leads my other logic to mark “an event” as still available to purchase.

    Any thoughts are much appreciated.

    All the best,
    Joe

    #1603176
    dsouzaj86
    Participant

    In order to get around my issue, I decided to clone the existing method and introduce an additional option to include the cart content in the count:

    function tribe_events_count_available_tickets_with_cart( $event = null, $consider_cart = false ) {
        $count = 0;
    
        if ( null === ( $event = tribe_tickets_parent_post( $event ) ) ) {
            return 0;
        }
    
        // Check basket to see if we're already "holding"
        // tickets for this event
        $cart = $consider_cart ? WC()->cart->get_cart() : NULL;
    
        foreach ( Tribe__Tickets__Tickets::get_all_event_tickets( $event->ID ) as $ticket ) {
    
            $global_stock_mode = $ticket->global_stock_mode();
    
            if ( $global_stock_mode === Tribe__Tickets__Global_Stock::GLOBAL_STOCK_MODE ) {
                continue;
            }
    
            $stock_level = $global_stock_mode === Tribe__Tickets__Global_Stock::CAPPED_STOCK_MODE ? $ticket->global_stock_cap : $ticket->stock;
            $count += (int) $stock_level; // Explicit cast needed because it's possible $stock_level will be an empty string (unlimited stock)
    
            if ($consider_cart) {
                // Reduce stock count where item is "held" in
                // the cart.
                foreach ($cart as $cart_item)
                {
                    if ($ticket->ID == $cart_item["product_id"]) {
                        $count--;
                    }
                }
            }
        }
    
        $global_stock = new Tribe__Tickets__Global_Stock( $event->ID );
        $global_stock = $global_stock->is_enabled() ? $global_stock->get_stock_level() : 0;
        $count += $global_stock;
    
        return $count;
    }

    I’d be interested to hear if there is another way to do this already in the system?

    Thanks,
    Joe

    #1605432
    Victor
    Member

    Hi Joe!

    Thanks for getting in touch with us!

    As you may already know, we are fairly limited in how much support we can give for custom development questions like that.

    That said, we always like helping out and at least point users into the right direction as much possible.

    Just like you say, theĀ tribe_events_count_available_tickets() function does not take into account tickets added to the cart.

    I’m not sure exactly what you are trying to accomplish, but I don’t see anything strange in the code you shared.

    In addition, you may want to have a look at theĀ validate_tickets() function located at /plugins/event-tickets-plus/src/Tribe/Commerce/WooCommerce/Main.php which is used to check if the tickets added to the cart are still available for sale.

    Hope that helps. Is there anything else I can help you with?

    Best,
    Victor

    #1622697
    Support Droid
    Keymaster

    Hey there! This thread has been pretty quiet for the last three weeks, so we’re going to go ahead and close it to avoid confusion with other topics. If you’re still looking for help with this, please do open a new thread, reference this one and we’d be more than happy to continue the conversation over there.

    Thanks so much!
    The Events Calendar Support Team

Viewing 4 posts - 1 through 4 (of 4 total)
  • The topic ‘Consider cart items for available tickets’ is closed to new replies.