Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ gateway, coupon or tax setup the shop already has.
editor. They are hidden from the shop catalogue and search, and their price, description, sale
window and capacity come from the ticket.
- **WooCommerce cart and checkout.** The event page's "Get tickets" form adds tickets to the
WooCommerce cart. Both the classic and the block checkout work.
WooCommerce cart. Both the classic and the block checkout work. Back on the event page, the form
shows the tickets already in the basket for that event; changing the numbers and pressing
"Get tickets" again sets the basket to them, so attendees are added to or removed from the
attendee details. Nothing already entered is lost: details of attendees left out, or of a
ticket taken out of the basket, come back if the numbers go up again, until checkout.
- **Attendees.** One attendee per ticket bought, created as soon as the order exists, so an unpaid
order holds its seats. They appear in Event Tickets' Attendees list, CSV export and REST API.
- **Ticket e-mails.** Sent through Event Tickets' own ticket e-mail once the order is paid.
Expand Down
68 changes: 68 additions & 0 deletions assets/js/basket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Tickets Sharp: fills the event page's "Get tickets" form from the basket.
*
* Event pages are often cached, so the basket is asked for after the page has
* loaded rather than written into it. When the answer arrives, each ticket's
* quantity shows what the basket holds for this event, Event Tickets' own
* "tickets already in your cart" notice appears, and the form is marked so
* that submitting it sets the basket to these numbers instead of adding them.
* Until then (or if the request fails) the form adds, as it always has.
*/
(function ($) {
'use strict';

var itemSelector = '.tribe-tickets__tickets-item';
var inputSelector = '.tribe-tickets__tickets-item-quantity-number-input';

function fill($form) {
var $marker = $form.find('input[name="tsharp_basket"]');
var url = $marker.data('url');
if (!$marker.length || !url) {
return;
}

$marker.val('');

$.post(url, { action: 'tsharp_basket', event: $form.data('post-id') }).done(function (response) {
var tickets = response && response.success && response.data ? response.data.tickets || {} : null;
if (!tickets) {
return;
}

var total = 0;
$.each(tickets, function (id, quantity) {
total += parseInt(quantity, 10) || 0;
});

if (total > 0) {
$form.find(itemSelector).each(function () {
var $item = $(this);
var $input = $item.find(inputSelector);
if (!$input.length || $input.prop('disabled')) {
return;
}
$input.val(parseInt(tickets[$item.data('ticket-id')], 10) || 0).trigger('change');
});
}

$form.find('#tribe-tickets__notice__tickets-in-cart').toggle(total > 0);
$marker.val('1');
});
}

function init() {
$('input[name="tsharp_basket"]').closest('form').each(function () {
fill($(this));
});
}

$(init);

// Coming back to the event page with the browser's Back button can show
// the page as it was left; ask again, since the basket may have changed.
window.addEventListener('pageshow', function (event) {
if (event.persisted) {
init();
}
});
}(jQuery));
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Tickets Sharp adds WooCommerce as a ticket provider to Event Tickets. Each ticke
* Ticket e-mails through Event Tickets' own e-mail once the order is paid, with a check-in QR code.
* Shared and capped event capacity, enforced across the whole basket.
* Per-ticket attendee information fields, collected between cart and checkout.
* The event page's ticket form shows what is already in the basket, and changing it adds or removes attendees.
* Cancelled, failed and refunded orders release their seats; partial refunds mark individual attendees.
* Compatible with WooCommerce High-Performance Order Storage and the cart and checkout blocks.

Expand Down
108 changes: 106 additions & 2 deletions src/Attendees/DetailsPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
* line when the order is created. Checkout is refused until every ticket has
* valid answers — if a quantity changes in the cart, the buyer is sent back.
*
* Nothing a buyer enters is thrown away while they change their mind. When a
* quantity goes down, the answers of the attendees left out stay on the cart
* line (unused, and not copied to the order) and come back if it goes up
* again. When a ticket leaves the basket its answers are set aside in the
* WooCommerce session and restored if it is added again; checking out clears
* them.
*
* Doing this before checkout, rather than as checkout fields, is what makes it
* work with the block checkout, whose extension API has no repeating fields.
*
Expand All @@ -49,6 +56,85 @@ public function register(): void
add_action('woocommerce_store_api_cart_errors', [$this, 'storeApiErrors'], 10, 2);
add_action('woocommerce_checkout_create_order_line_item', [$this, 'copyToOrderItem'], 10, 4);
add_filter('woocommerce_get_item_data', [$this, 'itemData'], 10, 2);
add_action('woocommerce_remove_cart_item', [$this, 'setAside'], 10, 2);
add_action('woocommerce_add_to_cart', [$this, 'restore'], 10, 2);
add_action('woocommerce_cart_emptied', [$this, 'forgetSetAside']);
}

