/**
  * Process the checkout after the confirm order button is pressed
  *
  * @access public
  * @return void
  */
 public function process_checkout()
 {
     global $wpdb, $current_user;
     wp_verify_nonce($_POST['_wpnonce'], 'woocommerce-process_checkout');
     if (!defined('WOOCOMMERCE_CHECKOUT')) {
         define('WOOCOMMERCE_CHECKOUT', true);
     }
     // Prevent timeout
     @set_time_limit(0);
     do_action('woocommerce_before_checkout_process');
     if (sizeof(WC()->cart->get_cart()) == 0) {
         wc_add_notice(sprintf(__('Sorry, your session has expired. <a href="%s" class="wc-backward">Return to homepage</a>', 'woocommerce'), home_url()), 'error');
     }
     do_action('woocommerce_checkout_process');
     // Checkout fields (not defined in checkout_fields)
     $this->posted['terms'] = isset($_POST['terms']) ? 1 : 0;
     $this->posted['createaccount'] = isset($_POST['createaccount']) ? 1 : 0;
     $this->posted['payment_method'] = isset($_POST['payment_method']) ? stripslashes($_POST['payment_method']) : '';
     $this->posted['shipping_method'] = isset($_POST['shipping_method']) ? $_POST['shipping_method'] : '';
     $this->posted['ship_to_different_address'] = isset($_POST['ship_to_different_address']) ? true : false;
     if (isset($_POST['shiptobilling'])) {
         _deprecated_argument('WC_Checkout::process_checkout()', '2.1', 'The "shiptobilling" field is deprecated. THe template files are out of date');
         $this->posted['ship_to_different_address'] = $_POST['shiptobilling'] ? false : true;
     }
     // Ship to billing only option
     if (WC()->cart->ship_to_billing_address_only()) {
         $this->posted['ship_to_different_address'] = false;
     }
     // Update customer shipping and payment method to posted method
     $chosen_shipping_methods = WC()->session->get('chosen_shipping_methods');
     if (isset($this->posted['shipping_method']) && is_array($this->posted['shipping_method'])) {
         foreach ($this->posted['shipping_method'] as $i => $value) {
             $chosen_shipping_methods[$i] = wc_clean($value);
         }
     }
     WC()->session->set('chosen_shipping_methods', $chosen_shipping_methods);
     WC()->session->set('chosen_payment_method', $this->posted['payment_method']);
     // Note if we skip shipping
     $skipped_shipping = false;
     // Get posted checkout_fields and do validation
     foreach ($this->checkout_fields as $fieldset_key => $fieldset) {
         // Skip shipping if not needed
         if ($fieldset_key == 'shipping' && ($this->posted['ship_to_different_address'] == false || !WC()->cart->needs_shipping())) {
             $skipped_shipping = true;
             continue;
         }
         // Ship account if not needed
         if ($fieldset_key == 'account' && (is_user_logged_in() || $this->must_create_account == false && empty($this->posted['createaccount']))) {
             continue;
         }
         foreach ($fieldset as $key => $field) {
             if (!isset($field['type'])) {
                 $field['type'] = 'text';
             }
             // Get Value
             switch ($field['type']) {
                 case "checkbox":
                     $this->posted[$key] = isset($_POST[$key]) ? 1 : 0;
                     break;
                 case "multiselect":
                     $this->posted[$key] = isset($_POST[$key]) ? implode(', ', array_map('wc_clean', $_POST[$key])) : '';
                     break;
                 case "textarea":
                     $this->posted[$key] = isset($_POST[$key]) ? wp_strip_all_tags(wp_check_invalid_utf8(stripslashes($_POST[$key]))) : '';
                     break;
                 default:
                     $this->posted[$key] = isset($_POST[$key]) ? wc_clean($_POST[$key]) : '';
                     break;
             }
             // Hooks to allow modification of value
             $this->posted[$key] = apply_filters('woocommerce_process_checkout_' . sanitize_title($field['type']) . '_field', $this->posted[$key]);
             $this->posted[$key] = apply_filters('woocommerce_process_checkout_field_' . $key, $this->posted[$key]);
             // Validation: Required fields
             if (isset($field['required']) && $field['required'] && empty($this->posted[$key])) {
                 wc_add_notice('<strong>' . $field['label'] . '</strong> ' . __('is a required field.', 'woocommerce'), 'error');
             }
             if (!empty($this->posted[$key])) {
                 // Validation rules
                 if (!empty($field['validate']) && is_array($field['validate'])) {
                     foreach ($field['validate'] as $rule) {
                         switch ($rule) {
                             case 'postcode':
                                 $this->posted[$key] = strtoupper(str_replace(' ', '', $this->posted[$key]));
                                 if (!WC_Validation::is_postcode($this->posted[$key], $_POST[$fieldset_key . '_country'])) {
                                     wc_add_notice(__('Please enter a valid postcode/ZIP.', 'woocommerce'), 'error');
                                 } else {
                                     $this->posted[$key] = wc_format_postcode($this->posted[$key], $_POST[$fieldset_key . '_country']);
                                 }
                                 break;
                             case 'phone':
                                 $this->posted[$key] = wc_format_phone_number($this->posted[$key]);
                                 if (!WC_Validation::is_phone($this->posted[$key])) {
                                     wc_add_notice('<strong>' . $field['label'] . '</strong> ' . __('is not a valid phone number.', 'woocommerce'), 'error');
                                 }
                                 break;
                             case 'email':
                                 $this->posted[$key] = strtolower($this->posted[$key]);
                                 if (!is_email($this->posted[$key])) {
                                     wc_add_notice('<strong>' . $field['label'] . '</strong> ' . __('is not a valid email address.', 'woocommerce'), 'error');
                                 }
                                 break;
                             case 'state':
                                 // Get valid states
                                 $valid_states = WC()->countries->get_states($_POST[$fieldset_key . '_country']);
                                 if ($valid_states) {
                                     $valid_state_values = array_flip(array_map('strtolower', $valid_states));
                                 }
                                 // Convert value to key if set
                                 if (isset($valid_state_values[strtolower($this->posted[$key])])) {
                                     $this->posted[$key] = $valid_state_values[strtolower($this->posted[$key])];
                                 }
                                 // Only validate if the country has specific state options
                                 if ($valid_states && sizeof($valid_states) > 0) {
                                     if (!in_array($this->posted[$key], array_keys($valid_states))) {
                                         wc_add_notice('<strong>' . $field['label'] . '</strong> ' . __('is not valid. Please enter one of the following:', 'woocommerce') . ' ' . implode(', ', $valid_states), 'error');
                                     }
                                 }
                                 break;
                         }
                     }
                 }
             }
         }
     }
     // Update customer location to posted location so we can correctly check available shipping methods
     if (isset($this->posted['billing_country'])) {
         WC()->customer->set_country($this->posted['billing_country']);
     }
     if (isset($this->posted['billing_state'])) {
         WC()->customer->set_state($this->posted['billing_state']);
     }
     if (isset($this->posted['billing_postcode'])) {
         WC()->customer->set_postcode($this->posted['billing_postcode']);
     }
     // Shipping Information
     if (!$skipped_shipping) {
         // Update customer location to posted location so we can correctly check available shipping methods
         if (isset($this->posted['shipping_country'])) {
             WC()->customer->set_shipping_country($this->posted['shipping_country']);
         }
         if (isset($this->posted['shipping_state'])) {
             WC()->customer->set_shipping_state($this->posted['shipping_state']);
         }
         if (isset($this->posted['shipping_postcode'])) {
             WC()->customer->set_shipping_postcode($this->posted['shipping_postcode']);
         }
     } else {
         // Update customer location to posted location so we can correctly check available shipping methods
         if (isset($this->posted['billing_country'])) {
             WC()->customer->set_shipping_country($this->posted['billing_country']);
         }
         if (isset($this->posted['billing_state'])) {
             WC()->customer->set_shipping_state($this->posted['billing_state']);
         }
         if (isset($this->posted['billing_postcode'])) {
             WC()->customer->set_shipping_postcode($this->posted['billing_postcode']);
         }
     }
     // Update cart totals now we have customer address
     WC()->cart->calculate_totals();
     // Terms
     if (!isset($_POST['woocommerce_checkout_update_totals']) && empty($this->posted['terms']) && wc_get_page_id('terms') > 0) {
         wc_add_notice(__('You must accept our Terms &amp; Conditions.', 'woocommerce'), 'error');
     }
     if (WC()->cart->needs_shipping()) {
         if (!in_array(WC()->customer->get_shipping_country(), array_keys(WC()->countries->get_shipping_countries()))) {
             wc_add_notice(sprintf(__('Unfortunately <strong>we do not ship to %s</strong>. Please enter an alternative shipping address.', 'woocommerce'), WC()->countries->shipping_to_prefix() . ' ' . WC()->customer->get_shipping_country()), 'error');
         }
         // Validate Shipping Methods
         $packages = WC()->shipping->get_packages();
         $this->shipping_methods = WC()->session->get('chosen_shipping_methods');
         foreach ($packages as $i => $package) {
             if (!isset($package['rates'][$this->shipping_methods[$i]])) {
                 wc_add_notice(__('Invalid shipping method.', 'woocommerce'), 'error');
                 $this->shipping_methods[$i] = '';
             }
         }
     }
     if (WC()->cart->needs_payment()) {
         // Payment Method
         $available_gateways = WC()->payment_gateways->get_available_payment_gateways();
         if (!isset($available_gateways[$this->posted['payment_method']])) {
             $this->payment_method = '';
             wc_add_notice(__('Invalid payment method.', 'woocommerce'), 'error');
         } else {
             $this->payment_method = $available_gateways[$this->posted['payment_method']];
             $this->payment_method->validate_fields();
         }
     }
     // Action after validation
     do_action('woocommerce_after_checkout_validation', $this->posted);
     if (!isset($_POST['woocommerce_checkout_update_totals']) && wc_notice_count('error') == 0) {
         try {
             // Customer accounts
             $this->customer_id = apply_filters('woocommerce_checkout_customer_id', get_current_user_id());
             if (!is_user_logged_in() && ($this->must_create_account || !empty($this->posted['createaccount']))) {
                 $username = !empty($this->posted['account_username']) ? $this->posted['account_username'] : '';
                 $password = !empty($this->posted['account_password']) ? $this->posted['account_password'] : '';
                 $new_customer = wc_create_new_customer($this->posted['billing_email'], $username, $password);
                 if (is_wp_error($new_customer)) {
                     throw new Exception($new_customer->get_error_message());
                 }
                 $this->customer_id = $new_customer;
                 wc_set_customer_auth_cookie($this->customer_id);
                 // As we are now logged in, checkout will need to refresh to show logged in data
                 WC()->session->set('reload_checkout', true);
                 // Add customer info from other billing fields
                 if ($this->posted['billing_first_name'] && apply_filters('woocommerce_checkout_update_customer_data', true, $this)) {
                     $userdata = array('ID' => $this->customer_id, 'first_name' => $this->posted['billing_first_name'] ? $this->posted['billing_first_name'] : '', 'last_name' => $this->posted['billing_last_name'] ? $this->posted['billing_last_name'] : '', 'display_name' => $this->posted['billing_first_name'] ? $this->posted['billing_first_name'] : '');
                     wp_update_user(apply_filters('woocommerce_checkout_customer_userdata', $userdata, $this));
                 }
             }
             // Do a final stock check at this point
             $this->check_cart_items();
             // Abort if errors are present
             if (wc_notice_count('error') > 0) {
                 throw new Exception();
             }
             $order_id = $this->create_order();
             do_action('woocommerce_checkout_order_processed', $order_id, $this->posted);
             // Process payment
             if (WC()->cart->needs_payment()) {
                 // Store Order ID in session so it can be re-used after payment failure
                 WC()->session->order_awaiting_payment = $order_id;
                 // Process Payment
                 $result = $available_gateways[$this->posted['payment_method']]->process_payment($order_id);
                 // Redirect to success/confirmation/payment page
                 if ($result['result'] == 'success') {
                     $result = apply_filters('woocommerce_payment_successful_result', $result, $order_id);
                     if (is_ajax()) {
                         echo '<!--WC_START-->' . json_encode($result) . '<!--WC_END-->';
                         exit;
                     } else {
                         wp_redirect($result['redirect']);
                         exit;
                     }
                 }
             } else {
                 if (empty($order)) {
                     $order = new WC_Order($order_id);
                 }
                 // No payment was required for order
                 $order->payment_complete();
                 // Empty the Cart
                 WC()->cart->empty_cart();
                 // Get redirect
                 $return_url = $order->get_checkout_order_received_url();
                 // Redirect to success/confirmation/payment page
                 if (is_ajax()) {
                     echo '<!--WC_START-->' . json_encode(array('result' => 'success', 'redirect' => apply_filters('woocommerce_checkout_no_payment_needed_redirect', $return_url, $order))) . '<!--WC_END-->';
                     exit;
                 } else {
                     wp_safe_redirect(apply_filters('woocommerce_checkout_no_payment_needed_redirect', $return_url, $order));
                     exit;
                 }
             }
         } catch (Exception $e) {
             if (!empty($e)) {
                 wc_add_notice($e->getMessage(), 'error');
             }
         }
     }
     // endif
     // If we reached this point then there were errors
     if (is_ajax()) {
         ob_start();
         wc_print_notices();
         $messages = ob_get_clean();
         echo '<!--WC_START-->' . json_encode(array('result' => 'failure', 'messages' => $messages, 'refresh' => isset(WC()->session->refresh_totals) ? 'true' : 'false', 'reload' => isset(WC()->session->reload_checkout) ? 'true' : 'false')) . '<!--WC_END-->';
         unset(WC()->session->refresh_totals, WC()->session->reload_checkout);
         exit;
     }
 }
 /**
  * Process the registration form.
  */
 public static function process_registration()
 {
     $nonce_value = isset($_POST['_wpnonce']) ? $_POST['_wpnonce'] : '';
     $nonce_value = isset($_POST['woocommerce-register-nonce']) ? $_POST['woocommerce-register-nonce'] : $nonce_value;
     if (!empty($_POST['register']) && wp_verify_nonce($nonce_value, 'woocommerce-register')) {
         $username = '******' === get_option('woocommerce_registration_generate_username') ? $_POST['username'] : '';
         $password = '******' === get_option('woocommerce_registration_generate_password') ? $_POST['password'] : '';
         $email = $_POST['email'];
         try {
             $validation_error = new WP_Error();
             $validation_error = apply_filters('woocommerce_process_registration_errors', $validation_error, $username, $password, $email);
             if ($validation_error->get_error_code()) {
                 throw new Exception($validation_error->get_error_message());
             }
             // Anti-spam trap
             if (!empty($_POST['email_2'])) {
                 throw new Exception(__('Anti-spam field was filled in.', 'woocommerce'));
             }
             $new_customer = wc_create_new_customer(sanitize_email($email), wc_clean($username), $password);
             if (is_wp_error($new_customer)) {
                 throw new Exception($new_customer->get_error_message());
             }
             if (apply_filters('woocommerce_registration_auth_new_customer', true, $new_customer)) {
                 wc_set_customer_auth_cookie($new_customer);
             }
             wp_safe_redirect(apply_filters('woocommerce_registration_redirect', wp_get_referer() ? wp_get_referer() : wc_get_page_permalink('myaccount')));
             exit;
         } catch (Exception $e) {
             wc_add_notice('<strong>' . __('Error:', 'woocommerce') . '</strong> ' . $e->getMessage(), 'error');
         }
     }
 }
