/**
  * Is customer shipping outside base, but within the same country? This is
  * used to determine how to apply taxes. Also, it no country is set, assume
  * shipping is going to base country.
  */
 public static function is_customer_outside_base($shippable)
 {
     $outside = false;
     $country = $shippable ? self::get_shipping_country() : self::get_country();
     // if no country is set, then assume customer is from the shop base
     if ($country) {
         $shop_country = jigoshop_countries::get_base_country();
         // check if it's a country with states.
         if (jigoshop_countries::country_has_states($country)) {
             $shop_state = jigoshop_countries::get_base_state();
             // taxes only apply if the customer is shipping in the same country. If the customer is
             // shipping outside of the shop country, then taxes do not apply.
             if ($shop_country === $country && $shop_state !== ($shippable ? self::get_shipping_state() : self::get_state())) {
                 $outside = true;
             }
         } else {
             if (jigoshop_countries::is_eu_country($shop_country) && $shop_country != $country) {
                 // if both base country and shipping country are in the EU, then outside country base is true
                 $outside = jigoshop_countries::is_eu_country($country);
             }
         }
     }
     return $outside;
 }
Esempio n. 2
0
	/**
	 * Get the tax rate based on the country and state
	 *
	 * @param   object	Tax Class
	 * @return  mixed		
	 */
	function get_shipping_tax_rate( $tax_class = '' ) {
		
		if (defined('JIGOSHOP_CHECKOUT') && JIGOSHOP_CHECKOUT) :
			$country 	= jigoshop_customer::get_country();
			$state 		= jigoshop_customer::get_state();
		else :
			$country 	= jigoshop_countries::get_base_country();
			$state 		= jigoshop_countries::get_base_state();
		endif;
		
		// If we are here then shipping is taxable - work it out
		
		if ($tax_class) :
			
			// This will be per item shipping
			$rate = $this->find_rate( $country, $state, $tax_class );
			
			if (isset($rate['shipping']) && $rate['shipping']=='yes') :
				return $rate['rate'];
			else :
				// Get standard rate
				$rate = $this->find_rate( $country, $state );
				if (isset($rate['shipping']) && $rate['shipping']=='yes') return $rate['rate'];
			endif;
			
		else :
			
			// This will be per order shipping - loop through the order and find the highest tax class rate
			
			$found_rates = array();
			$found_shipping_rates = array();
			
			// Loop cart and find the highest tax band
			if (sizeof(jigoshop_cart::$cart_contents)>0) : foreach (jigoshop_cart::$cart_contents as $item) :
				
				if ($item['data']->data['tax_class']) :
					
					$found_rate = $this->find_rate( $country, $state, $item['data']->data['tax_class'] );
					
					$found_rates[] = $found_rate['rate'];
					
					if (isset($found_rate['shipping']) && $found_rate['shipping']=='yes') $found_shipping_rates[] = $found_rate['rate'];
					
				endif;
				
			endforeach; endif;
			
			if (sizeof($found_rates) > 0 && sizeof($found_shipping_rates) > 0) :
				
				rsort($found_rates);
				rsort($found_shipping_rates);
				
				if ($found_rates[0] == $found_shipping_rates[0]) :
					return $found_shipping_rates[0];
				else :
					// Use standard rate
					$rate = $this->find_rate( $country, $state );
					if (isset($rate['shipping']) && $rate['shipping']=='yes') return $rate['rate'];
				endif;
				
			else :
				// Use standard rate
				$rate = $this->find_rate( $country, $state );	
				if (isset($rate['shipping']) && $rate['shipping']=='yes') return $rate['rate'];
			endif;
			
		endif;

		return 0; // return false
	}
    /**
     * 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;
    }
Esempio n. 4
0
 /**
  * Get the shop's taxation rate using find_rate()
  *
  * @param string $tax_class is the tax class (not object)
  * @param bool $rate_only
  * @return int
  */
 public function get_shop_base_rate($tax_class = '*', $rate_only = true)
 {
     $country = jigoshop_countries::get_base_country();
     $state = jigoshop_countries::get_base_state();
     $state = $state && jigoshop_countries::country_has_states($country) ? $state : '*';
     $rate = $this->find_rate($country, $state, $tax_class);
     return $rate_only ? $rate['rate'] : $rate;
 }