Advertisement
Guest User

Events Espresso Add new registration

a guest
Dec 13th, 2018
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.10 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Add new registration
  5.  *
  6.  * @return WP_Error|boolean|EE_Registration
  7.  */
  8. function add_new_registration($data, $check_referer = true, $reg_number = 1, $quantity = 1)
  9. {
  10.  
  11.     if ($check_referer && !check_ajax_referer('add_new_registration', false, false)) {
  12.         return new WP_Error("invalid_token", "Invalid  or expired session");
  13.     }
  14.  
  15.     extract($data);
  16.  
  17.     if (isset($quantity) && $quantity > 0) {
  18.         $quantity = $quantity;
  19.     } else {
  20.         $quantity = 1;
  21.     }
  22.  
  23.     if (!EE_Registry::instance()->CAP->current_user_can('ee_edit_registrations', 'espresso_registrations_new_registration', $event_id)) {
  24.  
  25.         return new WP_Error("permission_denied", "Permission Denied");
  26.     }
  27.  
  28.     if (empty($first_name)) {
  29.         return new WP_Error("first_name", "First Name is required");
  30.     }
  31.  
  32.     if (empty($last_name)) {
  33.         return new WP_Error("last_name", "Last Name is required");
  34.     }
  35.  
  36.     global $wpdb;
  37.  
  38.     $ticket = EEM_Ticket::instance()->get_one_by_ID($ticket_id);
  39.  
  40.     if (!$ticket instanceof EE_Ticket) {
  41.  
  42.         $event = EEM_Event::instance()->get_one_by_ID($event_id);
  43.  
  44.         if (!$event instanceof EE_Event) {
  45.             return new WP_Error('ticket', 'Invalid Ticket');
  46.         }
  47.  
  48.         $ticket = reset($event->tickets());
  49.  
  50.         if (!$ticket instanceof EE_Ticket) {
  51.             return new WP_Error('ticket', 'Invalid Ticket');
  52.         }
  53.     }
  54.  
  55.     $registration_processor = EE_Registry::instance()->load_class('Registration_Processor');
  56.  
  57.     $payment_method = EEM_Payment_Method::instance()->get_one_by_slug('other');
  58.     $transaction = EE_Transaction::new_instance(array(
  59.         'TXN_timestamp' => time(), //unix timestamp
  60.         'TXN_total' => 0, //txn_total
  61.         'TXN_paid' => 0, //txn_paid
  62.         'STS_ID' => EEM_Transaction::complete_status_code, //sts_id
  63.         'TXN_session_data' => null, //dump of txn session object (we're just going to leave blank here)
  64.         'TXN_hash_salt' => null, //hash salt blank as well
  65.         'PMD_ID' => $payment_method->ID(),
  66.     ));
  67.  
  68.     $transaction->save();
  69.  
  70.     $total_line_item = $transaction->total_line_item();
  71.     $line_item = \EEH_Line_Item::add_ticket_purchase(
  72.         $total_line_item,
  73.         $ticket,
  74.         $quantity
  75.     );
  76.  
  77.     $total_line_item->save_this_and_descendants_to_txn($transaction->ID());
  78.  
  79.     EEH_Line_Item::apply_taxes($total_line_item);
  80.  
  81.     for ($i = 1; $i <= $quantity; $i++) {
  82.  
  83.         $registration = (new \EventEspresso\core\domain\services\registration\CreateRegistrationService())->create(
  84.             $ticket->get_related_event(),
  85.             $transaction,
  86.             $ticket,
  87.             $line_item,
  88.             $i,
  89.             $quantity
  90.         );
  91.  
  92.         $attendee = EE_Attendee::new_instance(array(
  93.             'ATT_fname' => !empty($first_name) ? $first_name : '',
  94.             'ATT_lname' => !empty($last_name) ? $last_name : '',
  95.             'ATT_email' => !empty($email) ? $email : '',
  96.             'ATT_phone' => $phone,
  97.         ));
  98.  
  99.         if (!empty($other_questions)) {
  100.  
  101.             foreach ($other_questions as $question_id => $value) {
  102.  
  103.                 $where_conditions = array(
  104.                     'QST_ID' => $question_id,
  105.                     'REG_ID' => $registration->ID(),
  106.                 );
  107.                 $possibly_new_values = array(
  108.                     'ANS_value' => $value,
  109.                 );
  110.                 $answer = EEM_Answer::instance()->get_one(array($where_conditions));
  111.                 if ($answer instanceof EE_Answer) {
  112.                     $success = $answer->save($possibly_new_values);
  113.                 } else {
  114.                     //insert it then
  115.                     $cols_n_vals = array_merge($where_conditions, $possibly_new_values);
  116.                     $answer = EE_Answer::new_instance($cols_n_vals);
  117.                     $success = $answer->save();
  118.                 }
  119.             }
  120.         }
  121.  
  122.         $attendee->save();
  123.         $attendee->_add_relation_to($registration, 'Registration');
  124.  
  125.         $total_line_item->save_this_and_descendants_to_txn();
  126.  
  127.         $grand_total = $total_line_item->recalculate_total_including_taxes();
  128.  
  129.         $payment = EE_Payment::new_instance(
  130.             array(
  131.                 'TXN_ID' => $transaction->ID(),
  132.                 'STS_ID' => 'PAP',
  133.                 'PAY_source' => EEM_Payment_Method::scope_admin,
  134.                 'PMD_ID' => $payment_method->ID(),
  135.                 'PAY_amount' => $grand_total,
  136.             ));
  137.  
  138.         $payment->save();
  139.  
  140.         $registration_processor->update_registration_final_prices($transaction);
  141.  
  142.         if ($registration->set_status(EEM_Registration::status_id_approved)) {
  143.             $registration->save();
  144.         }
  145.  
  146.     }
  147.  
  148.     /** @type EE_Transaction_Payments $transaction_payments */
  149.     $transaction_payments = EE_Registry::instance()->load_class('Transaction_Payments');
  150.     // maybe update status, and make sure to save transaction if not done already
  151.     if (!$transaction_payments->calculate_total_payments_and_update_status($transaction)) {
  152.         $transaction->save();
  153.     }
  154.  
  155.     return $registration;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement