Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Add new registration
- *
- * @return WP_Error|boolean|EE_Registration
- */
- function add_new_registration($data, $check_referer = true, $reg_number = 1, $quantity = 1)
- {
- if ($check_referer && !check_ajax_referer('add_new_registration', false, false)) {
- return new WP_Error("invalid_token", "Invalid or expired session");
- }
- extract($data);
- if (isset($quantity) && $quantity > 0) {
- $quantity = $quantity;
- } else {
- $quantity = 1;
- }
- if (!EE_Registry::instance()->CAP->current_user_can('ee_edit_registrations', 'espresso_registrations_new_registration', $event_id)) {
- return new WP_Error("permission_denied", "Permission Denied");
- }
- if (empty($first_name)) {
- return new WP_Error("first_name", "First Name is required");
- }
- if (empty($last_name)) {
- return new WP_Error("last_name", "Last Name is required");
- }
- global $wpdb;
- $ticket = EEM_Ticket::instance()->get_one_by_ID($ticket_id);
- if (!$ticket instanceof EE_Ticket) {
- $event = EEM_Event::instance()->get_one_by_ID($event_id);
- if (!$event instanceof EE_Event) {
- return new WP_Error('ticket', 'Invalid Ticket');
- }
- $ticket = reset($event->tickets());
- if (!$ticket instanceof EE_Ticket) {
- return new WP_Error('ticket', 'Invalid Ticket');
- }
- }
- $registration_processor = EE_Registry::instance()->load_class('Registration_Processor');
- $payment_method = EEM_Payment_Method::instance()->get_one_by_slug('other');
- $transaction = EE_Transaction::new_instance(array(
- 'TXN_timestamp' => time(), //unix timestamp
- 'TXN_total' => 0, //txn_total
- 'TXN_paid' => 0, //txn_paid
- 'STS_ID' => EEM_Transaction::complete_status_code, //sts_id
- 'TXN_session_data' => null, //dump of txn session object (we're just going to leave blank here)
- 'TXN_hash_salt' => null, //hash salt blank as well
- 'PMD_ID' => $payment_method->ID(),
- ));
- $transaction->save();
- $total_line_item = $transaction->total_line_item();
- $line_item = \EEH_Line_Item::add_ticket_purchase(
- $total_line_item,
- $ticket,
- $quantity
- );
- $total_line_item->save_this_and_descendants_to_txn($transaction->ID());
- EEH_Line_Item::apply_taxes($total_line_item);
- for ($i = 1; $i <= $quantity; $i++) {
- $registration = (new \EventEspresso\core\domain\services\registration\CreateRegistrationService())->create(
- $ticket->get_related_event(),
- $transaction,
- $ticket,
- $line_item,
- $i,
- $quantity
- );
- $attendee = EE_Attendee::new_instance(array(
- 'ATT_fname' => !empty($first_name) ? $first_name : '',
- 'ATT_lname' => !empty($last_name) ? $last_name : '',
- 'ATT_email' => !empty($email) ? $email : '',
- 'ATT_phone' => $phone,
- ));
- if (!empty($other_questions)) {
- foreach ($other_questions as $question_id => $value) {
- $where_conditions = array(
- 'QST_ID' => $question_id,
- 'REG_ID' => $registration->ID(),
- );
- $possibly_new_values = array(
- 'ANS_value' => $value,
- );
- $answer = EEM_Answer::instance()->get_one(array($where_conditions));
- if ($answer instanceof EE_Answer) {
- $success = $answer->save($possibly_new_values);
- } else {
- //insert it then
- $cols_n_vals = array_merge($where_conditions, $possibly_new_values);
- $answer = EE_Answer::new_instance($cols_n_vals);
- $success = $answer->save();
- }
- }
- }
- $attendee->save();
- $attendee->_add_relation_to($registration, 'Registration');
- $total_line_item->save_this_and_descendants_to_txn();
- $grand_total = $total_line_item->recalculate_total_including_taxes();
- $payment = EE_Payment::new_instance(
- array(
- 'TXN_ID' => $transaction->ID(),
- 'STS_ID' => 'PAP',
- 'PAY_source' => EEM_Payment_Method::scope_admin,
- 'PMD_ID' => $payment_method->ID(),
- 'PAY_amount' => $grand_total,
- ));
- $payment->save();
- $registration_processor->update_registration_final_prices($transaction);
- if ($registration->set_status(EEM_Registration::status_id_approved)) {
- $registration->save();
- }
- }
- /** @type EE_Transaction_Payments $transaction_payments */
- $transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments');
- // maybe update status, and make sure to save transaction if not done already
- if (!$transaction_payments->calculate_total_payments_and_update_status($transaction)) {
- $transaction->save();
- }
- return $registration;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement