/** * Gets the "tickets sold" message for a given ticket * * @param Tribe__Tickets__Ticket_Object $ticket Ticket to analyze * * @return string */ function tribe_tickets_get_ticket_stock_message(Tribe__Tickets__Ticket_Object $ticket) { $stock = $ticket->stock(); $sold = $ticket->qty_sold(); $cancelled = $ticket->qty_cancelled(); $pending = $ticket->qty_pending(); $event = Tribe__Tickets__Tickets::find_matching_event($ticket); $global_stock = new Tribe__Tickets__Global_Stock($event->ID); $is_global = Tribe__Tickets__Global_Stock::GLOBAL_STOCK_MODE === $ticket->global_stock_mode(); $is_capped = Tribe__Tickets__Global_Stock::CAPPED_STOCK_MODE === $ticket->global_stock_mode(); $stock_cap = $ticket->global_stock_cap(); // If ticket sales are capped, do not suggest that more than the cap amount are available if ($is_capped && $stock > $stock_cap) { $stock = $stock_cap; } // If it is a global-stock ticket but the global stock level has not yet been set for the event // then return something better than just '0' as the available stock if ($is_global && 0 === $stock && !$global_stock->is_enabled()) { $stock = '<i>' . __('global inventory', 'event-tickets') . '</i>'; } // There may not be a fixed inventory - in which case just report the number actually sold so far if (empty($stock) && $stock !== 0) { $message = sprintf(esc_html__('Sold %d', 'event-tickets'), esc_html($sold)); } else { $cancelled_count = empty($cancelled) ? '' : esc_html(sprintf(_x(' cancelled: %1$d', 'ticket stock message (cancelled stock)', 'event-tickets'), (int) $cancelled)); $pending_count = $pending < 1 ? '' : esc_html(sprintf(__(' pending: %1$d', 'ticket stock message (pending stock)', 'event-tickets'), (int) $pending)); $message = sprintf(esc_html__('Sold %1$d (units remaining: %2$s%3$s%4$s)', 'event-tickets'), esc_html($sold), $stock, $cancelled_count, $pending_count); } return $message; }
/** * Gets an individual ticket * * @param $event_id * @param $ticket_id * * @return null|Tribe__Tickets__Ticket_Object */ public function get_ticket($event_id, $ticket_id) { if (class_exists('WC_Product_Simple')) { $product = new WC_Product_Simple($ticket_id); } else { $product = new WC_Product($ticket_id); } if (!$product) { return null; } $return = new Tribe__Tickets__Ticket_Object(); $product_data = $product->get_post_data(); $qty = get_post_meta($ticket_id, 'total_sales', true); $return->description = $product_data->post_excerpt; $return->frontend_link = get_permalink($ticket_id); $return->ID = $ticket_id; $return->name = $product->get_title(); $return->price = $product->get_price(); $return->regular_price = $product->get_regular_price(); $return->on_sale = (bool) $product->is_on_sale(); $return->provider_class = get_class($this); $return->admin_link = admin_url(sprintf(get_post_type_object($product_data->post_type)->_edit_link . '&action=edit', $ticket_id)); $return->start_date = get_post_meta($ticket_id, '_ticket_start_date', true); $return->end_date = get_post_meta($ticket_id, '_ticket_end_date', true); $return->purchase_limit = get_post_meta($ticket_id, '_ticket_purchase_limit', true); $complete_totals = $this->count_order_items_by_status($ticket_id, 'complete'); $pending_totals = $this->count_order_items_by_status($ticket_id, 'incomplete'); $qty = $qty ? $qty : 0; $pending = $pending_totals['total'] ? $pending_totals['total'] : 0; // if any orders transitioned from complete back to one of the incomplete states, their quantities // were already recorded in total_sales and we have to deduct them from there so they aren't // double counted $qty -= $pending_totals['recorded_sales']; // let's calculate the stock based on the product stock minus the quantity purchased minus the // pending purchases $stock = $product->get_stock_quantity() - $qty - $pending; // if any orders have reduced the stock of an order (check and cash on delivery payments do this, for example) // we need to re-inflate the stock by that amount $stock += $pending_totals['reduced_stock']; $stock += $complete_totals['reduced_stock']; $return->manage_stock($product->managing_stock()); $return->stock($stock); $return->qty_sold($qty); $return->qty_pending($pending); $return->qty_cancelled($this->get_cancelled($ticket_id)); if (empty($return->purchase_limit) && 0 !== (int) $return->purchase_limit) { /** * Filter the default purchase limit for the ticket * * @var int * * @return int */ $return->purchase_limit = apply_filters('tribe_tickets_default_purchase_limit', 0); } return apply_filters('wootickets_get_ticket', $return, $event_id, $ticket_id); }
/** * Gets an individual ticket * * @param $event_id * @param $ticket_id * * @return null|Tribe__Tickets__Ticket_Object */ public function get_ticket($event_id, $ticket_id) { if (class_exists('WC_Product_Simple')) { $product = new WC_Product_Simple($ticket_id); } else { $product = new WC_Product($ticket_id); } if (!$product) { return null; } $return = new Tribe__Tickets__Ticket_Object(); $product_data = $product->get_post_data(); $qty = get_post_meta($ticket_id, 'total_sales', true); $return->description = $product_data->post_excerpt; $return->frontend_link = get_permalink($ticket_id); $return->ID = $ticket_id; $return->name = $product->get_title(); $return->price = $product->get_price(); $return->regular_price = $product->get_regular_price(); $return->on_sale = (bool) $product->is_on_sale(); $return->provider_class = get_class($this); $return->admin_link = admin_url(sprintf(get_post_type_object($product_data->post_type)->_edit_link . '&action=edit', $ticket_id)); $return->start_date = get_post_meta($ticket_id, '_ticket_start_date', true); $return->end_date = get_post_meta($ticket_id, '_ticket_end_date', true); $return->purchase_limit = get_post_meta($ticket_id, '_ticket_purchase_limit', true); $complete_totals = $this->count_order_items_by_status($ticket_id, 'complete'); $pending_totals = $this->count_order_items_by_status($ticket_id, 'incomplete'); $qty = $qty ? $qty : 0; $pending = $pending_totals['total'] ? $pending_totals['total'] : 0; // Ticket stock is a simple reflection of remaining inventory for this item... $stock = $product->get_stock_quantity(); // ...With some exceptions for global stock tickets $stock = $this->set_stock_level_for_global_stock_tickets($stock, $event_id, $ticket_id); $return->manage_stock($product->managing_stock()); $return->stock($stock); $return->global_stock_mode(get_post_meta($ticket_id, '_global_stock_mode', true)); $return->global_stock_cap(get_post_meta($ticket_id, '_global_stock_cap', true)); $return->qty_sold($qty); $return->qty_pending($pending); $return->qty_cancelled($this->get_cancelled($ticket_id)); if (empty($return->purchase_limit) && 0 !== (int) $return->purchase_limit) { /** * Filter the default purchase limit for the ticket * * @var int * * @return int */ $return->purchase_limit = apply_filters('tribe_tickets_default_purchase_limit', 0); } return apply_filters('wootickets_get_ticket', $return, $event_id, $ticket_id); }