示例#1
0
    function jigoshop_shipping_calculator()
    {
        if (jigoshop_cart::needs_shipping()) {
            ?>
		<form class="shipping_calculator" action="<?php 
            echo jigoshop_cart::get_cart_url();
            ?>
" method="post">
			<h4><a href="#" class="shipping-calculator-button"><?php 
            _e('Calculate Shipping', 'jigoshop');
            ?>
 <span>&darr;</span></a></h4>
			<section class="shipping-calculator-form">
			<p class="form-row">
				<select name="calc_shipping_country" id="calc_shipping_country" class="country_to_state" rel="calc_shipping_state">
					<option value=""><?php 
            _e('Select a country&hellip;', 'jigoshop');
            ?>
</option>
					<?php 
            foreach (jigoshop_countries::get_allowed_countries() as $key => $value) {
                echo '<option value="' . $key . '"';
                if (jigoshop_customer::get_shipping_country() == $key) {
                    echo 'selected="selected"';
                }
                echo '>' . $value . '</option>';
            }
            ?>
				</select>
			</p>
			<div class="col2-set">
				<p class="form-row col-1">
					<?php 
            $current_cc = jigoshop_customer::get_shipping_country();
            $current_r = jigoshop_customer::get_shipping_state();
            $states = jigoshop_countries::$states;
            if (isset($states[$current_cc][$current_r])) {
                // Dropdown
                ?>
							<span>
								<select name="calc_shipping_state" id="calc_shipping_state"><option value=""><?php 
                _e('Select a state&hellip;', 'jigoshop');
                ?>
</option><?php 
                foreach ($states[$current_cc] as $key => $value) {
                    echo '<option value="' . $key . '"';
                    if ($current_r == $key) {
                        echo 'selected="selected"';
                    }
                    echo '>' . $value . '</option>';
                }
                ?>
</select>
							</span>
							<?php 
            } else {
                // Input
                ?>
							<input type="text" class="input-text" value="<?php 
                echo $current_r;
                ?>
" placeholder="<?php 
                _e('state', 'jigoshop');
                ?>
" name="calc_shipping_state" id="calc_shipping_state" />
							<?php 
            }
            ?>
				</p>
				<p class="form-row col-2">
					<input type="text" class="input-text" value="<?php 
            echo jigoshop_customer::get_shipping_postcode();
            ?>
" placeholder="<?php 
            _e('Postcode/Zip', 'jigoshop');
            ?>
" title="<?php 
            _e('Postcode', 'jigoshop');
            ?>
" name="calc_shipping_postcode" id="calc_shipping_postcode" />
				</p>
			</div>
			<p><button type="submit" name="calc_shipping" value="1" class="button"><?php 
            _e('Update Totals', 'jigoshop');
            ?>
</button></p>
			<?php 
            jigoshop::nonce_field('cart');
            ?>
			</section>
		</form>
		<?php 
        }
    }
    /**
     * Outputs a form field
     *
     * @param array $args contains a list of args for showing the field, merged with defaults (below)
     * @return string
     */
    public function field($args)
    {
        $defaults = array('type' => 'text', 'name' => '', 'label' => '', 'placeholder' => '', 'required' => false, 'class' => array(), 'label_class' => array(), 'options' => array(), 'selected' => '', 'rel' => '', 'echo' => true, 'return' => false);
        $args = wp_parse_args($args, $defaults);
        if ($args['return']) {
            $args['echo'] = false;
        }
        $required = '';
        $input_required = '';
        $after = '';
        if ($args['name'] == 'billing_state' || $args['name'] == 'shipping_state') {
            if (jigoshop_customer::has_valid_shipping_state()) {
                $args['required'] = false;
            }
        }
        if ($args['required']) {
            $required = ' <span class="required">*</span>';
            $input_required = ' input-required';
        }
        if (in_array('form-row-last', $args['class'])) {
            $after = '<div class="clear"></div>';
        }
        switch ($args['type']) {
            case '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' . esc_attr($input_required) . '" rel="' . esc_attr($args['rel']) . '">';
                $countries = jigoshop_countries::get_allowed_countries();
                if (Jigoshop_Base::get_options()->get('jigoshop_default_country_for_customer') == -1) {
                    $countries = array_merge(array(-1 => __('Select your country', 'jigoshop')), $countries);
                }
                foreach ($countries as $key => $value) {
                    $field .= '<option value="' . esc_attr($key) . '"';
                    if ($this->get_value($args['name']) == $key) {
                        $field .= ' selected="selected"';
                    } elseif (!$this->get_value($args['name']) && jigoshop_customer::get_country() == $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>';
                $allowed_countries = Jigoshop_Base::get_options()->get('jigoshop_allowed_countries');
                $current_cc = $this->get_value($args['rel']);
                if (!$current_cc) {
                    $current_cc = jigoshop_customer::get_country();
                }
                if ($allowed_countries === 'specific') {
                    $specific_countries = Jigoshop_Base::get_options()->get('jigoshop_specific_allowed_countries');
                    $base_cc = jigoshop_countries::get_base_country();
                    if (!in_array($current_cc, $specific_countries)) {
                        if (in_array($base_cc, $specific_countries)) {
                            $current_cc = $base_cc;
                        } else {
                            $current_cc = array_shift($specific_countries);
                        }
                    }
                }
                $current_r = $this->get_value($args['name']);
                if (!$current_r) {
                    $current_r = jigoshop_customer::get_state();
                }
                $states = jigoshop_countries::get_states($current_cc);
                $state_keys = array_keys($states);
                if (jigoshop_countries::country_has_states($current_cc) && !in_array($current_r, $state_keys)) {
                    $base_r = jigoshop_countries::get_base_state();
                    if (in_array($base_r, $state_keys)) {
                        $current_r = $base_r;
                    } else {
                        $current_r = array_shift($state_keys);
                    }
                }
                if (jigoshop_countries::country_has_states($current_cc)) {
                    // 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' . esc_attr($input_required) . '" 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 = $this->get_value($args['name']);
                if (!$current_pc) {
                    $current_pc = $args['rel'] == 'shipping_country' ? jigoshop_customer::get_shipping_postcode() : jigoshop_customer::get_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' . esc_attr($input_required) . '" name="' . esc_attr($args['name']) . '" id="' . esc_attr($args['name']) . '" placeholder="' . $args['placeholder'] . '" value="' . esc_attr($current_pc) . '" rel="' . esc_attr($args['rel']) . '" />
				</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" rel="' . esc_attr($args['rel']) . '">' . esc_textarea($this->get_value($args['name'])) . '</textarea>
				</p>' . $after;
                break;
            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>
					<select name="' . esc_attr($args['name']) . '" id="' . esc_attr($args['name']) . '" class="input-text' . esc_attr($input_required) . '" rel="' . esc_attr($args['rel']) . '">';
                foreach ($args['options'] as $key => $value) {
                    $field .= '<option value="' . esc_attr($key) . '"';
                    if (esc_attr($args['selected']) == $key) {
                        $field .= ' selected="selected"';
                    }
                    $field .= '>' . __($value, 'jigoshop') . '</option>';
                }
                $field .= '</select></p>' . $after;
                break;
            case 'text':
            case 'password':
                $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="' . $this->get_value($args['name']) . '" rel="' . esc_attr($args['rel']) . '" />
				</p>' . $after;
                break;
            default:
                ob_start();
                do_action('jigoshop_display_checkout_field', $args['type'], $args, $this->get_value($args['name']));
                echo $after;
                $field = ob_get_clean();
                break;
        }
        if ($args['echo']) {
            echo $field;
        }
        return $field;
    }
    /**
     * 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;
        }
    }
    function jigoshop_shipping_calculator()
    {
        if (jigoshop_shipping::show_shipping_calculator()) {
            ?>
			<form class="shipping_calculator" action="<?php 
            echo esc_url(jigoshop_cart::get_cart_url());
            ?>
" method="post">
				<h2><a href="#" class="shipping-calculator-button"><?php 
            _e('Calculate Shipping', 'jigoshop');
            ?>
<span>&darr;</span></a></h2>
				<section class="shipping-calculator-form">
					<p class="form-row">
						<select name="calc_shipping_country" id="calc_shipping_country" class="country_to_state" rel="calc_shipping_state">
							<?php 
            foreach (jigoshop_countries::get_allowed_countries() as $key => $value) {
                ?>
								<option value="<?php 
                echo esc_attr($key);
                ?>
" <?php 
                selected(jigoshop_customer::get_shipping_country(), $key);
                ?>
><?php 
                echo $value;
                ?>
</option>
							<?php 
            }
            ?>
						</select>
					</p>
					<div class="col2-set">
						<p class="form-row col-1">
							<?php 
            $current_cc = jigoshop_customer::get_shipping_country();
            $current_r = jigoshop_customer::get_shipping_state();
            $states = jigoshop_countries::$states;
            if (jigoshop_countries::country_has_states($current_cc)) {
                // Dropdown
                ?>
								<span>
								<select name="calc_shipping_state" id="calc_shipping_state">
									<option value=""><?php 
                _e('Select a state&hellip;', 'jigoshop');
                ?>
</option><?php 
                foreach ($states[$current_cc] as $key => $value) {
                    echo '<option value="' . esc_attr($key) . '"';
                    if ($current_r == $key) {
                        echo 'selected="selected"';
                    }
                    echo '>' . $value . '</option>';
                }
                ?>
</select>
							</span>
							<?php 
            } else {
                // Input
                ?>
								<input type="text" class="input-text" value="<?php 
                echo esc_attr($current_r);
                ?>
" placeholder="<?php 
                _e('state', 'jigoshop');
                ?>
" name="calc_shipping_state" id="calc_shipping_state" />
							<?php 
            }
            ?>
						</p>
						<p class="form-row col-2">
							<input type="text" class="input-text" value="<?php 
            echo esc_attr(jigoshop_customer::get_shipping_postcode());
            ?>
" placeholder="<?php 
            _e('Postcode/Zip', 'jigoshop');
            ?>
" title="<?php 
            _e('Postcode', 'jigoshop');
            ?>
" name="calc_shipping_postcode" id="calc_shipping_postcode" />
						</p>
						<?php 
            do_action('jigoshop_after_shipping_calculator_fields');
            ?>
					</div>
					<p>
						<button type="submit" name="calc_shipping" value="1" class="button"><?php 
            _e('Update Totals', 'jigoshop');
            ?>
</button>
					</p>
					<p>
						<?php 
            $available_methods = jigoshop_shipping::get_available_shipping_methods();
            foreach ($available_methods as $method) {
                for ($i = 0; $i < $method->get_rates_amount(); $i++) {
                    ?>
					<div class="col2-set">
						<p class="form-row col-1">
							<?php 
                    echo '<input type="radio" name="shipping_rates" value="' . esc_attr($method->id . ':' . $i) . '"' . ' class="shipping_select"';
                    if ($method->get_cheapest_service() == $method->get_selected_service($i) && $method->is_chosen()) {
                        echo ' checked>';
                    } else {
                        echo '>';
                    }
                    echo $method->get_selected_service($i);
                    ?>
						<p class="form-row col-2"><?php 
                    if ($method->get_selected_price($i) > 0) {
                        echo jigoshop_price($method->get_selected_price($i));
                        echo __(' (ex. tax)', 'jigoshop');
                    } else {
                        echo __('Free', 'jigoshop');
                    }
                    ?>
					</div>
					<?php 
                }
            }
            ?>
					<input type="hidden" name="cart-url" value="<?php 
            echo esc_attr(jigoshop_cart::get_cart_url());
            ?>
">
					<?php 
            jigoshop::nonce_field('cart');
            ?>
				</section>
			</form>
		<?php 
        }
    }