Displaying remaining stock in related events

Home Forums Ticket Products Event Tickets Plus Displaying remaining stock in related events

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1028401
    Chris
    Participant

    Hi – using the following function found on here I’m wanting to output the remaining stock level into the ‘Related Events’ section at the bottom of each single event page. I’ve tried modifying as it as standard just outputs the stock level of the current product page onto each related event.

    However it doesn’t work – Closest I can get is a list of all ticket stock! Any guidance would be much appreciated…

    function displaying_stock_information() {

    `$post_id = get_the_id();
    if ( Tribe__Events__Main::POSTTYPE !== get_post_type( $post_id ) ) {
    return;
    }
    $Woo__Tickets = Tribe__Events__Tickets__Woo__Main::get_instance();
    $ticket_ids = $Woo__Tickets->get_tickets_ids( $post_id );

    if ( is_array( $ticket_ids ) ) {

    foreach ( $ticket_ids as $ticket_id ) {
    $ticket = $Woo__Tickets->get_ticket( $post_id, $ticket_id );
    $html .= sprintf( ‘<dt>Places available:</dt><dd>%s</dd>’, $ticket->stock );
    // to show ticket name also: $html .= sprintf( ‘

    • %s tickets remaining: %s

     

    ‘, $ticket->name, $ticket->stock );
    }

    if ( $ticket->stock > 0 ) { // if all tickets sold or no tickets
    echo $html;
    } else {
    echo ‘<dt>Places available:</dt><dd>COURSE SOLD OUT</dd>’;
    }
    }

    }`

    #1028696
    Geoff
    Member

    Hey there, Chris!

    Would theĀ tribe_events_count_available_tickets() function be an easier way to display the number of remaining tickets? That’s where I would suggest starting.

    Let me know if that helps and I’d be happy to answer any follow-up questions as best I can.

    Cheers!
    Geoff

    #1028834
    Chris
    Participant

    Brilliant! sorted with a bit of tweaking, Many thanks.

    Here’s the code in case anyone else needs it:

    
    function tribe_events_count_available_related_tickets( $event = null ) {
    	$count = 0;
    
    	if ( null === ( $event = tribe_events_get_event( $event ) ) ) {
    		return 0;
    	}
    
    	foreach ( Tribe__Events__Tickets__Tickets::get_all_event_tickets( $event->ID ) as $ticket ) {
    		$count += $ticket->stock;
    		$html .= sprintf( '<dt><strong>Places available:</strong></dt><dd>%s</dd>',  $count );
    		$html2 .= sprintf( '<dd><strong>COURSE SOLD OUT</strong></dd>',  $ticket->stock );
    	}
    				
    		if ( $ticket->stock > 0 ) { // if all tickets sold or no tickets
    			echo $html;
    		} if ( $count < 1 ) {
    			echo '<dd><strong>COURSE SOLD OUT</strong></dd>';
    		}
    
    }
    
    
    #1028835
    Chris
    Participant

    Ta

    #1029055
    Chris
    Participant

    Forgot the last!

    This is the one:

    
    function tribe_events_count_available_related_tickets( $event = null ) {
    	$count = 0;
    
    	if ( null === ( $event = tribe_events_get_event( $event ) ) ) {
    		return 0;
    	}
    
    	foreach ( Tribe__Events__Tickets__Tickets::get_all_event_tickets( $event->ID ) as $ticket ) {
    		$count += $ticket->stock;
    		$html .= sprintf( '<dd><strong>Places available: </strong>%s</dd>',  $count );
    	}
    				
    		if ( $ticket->stock > 0 ) { 
    			echo $html;
    		} if ( $count < 1 ) { // if all tickets sold or no tickets
    			echo '<dd><strong>COURSE SOLD OUT</strong></dd>';
    		}
    
    }
    
    #1030437
    Brook
    Participant

    Thank you for sharing your solution! That’s very awesome of you.

    I am going to archive this topic now. It will be available for all who want to refer to your work. Cheers!
    – Brook

Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘Displaying remaining stock in related events’ is closed to new replies.