function ajax_wpshop_create_new_customer() { $result = ''; if ($_POST['attribute'][$_REQUEST['billing_address']]['varchar']['address_user_email'] != null) { /** Crerate the new customer user account */ $username = $_REQUEST['attribute'][$_REQUEST['billing_address']]['varchar']['address_user_email']; $password = wp_generate_password($length = 12, $include_standard_special_chars = false); $email = $_REQUEST['attribute'][$_REQUEST['billing_address']]['varchar']['address_user_email']; if (!empty($username) && !username_exists($username) && !empty($email) && !email_exists($email)) { $user_id = wp_create_user($username, $password, $email); $_REQUEST['user']['customer_id'] = $user_id; /** Save addresses */ $billing_set_infos = get_option('wpshop_billing_address'); $shipping_set_infos = get_option('wpshop_shipping_address_choice'); /** If it's same addresses for Shipping and Billing */ if (isset($_REQUEST['shiptobilling']) && $_REQUEST['shiptobilling'] == "on") { wpshop_account::same_billing_and_shipping_address($_REQUEST['billing_address'], $_REQUEST['shipping_address']); } if (!empty($_POST['billing_address'])) { wps_address::save_address_infos($_REQUEST['billing_address']); } if (!empty($_POST['shipping_address'])) { wps_address::save_address_infos($_REQUEST['shipping_address']); } $result = json_encode(array(true, __('Customer created', 'wpshop'), $user_id)); } else { $result = json_encode(array(false, __('A customer account is already created with this email address', 'wpshop'))); } } else { $result = json_encode(array(false, __('An email address is required', 'wpshop'))); } echo $result; die; }
/** * Save informations for current entity */ public static function save_entities_custom_informations($post) { global $wpdb; global $wpshop_account; $post_id = !empty($_REQUEST['post_ID']) ? intval(wpshop_tools::varSanitizer($_REQUEST['post_ID'])) : null; if (!empty($post_id) && empty($_POST['edit_other_thing']) || !empty($_REQUEST['post_ID']) && !(bool) $_POST['edit_other_thing'] && get_post_type($_REQUEST['post_ID']) != WPSHOP_NEWTYPE_IDENTIFIER_ORDER) { $current_post_type = get_post_type($post_id); /* Vérification de l'existence de l'envoi de l'identifiant du set d'attribut */ if (!empty($_REQUEST[$current_post_type . '_attribute_set_id'])) { $attribute_set_id = intval(wpshop_tools::varSanitizer($_REQUEST[$current_post_type . '_attribute_set_id'])); $attribet_set_infos = wpshop_attributes_set::getElement($attribute_set_id, "'valid'", 'id'); if ($attribet_set_infos->entity == $_REQUEST['post_type']) { /* Enregistrement de l'identifiant du set d'attribut associé à l'entité */ update_post_meta($post_id, sprintf(WPSHOP_ATTRIBUTE_SET_ID_META_KEY, $current_post_type), $attribute_set_id); /* Enregistrement de tous les attributs */ if (!empty($_REQUEST[$current_post_type . '_attribute'])) { /* Traduction des virgule en point pour la base de donnees */ if (!empty($_REQUEST[$current_post_type . '_attribute']['decimal'])) { foreach ($_REQUEST[$current_post_type . '_attribute']['decimal'] as $attributeName => $attributeValue) { if (!is_array($attributeValue)) { $_REQUEST[$current_post_type . '_attribute']['decimal'][$attributeName] = str_replace(',', '.', $_REQUEST[$current_post_type . '_attribute']['decimal'][$attributeName]); } } } /* Enregistrement des valeurs des différents attributs */ wpshop_attributes::saveAttributeForEntity($_REQUEST[$current_post_type . '_attribute'], wpshop_entities::get_entity_identifier_from_code($current_post_type), $post_id, WPSHOP_CURRENT_LOCALE); /* Enregistrement des valeurs des attributs dans les metas de l'entité => Permet de profiter de la recherche native de wordpress */ $productMetaDatas = array(); foreach ($_REQUEST[$current_post_type . '_attribute'] as $attributeType => $attributeValues) { foreach ($attributeValues as $attributeCode => $attributeValue) { $productMetaDatas[$attributeCode] = $attributeValue; } } update_post_meta($_REQUEST['post_ID'], WPSHOP_PRODUCT_ATTRIBUTE_META_KEY, $productMetaDatas); } } } if (!empty($_REQUEST['attribute'])) { $current_id = array(); foreach ($_REQUEST['attribute'] as $key => $values) { $ad_id = ''; $addresses_id = get_post_meta($_REQUEST['post_ID'], '_wpshop_attached_address', true); if (!empty($addresses_id)) { foreach ($addresses_id as $address_id) { $address_type = get_post_meta($address_id, '_wpshop_address_attribute_set_id', true); if ($address_type == $key) { $ad_id = $address_id; } } } $_REQUEST['item_id'] = $ad_id; $result = wps_address::save_address_infos($key); $current_id[] = $result['current_id']; } update_post_meta($_REQUEST['post_ID'], '_wpshop_attached_address', $current_id); } else { $current_id = array(); if (!empty($_REQUEST['address_type'])) { foreach ($_REQUEST['address_type'] as $key => $value) { $current_id[] = $value; } } update_post_meta($_REQUEST['post_ID'], '_wpshop_entity_attached_address', $current_id); } } /** Save price infos **/ if (!empty($_REQUEST) && !empty($_REQUEST['post_ID']) && !empty($_REQUEST['post_type']) && $_REQUEST['post_type'] == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT) { } flush_rewrite_rules(); }
/** * AJAX - Création d'un nouveau client / Create a new customer */ function create_customer() { global $wpdb; $response = array('status' => false, 'output' => __('An error occured while saving customer', 'wpshop'), 'customer_id' => -1); /** Check if a attribute set id have been sended in order to check if therer are some check to do on sended input */ $customer_attributes = wpshop_attributes_set::getAttributeSetDetails($_POST['wps-customer-account-set-id'], "'valid'"); /** Read sended values for checking */ $email_founded = false; $email_field = $last_name_field = $first_name_field = ''; if (!empty($_POST)) { foreach ($_POST['attribute'] as $attribute_type => $attributes) { foreach ($attributes as $attribute_code => $attribute_value) { $query = $wpdb->prepare("SELECT frontend_verification FROM " . WPSHOP_DBT_ATTRIBUTE . " WHERE code = %s", $attribute_code); $current_attribute = $wpdb->get_var($query); if ('email' == $current_attribute) { $email_founded = true; $email_field = $attribute_code; $email_field_type = $attribute_type; } else { if (strpos($attribute_code, 'last_name')) { $last_name_field = $attribute_code; $last_name_field_type = $attribute_type; } else { if (strpos($attribute_code, 'first_name')) { $first_name_field = $attribute_code; $first_name_field_type = $attribute_type; } } } } } } /** Define customer email field value */ $customer_email = $_POST['attribute'][$email_field_type][$email_field]; $customer_last_name = !empty($_POST) && !empty($_POST['attribute']) && !empty($_POST['attribute'][$last_name_field_type]) && !empty($_POST['attribute'][$last_name_field_type][$last_name_field]) ? $_POST['attribute'][$last_name_field_type][$last_name_field] : ''; $customer_first_name = !empty($_POST) && !empty($_POST['attribute']) && !empty($_POST['attribute'][$first_name_field_type]) && !empty($_POST['attribute'][$first_name_field_type][$first_name_field]) ? $_POST['attribute'][$first_name_field_type][$first_name_field] : ''; if ($email_founded && is_email($customer_email)) { /** Check if current e-mail address does not already exists */ $user_id = username_exists($customer_email); if (empty($user_id)) { /** Create the user with a random password */ $random_password = wp_generate_password(12, false); $user_id = wp_create_user($customer_email, $random_password, $customer_email); if (!is_wp_error($user_id)) { update_user_meta($user_id, 'last_name', $customer_last_name); update_user_meta($user_id, 'first_name', $customer_first_name); /** Build a response for customer first letter - Specific action (POS) */ if (!empty($customer_last_name)) { $field_for_letter = $customer_last_name; } elseif (!empty($customer_first_name)) { $field_for_letter = $customer_first_name; } else { $field_for_letter = $customer_email; } $response['letter'] = substr($field_for_letter, 0, 1); /** Build response */ $response['status'] = true; $response['output'] = __('Customer created succesfully', 'wpshop'); /** Create customer address from sended data **/ $_REQUEST['user']['customer_id'] = $user_id; $attribute_to_save = $_POST['attribute']; unset($_POST['attribute']); $_POST['attribute'][$_POST['wps-customer-account-set-id']] = $attribute_to_save; wps_address::save_address_infos($_POST['wps-customer-account-set-id']); } } else { $response['output'] = __('An account is already created with this e-mail address', 'wpshop'); } $response['customer_id'] = $user_id; } else { $response['output'] = __('An email address is required', 'wpshop'); } wp_die(json_encode($response)); }
function wps_save_first_address() { global $wpshop; $errors = ''; $status = false; $result = ''; $validate_address_2 = true; $shipping_address_option = get_option('wpshop_shipping_address_choice'); $billing_address_option = get_option('wpshop_billing_address'); /** Validate Shipping address **/ $group = wps_address::get_addresss_form_fields_by_type($shipping_address_option['choice']); foreach ($group as $attribute_sets) { foreach ($attribute_sets as $attribute_set_field) { $validate = $wpshop->validateForm($attribute_set_field['content'], $_POST['attribute'][$shipping_address_option['choice']], 'address_edition'); } } if (empty($_POST['shiptobilling'])) { $group = wps_address::get_addresss_form_fields_by_type($billing_address_option['choice']); foreach ($group as $attribute_sets) { foreach ($attribute_sets as $attribute_set_field) { $validate_address_2 = $wpshop->validateForm($attribute_set_field['content'], $_POST['attribute'][$billing_address_option['choice']], 'address_edition'); } } } if ($validate && $validate_address_2) { $return = wps_address::save_address_infos($shipping_address_option['choice']); if (!empty($return) && !empty($return['current_id'])) { $_SESSION['shipping_address'] = $return['current_id']; } if (!empty($_POST['shiptobilling'])) { self::same_shipping_as_billing($_POST['billing_address'], $_POST['shipping_address']); $return = wps_address::save_address_infos($billing_address_option['choice']); if (!empty($return) && !empty($return['current_id'])) { $_SESSION['billing_address'] = $return['current_id']; } } else { $return = wps_address::save_address_infos($billing_address_option['choice']); if (!empty($return) && !empty($return['current_id'])) { $_SESSION['billing_address'] = $return['current_id']; } } $status = true; $result = self::get_addresses(); } else { if (!empty($wpshop->errors)) { $result = '<div class="wps-alert wps-alert-error">' . __('Some errors have been detected', 'wpshop') . ' : <ul>'; foreach ($wpshop->errors as $error) { $result .= '<li>' . $error . '</li>'; } $result .= '</div>'; } } $response = array($status, $result); echo json_encode($response); die; }