-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Log ticket purchases via usermeta on user profile #1418
base: production
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7675,6 +7675,7 @@ function payment_result( $payment_token, $result, $data = array(), $interactive | |
if ( self::PAYMENT_STATUS_COMPLETED == $result ) { | ||
$attendee->post_status = 'publish'; | ||
wp_update_post( $attendee ); | ||
$this->log_ticket_purchase_on_user_profile( $attendee ); | ||
} | ||
|
||
if ( self::PAYMENT_STATUS_PENDING == $result ) { | ||
|
@@ -7687,6 +7688,7 @@ function payment_result( $payment_token, $result, $data = array(), $interactive | |
wp_update_post( $attendee ); | ||
update_post_meta( $attendee->ID, 'tix_refund_transaction_id', $refund_transaction_id ); | ||
update_post_meta( $attendee->ID, 'tix_refund_transaction_details', $refund_transaction_details ); | ||
$this->update_ticket_status_on_user_profile( $attendee->ID, $attendee->post_status ); | ||
$this->log( sprintf( 'Refunded %s by user request in %s.', $transaction_id, $refund_transaction_id ), $attendee->ID, $data, 'refund' ); | ||
} | ||
|
||
|
@@ -8595,6 +8597,83 @@ public function is_wordcamp_closed() { | |
public function has_tickets_available() { | ||
return $this->number_available_tickets() > 0; | ||
} | ||
|
||
/** | ||
* Log the purchased ticket information on the user's profile. | ||
* | ||
* @param object $attendee The attendee object containing ticket information. | ||
*/ | ||
function log_ticket_purchase_on_user_profile( $attendee ) { | ||
$user_id = get_current_user_id(); | ||
$purchase_history = get_user_meta( $user_id, 'wordcamp_ticket_history', true ); | ||
|
||
// Initialize the purchase history as an empty array if it's not already an array. | ||
if ( ! is_array( $purchase_history ) ) { | ||
$purchase_history = array(); | ||
} | ||
|
||
// Check if the current attendee's ID is already in the purchase history. | ||
$existing_attendee_ids = array_column( $purchase_history, 'id' ); | ||
if ( ! in_array( $attendee->ID, $existing_attendee_ids ) ) { | ||
// Gather ticket details to log the purchase. | ||
$ticket_id = intval( get_post_meta( $attendee->ID, 'tix_ticket_id', true ) ); | ||
$ticket = get_post( $ticket_id ); | ||
$ticket_type = $ticket->post_title; | ||
$ticket_price = $this->append_currency( (float) get_post_meta( $attendee->ID, 'tix_ticket_price', true ), false ); | ||
$purchase_date = $attendee->post_date; | ||
$edit_token = get_post_meta( $attendee->ID, 'tix_edit_token', true ); | ||
$edit_link = $this->get_edit_attendee_link( $attendee->ID, $edit_token ); | ||
$access_token = get_post_meta( $attendee->ID, 'tix_access_token', true ); | ||
$access_link = $this->get_access_tickets_link( $access_token ); | ||
$ticket_status = $attendee->post_status; | ||
Comment on lines
+8619
to
+8628
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are all these values needed to be stored in usermeta, or could it link to a purchase on the individual site? |
||
|
||
// Create a new purchase entry. | ||
$new_purchase = array( | ||
'id' => $attendee->ID, | ||
'wordcamp_name' => get_wordcamp_name(), | ||
'site_url' => site_url(), | ||
'purchase_date' => $purchase_date, | ||
'ticket_type' => $ticket_type, | ||
'ticket_price' => $ticket_price, | ||
'access_link' => $access_link, | ||
'edit_link' => $edit_link, | ||
'ticket_status' => $ticket_status, | ||
); | ||
|
||
// Add a refund link if the ticket is refundable. | ||
if ( $this->is_refundable( $attendee->ID ) ) { | ||
$new_purchase['refund_link'] = esc_url( $this->get_refund_tickets_link( $access_token ) ); | ||
} | ||
|
||
$purchase_history[] = $new_purchase; | ||
|
||
update_user_meta( $user_id, 'wordcamp_ticket_history', $purchase_history ); | ||
} | ||
} | ||
|
||
/** | ||
* Update purchased ticket status on the user's profile. | ||
* | ||
* @param int $attendee_id The ID of the rufunded attendee. | ||
* @param string $ticket_status Ticket status. | ||
*/ | ||
function update_ticket_status_on_user_profile( $attendee_id, $ticket_status ) { | ||
$user_id = get_current_user_id(); | ||
$purchase_history = get_user_meta( $user_id, 'wordcamp_ticket_history', true ); | ||
|
||
// If there's no purchase history or it's not an array, nothing to udpate. | ||
if ( ! is_array( $purchase_history ) || empty( $purchase_history ) ) { | ||
return; | ||
} | ||
|
||
foreach ( $purchase_history as $index => $ticket ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. by storing as a large single meta, this type of loop in then necessary, which appears difficult to understand 😄 |
||
if ( isset( $ticket['id'] ) && intval( $ticket['id'] ) === intval( $attendee_id ) ) { | ||
$purchase_history[ $index ]['ticket_status'] = $ticket_status; | ||
update_user_meta( $user_id, 'wordcamp_ticket_history', $purchase_history ); | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Initialize the $camptix global. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be a single user meta or can it be multiple?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a single entry works for how I currently store the data. But yeah I should save it in a way that allows multiple access as it would make the structure and data access clearer.