dsouzaj86

Forum Replies Created

Viewing 1 post (of 1 total)
  • Author
    Posts
  • in reply to: Consider cart items for available tickets #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

Viewing 1 post (of 1 total)