/**
* Keeps the answers of a ticket taken out of the basket.
*
* @param mixed $cartItemKey
* @param mixed $cart
*/
public function setAside($cartItemKey, $cart = null): void
{
$cart = $cart instanceof WC_Cart ? $cart : $this->shop->cart();
$item = $cart instanceof WC_Cart && is_string($cartItemKey) ? ($cart->cart_contents[$cartItemKey] ?? null) : null;
if (! is_array($item)) {
return;
}

$productId = is_numeric($item['product_id'] ?? null) ? (int) $item['product_id'] : 0;
$answers = self::answers($item[Keys::CART_ATTENDEES] ?? []);
if ($productId <= 0 || $answers === []) {
return;
}

$setAside = $this->setAsideAnswers();
$setAside[$productId] = $answers;
$this->shop->sessionSet(Keys::SESSION_SET_ASIDE, $setAside);
}

/**
* Gives a ticket added to the basket the answers set aside when it was
* taken out, unless its line already has answers of its own.
*
* @param mixed $cartItemKey
* @param mixed $productId
*/
public function restore($cartItemKey, $productId = 0): void
{
$productId = is_numeric($productId) ? (int) $productId : 0;
$setAside = $this->setAsideAnswers();
$cart = $this->shop->cart();
if (! isset($setAside[$productId]) || ! is_string($cartItemKey) || ! $cart instanceof WC_Cart || ! isset($cart->cart_contents[$cartItemKey])) {
return;
}

if (self::answers($cart->cart_contents[$cartItemKey][Keys::CART_ATTENDEES] ?? []) === []) {
$cart->cart_contents[$cartItemKey][Keys::CART_ATTENDEES] = $setAside[$productId];
}

unset($setAside[$productId]);
$this->shop->sessionSet(Keys::SESSION_SET_ASIDE, $setAside === [] ? null : $setAside);
}

public function forgetSetAside(): void
{
if ($this->setAsideAnswers() !== []) {
$this->shop->sessionSet(Keys::SESSION_SET_ASIDE, null);
}
}

/**
* @return array<int, list<array<string, string>>>
*/
private function setAsideAnswers(): array
{
$raw = $this->shop->sessionGet(Keys::SESSION_SET_ASIDE);
if (! is_array($raw)) {
return [];
}

$setAside = [];
foreach ($raw as $productId => $answers) {
if (is_numeric($productId) && (int) $productId > 0) {
$setAside[(int) $productId] = self::answers($answers);
}
}

return $setAside;
}

/**
Expand Down Expand Up @@ -131,6 +217,9 @@ public function save(array $input, ?WC_Cart $cart = null): array
}
}

// Answers for attendees beyond the quantity wait for it to go up again.
$answers = array_merge($answers, array_slice($item['answers'], $item['quantity']));

$cart->cart_contents[$item['key']][Keys::CART_ATTENDEES] = $answers;
}

Expand Down Expand Up @@ -256,7 +345,7 @@ public function copyToOrderItem($item, $cartItemKey, $values, $order = null): vo
$item->add_meta_data(Keys::ITEM_EVENT, (int) $values[Keys::CART_EVENT], true);
}
if (isset($values[Keys::CART_ATTENDEES]) && is_array($values[Keys::CART_ATTENDEES])) {
$item->add_meta_data(Keys::ITEM_ATTENDEES, self::answers($values[Keys::CART_ATTENDEES]), true);
$item->add_meta_data(Keys::ITEM_ATTENDEES, self::bought($values), true);
}
}

Expand All @@ -276,7 +365,7 @@ public function itemData($data, $cartItem)
$productId = is_numeric($cartItem['product_id'] ?? null) ? (int) $cartItem['product_id'] : 0;
$fields = FieldSet::parse(get_post_meta($productId, Keys::PRODUCT_FIELDS, true));
$names = [];
foreach (self::answers($cartItem[Keys::CART_ATTENDEES]) as $answers) {
foreach (self::bought($cartItem) as $answers) {
$name = FieldSet::holderName($fields, $answers);
if ($name !== '') {
$names[] = $name;
Expand Down Expand Up @@ -347,6 +436,21 @@ private function renderField(string $cartKey, int $index, array $field, string $
return $html . '<input type="' . $type . '" id="' . esc_attr($id) . '" name="' . esc_attr($name) . '" value="' . esc_attr($value) . '"' . $autocomplete . $required . '></p>';
}

/**
* A cart line's answers for the attendees actually in it, leaving out any
* kept from when its quantity was higher.
*
* @param array<mixed, mixed> $cartItem
* @return list<array<string, string>>
*/
private static function bought(array $cartItem): array
{
$answers = self::answers($cartItem[Keys::CART_ATTENDEES] ?? []);
$quantity = is_numeric($cartItem['quantity'] ?? null) ? (int) $cartItem['quantity'] : count($answers);

return array_slice($answers, 0, max(0, $quantity));
}

/**
* @return list<array<string, string>>
*/
Expand Down
Loading