/**
 * @deprecated
 */
function woocommerce_set_customer_auth_cookie($customer_id)
{
    wc_set_customer_auth_cookie($customer_id);
}
 /**
  * Regular checkout process
  */
 function regular_checkout($posted)
 {
     if ($posted['payment_method'] == 'paypal_express' && wc_notice_count('error') == 0) {
         if (!is_user_logged_in() && (get_option('woocommerce_enable_guest_checkout') != 'yes' || isset($posted['createaccount']) && $posted['createaccount'] == 1)) {
             $this->customer_id = apply_filters('woocommerce_checkout_customer_id', get_current_user_id());
             $username = !empty($posted['account_username']) ? $posted['account_username'] : '';
             $password = !empty($posted['account_password']) ? $posted['account_password'] : '';
             $new_customer = wc_create_new_customer($posted['billing_email'], $username, $password);
             if (is_wp_error($new_customer)) {
                 throw new Exception($new_customer->get_error_message());
             }
             $this->customer_id = $new_customer;
             wc_set_customer_auth_cookie($this->customer_id);
             // As we are now logged in, checkout will need to refresh to show logged in data
             WC()->session->set('reload_checkout', true);
             // Also, recalculate cart totals to reveal any role-based discounts that were unavailable before registering
             WC()->cart->calculate_totals();
             // Add customer info from other billing fields
             if ($posted['billing_first_name'] && apply_filters('woocommerce_checkout_update_customer_data', true, $this)) {
                 $userdata = array('ID' => $this->customer_id, 'first_name' => $posted['billing_first_name'] ? $posted['billing_first_name'] : '', 'last_name' => $posted['billing_last_name'] ? $posted['billing_last_name'] : '', 'display_name' => $posted['billing_first_name'] ? $posted['billing_first_name'] : '');
                 wp_update_user(apply_filters('woocommerce_checkout_customer_userdata', $userdata, $this));
             }
         }
         $this->set_session('checkout_form', serialize($posted));
         $this->paypal_express_checkout($posted);
         return;
     }
 }
