Пример #1
0
function ajax_wpshop_change_address()
{
    $address_id = !empty($_POST['address_id']) ? wpshop_tools::varSanitizer($_POST['address_id']) : null;
    $address_type = !empty($_POST['address_type']) ? wpshop_tools::varSanitizer($_POST['address_type']) : null;
    $is_allowed_destination = true;
    if (!empty($address_id) && !empty($address_type)) {
        //Check if it's an allowed address for shipping
        $checkout_payment_button = '';
        $cart_type = !empty($_SESSION['cart']['cart_type']) && $_SESSION['cart']['cart_type'] == 'quotation' ? 'quotation' : 'cart';
        $is_allowed_destination = true;
        //wpshop_shipping_configuration::is_allowed_country ( $address_id );
        if ($is_allowed_destination) {
            $available_payement_method = wpshop_payment::display_payment_methods_choice_form(0, $cart_type);
            //if(!empty($available_payement_method[1]['paypal']) || !empty($available_payement_method[1]['banktransfer']) || !empty($available_payement_method[1]['checks']) || WPSHOP_PAYMENT_METHOD_CIC || !empty($available_payement_method[1]['cic']) || ($cart_type == 'quotation')) {
            if (!empty($available_payement_method[0])) {
                if ($cart_type == 'quotation') {
                    $checkout_payment_button = wpshop_display::display_template_element('wpshop_checkout_page_quotation_validation_button', array());
                } else {
                    $checkout_payment_button = wpshop_display::display_template_element('wpshop_checkout_page_validation_button', array());
                }
            }
        } else {
            $checkout_payment_button = wpshop_display::display_template_element('wpshop_checkout_page_impossible_to_order', array());
        }
        if ($address_type == 'billing_address') {
            $billing_option = get_option('wpshop_billing_address');
            $address_option = $billing_option['choice'];
        } else {
            $shipping_address_option = get_option('wpshop_shipping_address_choice');
            $address_option = $shipping_address_option['choice'];
        }
        $add = wps_address::get_addresss_form_fields_by_type($address_option);
        $address_infos = get_post_meta($address_id, '_' . WPSHOP_NEWTYPE_IDENTIFIER_ADDRESS . '_metadata', true);
        $retour = wpshop_account::display_an_address($add, $address_infos, $address_id);
        $_SESSION[$address_type] = $address_id;
        $edit_link = '<a href="' . get_permalink(wpshop_tools::get_page_id(get_option('wpshop_myaccount_page_id'))) . (strpos(get_permalink(wpshop_tools::get_page_id(get_option('wpshop_myaccount_page_id'))), '?') === false ? '?' : '&') . 'action=editAddress&amp;id=' . $address_id . '" title="' . __('Edit', 'wpshop') . '">' . __('Edit', 'wpshop') . '</a>';
        $result = json_encode(array(true, $retour, $edit_link, $is_allowed_destination, $checkout_payment_button));
    } else {
        $result = json_encode(array(false, 'missing_informations'));
    }
    echo $result;
    die;
}
    /**
     * Display the differents forms fields
     * @param string $type : Type of address
     * @param string $first : Customer first address ?
     * @param string $referer : Referer website page
     * @param string $admin : Display this form in admin panel
     */
    function display_form_fields($type, $id = '', $first = '', $referer = '', $special_values = array(), $options = array(), $display_for_admin = array(), $other_customer = '')
    {
        global $wpshop, $wpshop_form, $wpdb;
        $choosen_address = get_option('wpshop_billing_address');
        $shipping_address = get_option('wpshop_shipping_address_choice');
        $output_form_fields = $form_model = '';
        $user_id = !empty($other_customer) ? $other_customer : get_current_user_id();
        if (empty($type)) {
            $type = $choosen_address['choice'];
        }
        $result = wps_address::get_addresss_form_fields_by_type($type, $id);
        /** Check if it's shipping or billing **/
        if ($type == $choosen_address['choice']) {
            $form_model = !empty($choosen_address['display_model']) ? $choosen_address['display_model'] : null;
        } elseif ($type == $shipping_address['choice']) {
            $form_model = !empty($shipping_address['display_model']) ? $shipping_address['display_model'] : null;
        }
        $form = $result[$type];
        // Take the post id to make the link with the post meta of  address
        $values = array();
        // take the address informations
        $current_item_edited = !empty($id) ? (int) wpshop_tools::varSanitizer($id) : null;
        foreach ($form as $group_id => $group_fields) {
            if (empty($options) || !empty($options) && $options['title']) {
                $output_form_fields .= '<h2>' . __($group_fields['name'], 'wpshop') . '</h2>';
            }
            $end_line_indicator = 0;
            $fields_limit_per_line = -1;
            foreach ($group_fields['content'] as $key => $field) {
                $attribute_def = wpshop_attributes::getElement($field['name'], $element_status = "'valid'", $field_to_search = 'code');
                /** Grid opening **/
                if (!empty($form_model) && !empty($form_model[$group_id]) && in_array('wps-attribute-end-line-' . $end_line_indicator, $form_model[$group_id]) && $fields_limit_per_line == -1) {
                    $current_key = array_search('wps-attribute-end-line-' . $end_line_indicator, $form_model[$group_id]);
                    $current_attribute_key = array_search('attribute_' . $attribute_def->id, $form_model[$group_id]);
                    if ($current_attribute_key > $current_key) {
                        /** Define limit **/
                        if (in_array('wps-attribute-end-line-' . ($end_line_indicator + 1), $form_model[$group_id])) {
                            $next_key = array_search('wps-attribute-end-line-' . ($end_line_indicator + 1), $form_model[$group_id]);
                            $fields_limit_per_line = $next_key - $current_key - 1;
                            $fields_limit_per_line = $fields_limit_per_line > 6 ? 6 : $fields_limit_per_line;
                        } else {
                            $current_key = array_search('wps-attribute-end-line-' . $end_line_indicator, $form_model[$group_id]);
                            $end_tab = count($form_model[$group_id]) - 1;
                            $fields_limit_per_line = $end_tab - $current_key - 1;
                            $fields_limit_per_line = $fields_limit_per_line > 6 ? 6 : $fields_limit_per_line;
                        }
                        if (!empty($fields_limit_per_line) && $fields_limit_per_line != -1) {
                            if ($fields_limit_per_line == 1) {
                                $output_form_fields .= '<div class="wps-row">';
                            } else {
                                $output_form_fields .= '<div class="wps-row wps-gridwrapper' . $fields_limit_per_line . '-padded">';
                            }
                        }
                    }
                }
                if (empty($options['field_to_hide']) || !is_array($options['field_to_hide']) || !in_array($key, $options['field_to_hide'])) {
                    $attributeInputDomain = 'attribute[' . $type . '][' . $field['data_type'] . ']';
                    // Test if there is POST var or if user have already fill his address infos and fill the fields with these infos
                    if (!empty($_POST)) {
                        $referer = !empty($_POST['referer']) ? $_POST['referer'] : '';
                        if (!empty($form['id']) && !empty($field['name']) && isset($_POST[$form['id'] . "_" . $field['name']])) {
                            $value = $_POST[$form['id'] . "_" . $field['name']];
                        }
                    }
                    // Fill Automaticly some fields when it's an address creation
                    switch ($field['name']) {
                        case 'address_title':
                            if (empty($field['value'])) {
                                /** Count Billing and shipping address **/
                                $billing_address_count = $shipping_address_count = 1;
                                if (get_current_user_id() != 0) {
                                    $addresses = get_posts(array('posts_per_page' => -1, 'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_ADDRESS, 'post_parent' => get_current_user_id(), 'post_status' => 'draft'));
                                    if (!empty($addresses)) {
                                        foreach ($addresses as $address) {
                                            $address_type = get_post_meta($address->ID, '_wpshop_address_attribute_set_id', true);
                                            if (!empty($address_type)) {
                                                if (!empty($shipping_address_choice['choice']) && $address_type == $shipping_address_choice['choice']) {
                                                    $shipping_address_count++;
                                                } else {
                                                    $billing_address_count++;
                                                }
                                            }
                                        }
                                    }
                                }
                                $field['value'] = $type == $choosen_address['choice'] ? __('Billing address', 'wpshop') . ($billing_address_count > 1 ? ' ' . $billing_address_count : '') : __('Shipping address', 'wpshop') . ($shipping_address_count > 1 ? ' ' . $shipping_address_count : '');
                            }
                            break;
                        case 'address_last_name':
                            if (empty($field['value'])) {
                                $usermeta_last_name = get_user_meta($user_id, 'last_name', true);
                                $field['value'] = !empty($usermeta_last_name) ? $usermeta_last_name : '';
                            }
                            break;
                        case 'address_first_name':
                            if (empty($field['value'])) {
                                $usermeta_first_name = get_user_meta($user_id, 'first_name', true);
                                $field['value'] = !empty($usermeta_first_name) ? $usermeta_first_name : '';
                            }
                            break;
                        case 'address_user_email':
                            if (empty($field['value'])) {
                                $user_infos = get_userdata($user_id);
                                $field['value'] = !empty($user_infos) && !empty($user_infos->user_email) ? $user_infos->user_email : '';
                            }
                            break;
                        default:
                            $field['value'] = !empty($field['value']) ? $field['value'] : '';
                            break;
                    }
                    /** Fill fields if $_POST exist **/
                    if (!empty($_POST['attribute'][$type][$field['data_type']][$field['name']])) {
                        $field['value'] = $_POST['attribute'][$type][$field['data_type']][$field['name']];
                    }
                    if ($field['name'] == 'address_title' && !empty($first) && $type == __('Billing address', 'wpshop')) {
                        $value = __('Billing address', 'wpshop');
                    } elseif ($field['name'] == 'address_title' && !empty($first) && $type == __('Shipping address', 'wpshop')) {
                        $value = __('Shipping address', 'wpshop');
                    }
                    if (!empty($special_values[$field['name']])) {
                        $field['value'] = $special_values[$field['name']];
                    }
                    $template = 'wpshop_account_form_input';
                    if ($field['type'] == 'hidden') {
                        $template = 'wpshop_account_form_hidden_input';
                    }
                    if ($field['frontend_verification'] == 'country') {
                        $field['type'] = 'select';
                        /** display a country list **/
                        $countries_list = unserialize(WPSHOP_COUNTRY_LIST);
                        $possible_values = array_merge(array('' => __('Choose a country')), $countries_list);
                        $limit_countries_list = get_option('wpshop_limit_country_list');
                        $default_country_choice = get_option('wpshop_country_default_choice');
                        if (!empty($limit_countries_list)) {
                            $possible_values = array();
                            if (count($limit_countries_list) > 1) {
                                $possible_values[''] = __('Choose a country');
                            }
                            foreach ($limit_countries_list as $country_code) {
                                if (!empty($countries_list) && !empty($countries_list[$country_code])) {
                                    $possible_values[$country_code] = $countries_list[$country_code];
                                }
                            }
                        }
                        $field['value'] = !empty($default_country_choice) && array_key_exists($default_country_choice, $possible_values) ? $default_country_choice : '';
                        $field['possible_value'] = $possible_values;
                        $field['valueToPut'] = 'index';
                    }
                    $element_simple_class = str_replace('"', '', str_replace('class="', '', str_replace('wpshop_input_datetime', '', $field['option'])));
                    $input_tpl_component = array();
                    //$input_tpl_component['CUSTOMER_FORM_INPUT_MAIN_CONTAINER_CLASS'] = ' wsphop_customer_account_form_container wsphop_customer_account_form_container_' . $field['name'] . $element_simple_class;
                    $input_tpl_component['CUSTOMER_FORM_INPUT_LABEL'] = $field['type'] != 'hidden' ? stripslashes(__($field['label'], 'wpshop')) . ($field['required'] == 'yes' ? ' <em>*</em>' : '') : '';
                    $input_tpl_component['CUSTOMER_FORM_INPUT_LABEL_OPTIONS'] = ' for="' . $field['id'] . '"';
                    $input_tpl_component['CUSTOMER_FORM_INPUT_FIELD'] = wpshop_form::check_input_type($field, $attributeInputDomain);
                    //$output_form_fields .= wpshop_display::display_template_element($template, $input_tpl_component);
                    $output_form_fields .= wpshop_display::display_template_element('wps_address_field', $input_tpl_component, array(), 'wpshop');
                    unset($input_tpl_component);
                    if ($field['_need_verification'] == 'yes') {
                        $field['name'] = $field['name'] . '2';
                        $field['id'] = $field['id'] . '2';
                        $element_simple_class = str_replace('"', '', str_replace('class="', '', str_replace('wpshop_input_datetime', '', $field['option'])));
                        $input_tpl_component = array();
                        $input_tpl_component['CUSTOMER_FORM_INPUT_LABEL'] = __($field['label'], 'wpshop') . ($field['required'] == 'yes' && !is_admin() || $field['name'] == 'address_user_email' && is_admin() ? ' <span class="required">*</span>' : '');
                        $input_tpl_component['CUSTOMER_FORM_INPUT_LABEL_OPTIONS'] = ' for="' . $field['id'] . '"';
                        $input_tpl_component['CUSTOMER_FORM_INPUT_MAIN_CONTAINER_CLASS'] = ' wsphop_customer_account_form_container wsphop_customer_account_form_container_' . $field['name'] . $element_simple_class;
                        $input_tpl_component['CUSTOMER_FORM_INPUT_LABEL'] = sprintf(__('Confirm %s', 'wpshop'), strtolower(__($field['label'], 'wpshop'))) . ($field['required'] == 'yes' && !is_admin() ? ' <span class="required">*</span>' : '');
                        $input_tpl_component['CUSTOMER_FORM_INPUT_FIELD'] = wpshop_form::check_input_type($field, $attributeInputDomain) . $field['options'];
                        //$output_form_fields .= wpshop_display::display_template_element($template, $input_tpl_component);
                        $output_form_fields .= wpshop_display::display_template_element('wps_address_field', $input_tpl_component, array(), 'wpshop');
                        unset($input_tpl_component);
                    }
                }
                /** Grid closing **/
                if ($fields_limit_per_line != -1 && !empty($fields_limit_per_line)) {
                    $fields_limit_per_line--;
                    if ($fields_limit_per_line == 0) {
                        $output_form_fields .= '</div>';
                        $fields_limit_per_line = -1;
                        $end_line_indicator++;
                    }
                }
            }
        }
        if ($type == $choosen_address['choice']) {
            $output_form_fields .= '<input type="hidden" name="billing_address" value="' . $choosen_address['choice'] . '" />';
        }
        $shipping_address_options = get_option('wpshop_shipping_address_choice');
        if ($type == $shipping_address_options['choice']) {
            $output_form_fields .= '<input type="hidden" name="shipping_address" value="' . $shipping_address_options['choice'] . '" />';
        }
        $output_form_fields .= '<input type="hidden" name="edit_other_thing" value="' . false . '" /><input type="hidden" name="referer" value="' . $referer . '" />
								<input type="hidden" name="type_of_form" value="' . $type . '" /><input type="hidden" name="attribute[' . $type . '][item_id]" value="' . $current_item_edited . '" />';
        $output_form_fields .= $user_id != get_current_user_id() ? '<input type="hidden" name="user[customer_id]" value="' . $user_id . '" />' : '';
        if (empty($first)) {
            $output_form_fields = wpshop_display::display_template_element('wpshop_customer_addresses_form', array('CUSTOMER_ADDRESSES_FORM_CONTENT' => $output_form_fields, 'CUSTOMER_ADDRESSES_FORM_BUTTONS' => ''));
        }
        return $output_form_fields;
    }
Пример #3
0
 function wpshop_billing_address_validator($input)
 {
     global $wpdb;
     $t = wps_address::get_addresss_form_fields_by_type($input['choice']);
     $the_code = '';
     foreach ($t[$input['choice']] as $group_id => $group_def) {
         if (!empty($input['integrate_into_register_form_matching_field']) && !empty($input['integrate_into_register_form_matching_field']['user_email']) && array_key_exists($input['integrate_into_register_form_matching_field']['user_email'], $group_def['content'])) {
             $the_code = $group_def['content'][$input['integrate_into_register_form_matching_field']['user_email']]['name'];
             continue;
         }
     }
     $the_code;
     if (!empty($input['integrate_into_register_form']) && $input['integrate_into_register_form'] == 'yes') {
         if (!empty($input['integrate_into_register_form_matching_field']) && !empty($input['integrate_into_register_form_matching_field']['user_email']) && $the_code == 'address_user_email') {
             $wpdb->update(WPSHOP_DBT_ATTRIBUTE, array('_need_verification' => 'no'), array('code' => $the_code));
         }
     }
     $billing_option = get_option('wpshop_billing_address');
     if (!empty($billing_option) && !empty($billing_option['display_model'])) {
         $input['display_model'] = $billing_option['display_model'];
     }
     return $input;
 }
 /**
  * AJAX - Function for save address
  */
 function wps_save_address()
 {
     global $wpshop;
     $status = false;
     $result = $address_type = $same_address_type = '';
     foreach ($_POST['attribute'] as $id_group => $attribute_group) {
         $address_type = $id_group;
         $group = wps_address::get_addresss_form_fields_by_type($id_group);
         foreach ($group as $attribute_sets) {
             foreach ($attribute_sets as $attribute_set_field) {
                 $validate = $wpshop->validateForm($attribute_set_field['content'], $_POST['attribute'][$id_group], 'address_edition');
             }
             if ($validate) {
                 $shipping_save = self::save_address_infos($id_group);
                 //	echo '<pre>'; print_r($_POST); echo '</pre>';
                 if (!empty($_POST['wps-shipping-to-billing'])) {
                     $_POST['wps-shipping-to-billing-id'] = $shipping_save['current_id'];
                     $billing_option = get_option('wpshop_billing_address');
                     $shipping_option = get_option('wpshop_shipping_address_choice');
                     self::shipping_to_billing($shipping_option['choice'], $billing_option['choice']);
                     self::save_address_infos($billing_option['choice']);
                     $same_address_type = $billing_option['choice'];
                 }
                 $status = true;
             } 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>';
                 }
             }
         }
     }
     echo json_encode(array($status, $result, $address_type, $same_address_type));
     die;
 }
 /**
  * ACCOUNT - Save account informations
  */
 function wps_save_account_informations()
 {
     global $wpdb;
     global $wpshop;
     $status = false;
     $response = '';
     $exclude_user_meta = array('user_email', 'user_pass');
     $wps_entities = new wpshop_entities();
     $element_id = $wps_entities->get_entity_identifier_from_code(WPSHOP_NEWTYPE_IDENTIFIER_CUSTOMERS);
     $user_id = get_current_user_id();
     if (!empty($element_id) && !empty($user_id)) {
         $query = $wpdb->prepare('SELECT ID FROM ' . $wpdb->posts . ' WHERE post_type = %s AND post_author = %d', WPSHOP_NEWTYPE_IDENTIFIER_CUSTOMERS, $user_id);
         $cid = $wpdb->get_var($query);
         $query = $wpdb->prepare('SELECT id FROM ' . WPSHOP_DBT_ATTRIBUTE_SET . ' WHERE entity_id = %d', $element_id);
         $attribute_set_id = $wpdb->get_var($query);
         if (!empty($attribute_set_id)) {
             $group = wps_address::get_addresss_form_fields_by_type($attribute_set_id);
             foreach ($group as $attribute_sets) {
                 foreach ($attribute_sets as $attribute_set_field) {
                     $validate = $wpshop->validateForm($attribute_set_field['content'], $_POST['attribute']);
                     if (empty($wpshop->errors)) {
                         $user_name = !empty($_POST['attribute']['varchar']['user_login']) ? $_POST['attribute']['varchar']['user_login'] : $_POST['attribute']['varchar']['user_email'];
                         $user_pass = !empty($_POST['attribute']['varchar']['user_pass']) ? $_POST['attribute']['varchar']['user_pass'] : '';
                         $wpshop_attributes = new wpshop_attributes();
                         foreach ($attribute_set_field['content'] as $attribute) {
                             $attribute_def = wpshop_attributes::getElement($attribute['name'], "'valid'", 'code');
                             if (!in_array($attribute['name'], $exclude_user_meta)) {
                                 update_user_meta($user_id, $attribute['name'], wpshop_tools::varSanitizer($_POST['attribute'][$attribute['data_type']][$attribute['name']]));
                             } else {
                                 wp_update_user(array('ID' => $user_id, $attribute['name'] => wpshop_tools::varSanitizer($_POST['attribute'][$attribute['data_type']][$attribute['name']])));
                             }
                             //Save data in attribute tables, ckeck first if exist to know if Insert or Update
                             $query = $wpdb->prepare('SELECT * FROM ' . WPSHOP_DBT_ATTRIBUTE_VALUES_PREFIX . strtolower($attribute['data_type']) . ' WHERE entity_type_id = %d AND entity_id = %d AND attribute_id = %d', $element_id, $cid, $attribute_def->id);
                             $checking_attribute_exist = $wpdb->get_results($query);
                             if (!empty($checking_attribute_exist)) {
                                 $wpdb->update(WPSHOP_DBT_ATTRIBUTE_VALUES_PREFIX . strtolower($attribute['data_type']), array('value' => wpshop_tools::varSanitizer($_POST['attribute'][$attribute['data_type']][$attribute['name']])), array('entity_type_id' => $element_id, 'entity_id' => $cid, 'attribute_id' => $attribute_def->id));
                             } else {
                                 $wpdb->insert(WPSHOP_DBT_ATTRIBUTE_VALUES_PREFIX . strtolower($attribute['data_type']), array('entity_type_id' => $element_id, 'attribute_id' => $attribute_def->id, 'entity_id' => $cid, 'user_id' => $user_id, 'creation_date_value' => current_time('mysql', 0), 'language' => 'fr_FR', 'value' => wpshop_tools::varSanitizer($_POST['attribute'][$attribute['data_type']][$attribute['name']])));
                             }
                         }
                         /** Update newsletter user preferences **/
                         $newsletter_preferences = array();
                         if (!empty($_POST['newsletters_site'])) {
                             $newsletter_preferences['newsletters_site'] = 1;
                         }
                         if (!empty($_POST['newsletters_site_partners'])) {
                             $newsletter_preferences['newsletters_site_partners'] = 1;
                         }
                         update_user_meta($user_id, 'user_preferences', $newsletter_preferences);
                         $status = true;
                     } else {
                         $response = '<div class="wps-alert-error">' . __('Some errors have been detected', 'wpshop') . ' : <ul>';
                         foreach ($wpshop->errors as $error) {
                             $response .= '<li>' . $error . '</li>';
                         }
                         $response .= '</div>';
                     }
                 }
             }
         }
     }
     echo json_encode(array('status' => $status, 'response' => $response));
     wp_die();
 }