/**
  * When Options are saved, return the 'jigoshop_tax_rates' option values
  *
  * @return  mixed  false if not rax rates, array of tax rates otherwise
  * @since  1.3
  */
 function get_updated_tax_classes()
 {
     $tax_rates = array();
     $tax_fields = array('tax_classes' => '', 'tax_country' => '', 'tax_rate' => '', 'tax_label' => '', 'tax_shipping' => '', 'tax_compound' => '');
     /* Save each array key to a variable */
     foreach ($tax_fields as $name => $val) {
         if (isset($_POST[$name])) {
             $tax_fields[$name] = $_POST[$name];
         }
     }
     for ($i = 0; $i < sizeof($tax_fields['tax_classes']); $i++) {
         if (empty($tax_fields['tax_rate'][$i])) {
             continue;
         }
         $countries = $tax_fields['tax_country'][$i];
         $label = trim($tax_fields['tax_label'][$i]);
         $rate = number_format(floatval($tax_fields['tax_rate'][$i]), 4);
         $class = jigowatt_clean($tax_fields['tax_classes'][$i]);
         $shipping = !empty($tax_fields['tax_shipping'][$i]) ? 'yes' : 'no';
         $compound = !empty($tax_fields['tax_compound'][$i]) ? 'yes' : 'no';
         /* Save the state & country separately from options eg US:OH */
         $whole_countries_processed = array();
         foreach ($countries as $country_code) {
             @(list($country, $state) = explode(':', $country_code, 2));
             if (!in_array($country, $whole_countries_processed)) {
                 if ($state === null && jigoshop_countries::country_has_states($country)) {
                     $whole_countries_processed[] = $country;
                     foreach (jigoshop_countries::get_states($country) as $state => $state_name) {
                         $tax_rates[] = array('country' => $country, 'label' => $label, 'state' => $state, 'rate' => $rate, 'shipping' => $shipping, 'class' => $class, 'compound' => $compound, 'is_all_states' => true);
                     }
                 } else {
                     $tax_rates[] = array('country' => $country, 'label' => $label, 'state' => $state, 'rate' => $rate, 'shipping' => $shipping, 'class' => $class, 'compound' => $compound, 'is_all_states' => false);
                 }
             }
         }
     }
     usort($tax_rates, array($this, 'csort_tax_rates'));
     return $tax_rates;
 }
 /**
  * Validate the checkout
  */
 public function validate_checkout()
 {
     if (jigoshop_cart::is_empty()) {
         jigoshop::add_error(sprintf(__('Sorry, your session has expired. <a href="%s">Return to homepage &rarr;</a>', 'jigoshop'), home_url()));
     }
     // Process Discount Codes
     if (!empty($_POST['coupon_code'])) {
         $coupon = sanitize_title($_POST['coupon_code']);
         jigoshop_cart::add_discount($coupon);
     }
     foreach (jigoshop_cart::get_coupons() as $coupon) {
         jigoshop_cart::is_valid_coupon($coupon);
     }
     // Checkout fields
     $this->posted['shipping_method'] = '';
     $this->posted['shipping_service'] = '';
     if (isset($_POST['shipping_method'])) {
         $shipping_method = jigowatt_clean($_POST['shipping_method']);
         $shipping_data = explode(':', $shipping_method);
         $this->posted['shipping_method'] = $shipping_data[0];
         $this->posted['shipping_service'] = $shipping_data[1];
     }
     $this->posted['shiptobilling'] = isset($_POST['shiptobilling']) ? jigowatt_clean($_POST['shiptobilling']) : '';
     $this->posted['payment_method'] = isset($_POST['payment_method']) ? jigowatt_clean($_POST['payment_method']) : '';
     $this->posted['order_comments'] = isset($_POST['order_comments']) ? jigowatt_clean($_POST['order_comments']) : '';
     $this->posted['terms'] = isset($_POST['terms']) ? jigowatt_clean($_POST['terms']) : '';
     $this->posted['create_account'] = isset($_POST['create_account']) ? jigowatt_clean($_POST['create_account']) : '';
     $this->posted['account_username'] = isset($_POST['account_username']) ? jigowatt_clean($_POST['account_username']) : '';
     $this->posted['account_password'] = isset($_POST['account_password']) ? jigowatt_clean($_POST['account_password']) : '';
     $this->posted['account_password_2'] = isset($_POST['account_password_2']) ? jigowatt_clean($_POST['account_password_2']) : '';
     if (jigoshop_cart::get_total(false) == 0) {
         $this->posted['payment_method'] = 'no_payment';
     }
     // establish customer billing and shipping locations
     if (jigoshop_cart::ship_to_billing_address_only()) {
         $this->posted['shiptobilling'] = 'true';
     }
     $country = isset($_POST['billing_country']) ? jigowatt_clean($_POST['billing_country']) : '';
     $state = isset($_POST['billing_state']) ? jigowatt_clean($_POST['billing_state']) : '';
     $allowed_countries = Jigoshop_Base::get_options()->get('jigoshop_allowed_countries');
     if ($allowed_countries === 'specific') {
         $specific_countries = Jigoshop_Base::get_options()->get('jigoshop_specific_allowed_countries');
         if (!in_array($country, $specific_countries)) {
             jigoshop::add_error(__('Invalid billing country.', 'jigoshop'));
             return;
         }
     }
     if (jigoshop_countries::country_has_states($country)) {
         $states = jigoshop_countries::get_states($country);
         if (!in_array($state, array_keys($states))) {
             jigoshop::add_error(__('Invalid billing state.', 'jigoshop'));
             return;
         }
     }
     $postcode = isset($_POST['billing_postcode']) ? jigowatt_clean($_POST['billing_postcode']) : '';
     $ship_to_billing = Jigoshop_Base::get_options()->get('jigoshop_ship_to_billing_address_only') == 'yes';
     jigoshop_customer::set_location($country, $state, $postcode);
     if (Jigoshop_Base::get_options()->get('jigoshop_calc_shipping') == 'yes') {
         if ($ship_to_billing || !empty($_POST['shiptobilling'])) {
             jigoshop_customer::set_shipping_location($country, $state, $postcode);
         } else {
             $country = isset($_POST['shipping_country']) ? jigowatt_clean($_POST['shipping_country']) : '';
             $state = isset($_POST['shipping_state']) ? jigowatt_clean($_POST['shipping_state']) : '';
             $postcode = isset($_POST['shipping_postcode']) ? jigowatt_clean($_POST['shipping_postcode']) : '';
             if ($allowed_countries === 'specific') {
                 $specific_countries = Jigoshop_Base::get_options()->get('jigoshop_specific_allowed_countries');
                 if (!in_array($country, $specific_countries)) {
                     jigoshop::add_error(__('Invalid shipping country.', 'jigoshop'));
                     return;
                 }
             }
             if (jigoshop_countries::country_has_states($country)) {
                 $states = jigoshop_countries::get_states($country);
                 if (!in_array($state, array_keys($states))) {
                     jigoshop::add_error(__('Invalid shipping state.', 'jigoshop'));
                     return;
                 }
             }
             jigoshop_customer::set_shipping_location($country, $state, $postcode);
         }
     }
     // Billing Information
     foreach ($this->billing_fields as $field) {
         $field = apply_filters('jigoshop_billing_field', $field);
         $this->posted[$field['name']] = isset($_POST[$field['name']]) ? jigowatt_clean($_POST[$field['name']]) : '';
         // Format
         if (isset($field['format'])) {
             switch ($field['format']) {
                 case 'postcode':
                     $this->posted[$field['name']] = strtolower(str_replace(' ', '', $this->posted[$field['name']]));
                     break;
             }
         }
         // Required
         if ($field['name'] == 'billing_state' && jigoshop_customer::has_valid_shipping_state()) {
             $field['required'] = false;
         }
         if (isset($field['required']) && $field['required'] && empty($this->posted[$field['name']])) {
             jigoshop::add_error($field['label'] . __(' (billing) is a required field.', 'jigoshop'));
         }
         if ($field['name'] == 'billing_euvatno') {
             $vatno = isset($this->posted['billing_euvatno']) ? $this->posted['billing_euvatno'] : '';
             $vatno = str_replace(' ', '', $vatno);
             $country = jigoshop_tax::get_customer_country();
             // strip any country code from the beginning of the number
             if (strpos($vatno, $country) === 0) {
                 $vatno = substr($vatno, strlen($country));
             }
             if ($vatno != '') {
                 $url = 'http://isvat.appspot.com/' . $country . '/' . $vatno . '/';
                 $httpRequest = curl_init();
                 curl_setopt($httpRequest, CURLOPT_FAILONERROR, true);
                 curl_setopt($httpRequest, CURLOPT_RETURNTRANSFER, true);
                 curl_setopt($httpRequest, CURLOPT_HEADER, false);
                 curl_setopt($httpRequest, CURLOPT_URL, $url);
                 $result = curl_exec($httpRequest);
                 curl_close($httpRequest);
                 if ($result === 'false') {
                     jigoshop_log('EU VAT validation error with URL: ' . $url);
                     jigoshop::add_error($field['label'] . __(' (billing) is not a valid VAT Number.  Leave it blank to disable VAT validation. (VAT may be charged depending on your location)', 'jigoshop'));
                 } else {
                     $this->valid_euvatno = jigoshop_countries::get_base_country() != jigoshop_tax::get_customer_country() && jigoshop_countries::is_eu_country(jigoshop_tax::get_customer_country());
                 }
             }
         }
         // Validation
         if (isset($field['validate']) && !empty($this->posted[$field['name']])) {
             switch ($field['validate']) {
                 case 'phone':
                     if (!jigoshop_validation::is_phone($this->posted[$field['name']])) {
                         jigoshop::add_error($field['label'] . __(' (billing) is not a valid number.', 'jigoshop'));
                     }
                     break;
                 case 'email':
                     if (!jigoshop_validation::is_email($this->posted[$field['name']])) {
                         jigoshop::add_error($field['label'] . __(' (billing) is not a valid email address.', 'jigoshop'));
                     }
                     break;
                 case 'postcode':
                     if (!jigoshop_validation::is_postcode($this->posted[$field['name']], $_POST['billing_country'])) {
                         jigoshop::add_error($field['label'] . __(' (billing) is not a valid postcode/ZIP.', 'jigoshop'));
                     } else {
                         $this->posted[$field['name']] = jigoshop_validation::format_postcode($this->posted[$field['name']], $_POST['billing_country']);
                     }
                     break;
             }
         }
     }
     // Shipping Information
     if (jigoshop_shipping::is_enabled() && !jigoshop_cart::ship_to_billing_address_only() && empty($this->posted['shiptobilling'])) {
         foreach ($this->shipping_fields as $field) {
             $field = apply_filters('jigoshop_shipping_field', $field);
             if (isset($_POST[$field['name']])) {
                 $this->posted[$field['name']] = jigowatt_clean($_POST[$field['name']]);
             } else {
                 $this->posted[$field['name']] = '';
             }
             // Format
             if (isset($field['format'])) {
                 switch ($field['format']) {
                     case 'postcode':
                         $this->posted[$field['name']] = strtolower(str_replace(' ', '', $this->posted[$field['name']]));
                         break;
                 }
             }
             // Required
             if ($field['name'] == 'shipping_state' && jigoshop_customer::has_valid_shipping_state()) {
                 $field['required'] = false;
             }
             if (isset($field['required']) && $field['required'] && empty($this->posted[$field['name']])) {
                 jigoshop::add_error($field['label'] . __(' (shipping) is a required field.', 'jigoshop'));
             }
             // Validation
             if (isset($field['validate']) && !empty($this->posted[$field['name']])) {
                 switch ($field['validate']) {
                     case 'postcode':
                         if (!jigoshop_validation::is_postcode($this->posted[$field['name']], $country)) {
                             jigoshop::add_error($field['label'] . __(' (shipping) is not a valid postcode/ZIP.', 'jigoshop'));
                         } else {
                             $this->posted[$field['name']] = jigoshop_validation::format_postcode($this->posted[$field['name']], $country);
                         }
                         break;
                 }
             }
         }
     }
     if ($this->must_register && empty($this->posted['create_account'])) {
         jigoshop::add_error(__('Sorry, you must agree to creating an account', 'jigoshop'));
     }
     if ($this->must_register || empty($user_id) && $this->posted['create_account']) {
         if (!$this->show_signup) {
             jigoshop::add_error(__('Sorry, the shop owner has disabled guest purchases.', 'jigoshop'));
         }
         if (empty($this->posted['account_username'])) {
             jigoshop::add_error(__('Please enter an account username.', 'jigoshop'));
         }
         if (empty($this->posted['account_password'])) {
             jigoshop::add_error(__('Please enter an account password.', 'jigoshop'));
         }
         if ($this->posted['account_password_2'] !== $this->posted['account_password']) {
             jigoshop::add_error(__('Passwords do not match.', 'jigoshop'));
         }
         // Check the username
         if (!validate_username($this->posted['account_username'])) {
             jigoshop::add_error(__('Invalid email/username.', 'jigoshop'));
         } elseif (username_exists($this->posted['account_username'])) {
             jigoshop::add_error(__('An account is already registered with that username. Please choose another.', 'jigoshop'));
         }
         // Check the e-mail address
         if (email_exists($this->posted['billing_email'])) {
             jigoshop::add_error(__('An account is already registered with your email address. Please login.', 'jigoshop'));
         }
     }
     // Terms
     if (!isset($_POST['update_totals']) && empty($this->posted['terms']) && jigoshop_get_page_id('terms') > 0) {
         jigoshop::add_error(__('You must accept our Terms &amp; Conditions.', 'jigoshop'));
     }
     if (jigoshop_cart::needs_shipping()) {
         // Shipping Method
         $available_methods = jigoshop_shipping::get_available_shipping_methods();
         if (!isset($available_methods[$this->posted['shipping_method']])) {
             jigoshop::add_error(__('Invalid shipping method.', 'jigoshop'));
         }
     }
 }
    /**
     * Outputs a form field
     *
     * @param array $args contains a list of args for showing the field, merged with defaults (below)
     * @return string
     */
    public static function address_form_field($args)
    {
        $defaults = array('type' => 'text', 'name' => '', 'label' => '', 'placeholder' => '', 'required' => false, 'class' => array(), 'label_class' => array(), 'rel' => '', 'return' => false, 'options' => array(), 'value' => '');
        $args = wp_parse_args($args, $defaults);
        if ($args['required']) {
            $required = ' <span class="required">*</span>';
            $input_required = ' input-required';
        } else {
            $required = '';
            $input_required = '';
        }
        if (in_array('form-row-last', $args['class'])) {
            $after = '<div class="clear"></div>';
        } else {
            $after = '';
        }
        switch ($args['type']) {
            case "country":
                $current_c = self::get_value($args['name']);
                $is_shipping_c = strpos($args['name'], 'shipping');
                if (!$current_c) {
                    if ($is_shipping_c === false) {
                        $current_c = jigoshop_customer::get_country();
                    } else {
                        $current_c = jigoshop_customer::get_shipping_country();
                    }
                }
                // Remove 'Select a Country' option from drop-down menu for countries.
                // There is no need to have it, because was assume when user hasn't selected
                // a country that they are from the shop base country.
                $field = '<p class="form-row ' . implode(' ', $args['class']) . '">
        <label for="' . esc_attr($args['name']) . '" class="' . esc_attr(implode(' ', $args['label_class'])) . '">' . $args['label'] . $required . '</label>
        <select name="' . esc_attr($args['name']) . '" id="' . esc_attr($args['name']) . '" class="country_to_state" rel="' . esc_attr($args['rel']) . '">';
                foreach (jigoshop_countries::get_allowed_countries() as $key => $value) {
                    $field .= '<option value="' . esc_attr($key) . '"';
                    if (self::get_value($args['name']) == $key) {
                        $field .= ' selected="selected"';
                    } elseif (self::get_value($args['name']) && $current_c == $key) {
                        $field .= ' selected="selected"';
                    }
                    $field .= '>' . __($value, 'jigoshop') . '</option>';
                }
                $field .= '</select></p>' . $after;
                break;
            case "state":
                $field = '<p class="form-row ' . implode(' ', $args['class']) . '">
					<label for="' . esc_attr($args['name']) . '" class="' . implode(' ', $args['label_class']) . '">' . $args['label'] . $required . '</label>';
                $is_shipping_s = strpos($args['name'], 'shipping');
                $current_cc = self::get_value($args['rel']);
                if (!$current_cc) {
                    if ($is_shipping_s === false) {
                        $current_cc = jigoshop_customer::get_country();
                    } else {
                        $current_cc = jigoshop_customer::get_shipping_country();
                    }
                }
                $current_r = self::get_value($args['name']);
                if (!$current_r) {
                    if ($is_shipping_s === false) {
                        $current_r = jigoshop_customer::get_state();
                    } else {
                        $current_r = jigoshop_customer::get_shipping_state();
                    }
                }
                $states = jigoshop_countries::get_states($current_cc);
                if (!empty($states)) {
                    // Dropdown
                    $field .= '<select name="' . esc_attr($args['name']) . '" id="' . esc_attr($args['name']) . '" class="' . esc_attr($input_required) . '"><option value="">' . __('Select a state&hellip;', 'jigoshop') . '</option>';
                    foreach ($states as $key => $value) {
                        $field .= '<option value="' . esc_attr($key) . '"';
                        if ($current_r == $key) {
                            $field .= ' selected="selected"';
                        }
                        $field .= '>' . __($value, 'jigoshop') . '</option>';
                    }
                    $field .= '</select>';
                } else {
                    // Input
                    $field .= '<input type="text" class="input-text" value="' . esc_attr($current_r) . '" placeholder="' . __('State/Province', 'jigoshop') . '" name="' . esc_attr($args['name']) . '" id="' . esc_attr($args['name']) . '" />';
                }
                $field .= '</p>' . $after;
                break;
            case "postcode":
                $current_pc = self::get_value($args['name']);
                $is_shipping_pc = strpos($args['name'], 'shipping');
                if (!$current_pc) {
                    if ($is_shipping_pc === false) {
                        $current_pc = jigoshop_customer::get_postcode();
                    } else {
                        $current_pc = jigoshop_customer::get_shipping_postcode();
                    }
                }
                $field = '<p class="form-row ' . implode(' ', $args['class']) . '">
					<label for="' . esc_attr($args['name']) . '" class="' . implode(' ', $args['label_class']) . '">' . $args['label'] . $required . '</label>
					<input type="text" class="input-text" name="' . esc_attr($args['name']) . '" id="' . esc_attr($args['name']) . '" placeholder="' . $args['placeholder'] . '" value="' . esc_attr($current_pc) . '" />
				</p>' . $after;
                break;
            case "textarea":
                $field = '<p class="form-row ' . implode(' ', $args['class']) . '">
					<label for="' . esc_attr($args['name']) . '" class="' . implode(' ', $args['label_class']) . '">' . $args['label'] . $required . '</label>
					<textarea name="' . esc_attr($args['name']) . '" class="input-text' . esc_attr($input_required) . '" id="' . esc_attr($args['name']) . '" placeholder="' . $args['placeholder'] . '" cols="5" rows="2">' . esc_textarea(self::get_value($args['name'])) . '</textarea>
				</p>' . $after;
                break;
                //Adds a drop down custom type
            //Adds a drop down custom type
            case "select":
                $field = '<p class="form-row ' . implode(' ', $args['class']) . '">
						  <label for="' . esc_attr($args['name']) . '" class="' . implode(' ', $args['label_class']) . '">' . $args['label'] . $required . '</label>';
                $field .= '<select name="' . esc_attr($args['name']) . '" id="' . esc_attr($args['name']) . '" class="' . esc_attr($input_required) . '">';
                foreach ($args['options'] as $value => $label) {
                    $field .= '<option value="' . esc_attr($value) . '"';
                    if (self::get_value($args['name']) == $value) {
                        $field .= ' selected="selected"';
                    }
                    $field .= '>' . __($label, 'jigoshop') . '</option>';
                }
                $field .= '</select></p>' . $after;
                break;
            default:
                $field = '<p class="form-row ' . implode(' ', $args['class']) . '">
					<label for="' . esc_attr($args['name']) . '" class="' . implode(' ', $args['label_class']) . '">' . $args['label'] . $required . '</label>
					<input type="' . $args['type'] . '" class="input-text' . esc_attr($input_required) . '" name="' . esc_attr($args['name']) . '" id="' . esc_attr($args['name']) . '" placeholder="' . $args['placeholder'] . '" value="' . self::get_value($args['name']) . '" />
				</p>' . $after;
                break;
        }
        $field = apply_filters('jigoshop_address_field_types', $field, $args);
        if ($args['return']) {
            return $field;
        } else {
            echo $field;
            return null;
        }
    }
Esempio n. 4
0
 /**
  * Gets the tax classes for the customer based on customer shipping
  * country and state.
  *
  * @return array array of tax classes
  */
 public function get_tax_classes_for_customer()
 {
     // if local pickup, we need to use the base tax classes
     if (jigoshop_session::instance()->chosen_shipping_method_id == 'local_pickup') {
         return $this->get_tax_classes_for_base();
     }
     $allowed_countries = Jigoshop_Base::get_options()->get('jigoshop_allowed_countries');
     $country = self::get_customer_country();
     $state = self::get_customer_state();
     if ($allowed_countries === 'specific') {
         $specific_countries = Jigoshop_Base::get_options()->get('jigoshop_specific_allowed_countries');
         $base_cc = jigoshop_countries::get_base_country();
         if (is_array($specific_countries) && !in_array($country, $specific_countries)) {
             if (in_array($base_cc, $specific_countries)) {
                 $country = $base_cc;
             } else {
                 $country = array_shift($specific_countries);
             }
         }
         if (jigoshop_countries::country_has_states($country) && !in_array($state, array_keys(jigoshop_countries::get_states($country)))) {
             if (isset($this->rates[$country])) {
                 $states = array_keys($this->rates[$country]);
                 $state = array_shift($states);
             } else {
                 $state = jigoshop_countries::get_base_state();
             }
         }
     }
     $state = $state && jigoshop_countries::country_has_states($country) ? $state : '*';
     $tax_classes = isset($this->rates[$country]) && isset($this->rates[$country][$state]) ? $this->rates[$country][$state] : false;
     return $tax_classes && is_array($tax_classes) ? array_keys($tax_classes) : array();
 }