예제 #5
0
 /**
  * Create a new customer account if needed.
  * @param  array $data
  * @throws Exception
  */
 protected function process_customer($data)
 {
     $customer_id = get_current_user_id();
     if (!is_user_logged_in() && ($this->is_registration_required() || !empty($data['createaccount']))) {
         $username = !empty($data['account_username']) ? $data['account_username'] : '';
         $password = !empty($data['account_password']) ? $data['account_password'] : '';
         $customer_id = wc_create_new_customer($data['billing_email'], $username, $password);
         if (is_wp_error($customer_id)) {
             throw new Exception($customer_id->get_error_message());
         }
         wp_set_current_user($customer_id);
         wc_set_customer_auth_cookie($customer_id);
         // As we are now logged in, checkout will need to refresh to show logged in data
         WC()->session->set('reload_checkout', true);
         // Also, recalculate cart totals to reveal any role-based discounts that were unavailable before registering
         WC()->cart->calculate_totals();
     }
     // Add customer info from other fields.
     if ($customer_id && apply_filters('woocommerce_checkout_update_customer_data', true, $this)) {
         $customer = new WC_Customer($customer_id);
         $customer->set_first_name($data['billing_first_name']);
         $customer->set_last_name($data['billing_last_name']);
         foreach ($data as $key => $value) {
             if (is_callable(array($customer, "set_{$key}"))) {
                 $customer->{"set_{$key}"}($value);
             }
         }
         $customer->save();
     }
     do_action('woocommerce_checkout_update_user_meta', $customer_id, $data);
 }
 /**
  * Process authenticated user's profile
  *
  * @since 1.0
  * @param WC_Social_Login_Provider_profile $profile
  * @return int the user ID
  */
 public function process_profile($profile)
 {
     global $wpdb;
     $user = null;
     $new_customer = false;
     $found_via = null;
     // Look up if the user already exists on WP
     // First, try to identify user based on the social identifier
     $user_id = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value = %s", '_wc_social_login_' . $this->id . '_uid', $profile->get_uid()));
     if ($user_id) {
         $user = get_user_by('id', $user_id);
         if ($user) {
             $found_via = 'uid';
         }
     }
     // Fall back to email - user may already have an account on WooCommerce with the
     // same email as in their social profile
     if (!$user && $profile->has_email()) {
         $user = get_user_by('email', $profile->get_email());
         if ($user) {
             $found_via = 'email';
         }
     }
     // If a user is already logged in...
     if (is_user_logged_in()) {
         // ...and a user matching the social profile was found,
         // check that the logged in user and found user are the same.
         // This happens when user is linking a new social profile to their account.
         if ($user && get_current_user_id() !== $user->ID) {
             if ('uid' == $found_via) {
                 wc_add_notice($this->get_notice_text('account_already_linked'), 'error');
             } else {
                 wc_add_notice($this->get_notice_text('account_already_exists'), 'error');
             }
             return 0;
         }
         // If the social profile is not linked to any user accounts,
         // use the currently logged in user as the customer
         if (!$user) {
             $user = get_user_by('id', get_current_user_id());
         }
     }
     // Check if a user is found via email and not it one of the allowed roles
     if ($user && 'email' === $found_via && !in_array($user->roles[0], apply_filters('wc_social_login_find_by_email_allowed_user_roles', array('subscriber', 'customer')))) {
         return new WP_Error('wc-social-login-restricted-role-error', __('An account with this email address already exists and has a restricted role.', WC_Social_Login::TEXT_DOMAIN));
     }
     // If no user was found, create one
     if (!$user) {
         $user_id = $this->create_new_customer($profile);
         if (is_wp_error($user_id)) {
             // log error messages and response data
             wc_social_login()->log(sprintf('Error: %s, Response: %s', 'registration-error', $user_id->get_error_message('registration-error')));
             return new WP_Error('wc-social-login-registration-error', $user_id->get_error_message('registration-error'));
         }
         $user = get_user_by('id', $user_id);
         // indicate that a new user was created
         $new_customer = true;
     }
     // Update customer's WP user profile and billing details
     $profile->update_customer_profile($user->ID, $new_customer);
     // Log user in or add account linked notice for a logged in user
     if (!is_user_logged_in()) {
         if (!($message = apply_filters('wc_social_login_set_auth_cookie', '', $user))) {
             wc_set_customer_auth_cookie($user->ID);
             // Store login timestamp
             update_user_meta($user->ID, '_wc_social_login_' . $this->get_id() . '_login_timestamp', current_time('timestamp'));
             update_user_meta($user->ID, '_wc_social_login_' . $this->get_id() . '_login_timestamp_gmt', time());
             /**
              * User authenticated via social login.
              *
              * @since 1.0
              * @param int $user_id ID of the user
              * @param string $provider_id Social Login provider ID
              */
             do_action('wc_social_login_user_authenticated', $user->ID, $this->get_id());
         } else {
             wc_add_notice($message, 'notice');
         }
     } else {
         wc_add_notice($this->get_notice_text('account_linked'), 'notice');
     }
     return $user->ID;
 }
 /**
  * Process the registration form.
  */
 public function process_registration()
 {
     if (!empty($_POST['register'])) {
         wp_verify_nonce($_POST['register'], 'woocommerce-register');
         if ('no' === get_option('woocommerce_registration_generate_username')) {
             $_username = $_POST['username'];
         } else {
             $_username = '';
         }
         if ('no' === get_option('woocommerce_registration_generate_password')) {
             $_password = $_POST['password'];
         } else {
             $_password = '';
         }
         try {
             $validation_error = new WP_Error();
             $validation_error = apply_filters('woocommerce_process_registration_errors', $validation_error, $_username, $_password, $_POST['email']);
             if ($validation_error->get_error_code()) {
                 throw new Exception('<strong>' . __('Error', 'woocommerce') . ':</strong> ' . $validation_error->get_error_message());
             }
         } catch (Exception $e) {
             wc_add_notice($e->getMessage(), 'error');
             return;
         }
         $username = !empty($_username) ? wc_clean($_username) : '';
         $email = !empty($_POST['email']) ? sanitize_email($_POST['email']) : '';
         $password = $_password;
         // Anti-spam trap
         if (!empty($_POST['email_2'])) {
             wc_add_notice('<strong>' . __('ERROR', 'woocommerce') . '</strong>: ' . __('Anti-spam field was filled in.', 'woocommerce'), 'error');
             return;
         }
         $new_customer = wc_create_new_customer($email, $username, $password);
         if (is_wp_error($new_customer)) {
             wc_add_notice($new_customer->get_error_message(), 'error');
             return;
         }
         wc_set_customer_auth_cookie($new_customer);
         // Redirect
         if (wp_get_referer()) {
             $redirect = esc_url(wp_get_referer());
         } else {
             $redirect = esc_url(get_permalink(wc_get_page_id('myaccount')));
         }
         wp_redirect(apply_filters('woocommerce_registration_redirect', $redirect));
         exit;
     }
 }