public function get_origin_address() { $wc_address_fields = array(); $wc_address_fields['company'] = get_bloginfo('name'); $wc_address_fields['name'] = wp_get_current_user()->display_name; $base_location = wc_get_base_location(); $wc_address_fields['country'] = $base_location['country']; $wc_address_fields['state'] = $base_location['state']; $wc_address_fields['address'] = ''; $wc_address_fields['address_2'] = ''; $wc_address_fields['city'] = ''; $wc_address_fields['postcode'] = ''; $stored_address_fields = get_option('wc_connect_origin_address', array()); return array_merge($wc_address_fields, $stored_address_fields); }
/** * Output manual country select form */ public function country_select() { $all_countries = WC()->countries->get_countries(); $base_country = wc_get_base_location(); $countries[$base_country['country']] = $all_countries[$base_country['country']]; foreach (TFLS()->get_regions() as $region) { foreach ($region['countries'] as $country) { if (!array_key_exists($country, $countries)) { $countries[$country] = $all_countries[$country]; } } } asort($countries); $other_country = key(array_diff_key($all_countries, $countries)); $countries[$other_country] = apply_filters('tfls_other_countries_text', __('Other countries')); wc_get_template('country-selector.php', array('countries' => $countries), 'woocommerce-the-fairy-light-shop/', untrailingslashit(plugin_dir_path(TFLS_FILE)) . '/templates/'); }
/** * get_taxable_address function. * * @return array */ public function get_taxable_address() { $tax_based_on = get_option('woocommerce_tax_based_on'); // Check shipping method at this point to see if we need special handling if (true == apply_filters('woocommerce_apply_base_tax_for_local_pickup', true) && WC()->cart->needs_shipping() && sizeof(array_intersect(WC()->session->get('chosen_shipping_methods', array(get_option('woocommerce_default_shipping_method'))), apply_filters('woocommerce_local_pickup_methods', array('local_pickup')))) > 0) { $tax_based_on = 'base'; } if ($tax_based_on == 'base') { $default = wc_get_base_location(); $country = $default['country']; $state = $default['state']; $postcode = ''; $city = ''; } elseif ($tax_based_on == 'billing') { $country = $this->get_country(); $state = $this->get_state(); $postcode = $this->get_postcode(); $city = $this->get_city(); } else { $country = $this->get_shipping_country(); $state = $this->get_shipping_state(); $postcode = $this->get_shipping_postcode(); $city = $this->get_shipping_city(); } return apply_filters('woocommerce_customer_taxable_address', array($country, $state, $postcode, $city)); }
/** * Get the base state for the store. * @return string */ public function get_base_state() { $default = wc_get_base_location(); return apply_filters('woocommerce_countries_base_state', $default['state']); }
/** * Calc line tax. */ public static function calc_line_taxes() { global $wpdb; check_ajax_referer('calc-totals', 'security'); if (!current_user_can('edit_shop_orders')) { die(-1); } $tax = new WC_Tax(); $tax_based_on = get_option('woocommerce_tax_based_on'); $order_id = absint($_POST['order_id']); $items = array(); $country = strtoupper(esc_attr($_POST['country'])); $state = strtoupper(esc_attr($_POST['state'])); $postcode = strtoupper(esc_attr($_POST['postcode'])); $city = wc_clean(esc_attr($_POST['city'])); $order = wc_get_order($order_id); $taxes = array(); $shipping_taxes = array(); // Default to base if ('base' === $tax_based_on || empty($country)) { $default = wc_get_base_location(); $country = $default['country']; $state = $default['state']; $postcode = ''; $city = ''; } // Parse the jQuery serialized items parse_str($_POST['items'], $items); // Prevent undefined warnings if (!isset($items['line_tax'])) { $items['line_tax'] = array(); } if (!isset($items['line_subtotal_tax'])) { $items['line_subtotal_tax'] = array(); } $items['order_taxes'] = array(); // Action $items = apply_filters('woocommerce_ajax_calc_line_taxes', $items, $order_id, $country, $_POST); // Get items and fees taxes if (isset($items['order_item_id'])) { $line_total = $line_subtotal = $order_item_tax_class = array(); foreach ($items['order_item_id'] as $item_id) { $item_id = absint($item_id); $line_total[$item_id] = isset($items['line_total'][$item_id]) ? wc_format_decimal($items['line_total'][$item_id]) : 0; $line_subtotal[$item_id] = isset($items['line_subtotal'][$item_id]) ? wc_format_decimal($items['line_subtotal'][$item_id]) : $line_total[$item_id]; $order_item_tax_class[$item_id] = isset($items['order_item_tax_class'][$item_id]) ? sanitize_text_field($items['order_item_tax_class'][$item_id]) : ''; $product_id = $order->get_item_meta($item_id, '_product_id', true); // Get product details if (get_post_type($product_id) == 'product') { $_product = wc_get_product($product_id); $item_tax_status = $_product->get_tax_status(); } else { $item_tax_status = 'taxable'; } if ('0' !== $order_item_tax_class[$item_id] && 'taxable' === $item_tax_status) { $tax_rates = WC_Tax::find_rates(array('country' => $country, 'state' => $state, 'postcode' => $postcode, 'city' => $city, 'tax_class' => $order_item_tax_class[$item_id])); $line_taxes = WC_Tax::calc_tax($line_total[$item_id], $tax_rates, false); $line_subtotal_taxes = WC_Tax::calc_tax($line_subtotal[$item_id], $tax_rates, false); // Set the new line_tax foreach ($line_taxes as $_tax_id => $_tax_value) { $items['line_tax'][$item_id][$_tax_id] = $_tax_value; } // Set the new line_subtotal_tax foreach ($line_subtotal_taxes as $_tax_id => $_tax_value) { $items['line_subtotal_tax'][$item_id][$_tax_id] = $_tax_value; } // Sum the item taxes foreach (array_keys($taxes + $line_taxes) as $key) { $taxes[$key] = (isset($line_taxes[$key]) ? $line_taxes[$key] : 0) + (isset($taxes[$key]) ? $taxes[$key] : 0); } } } } // Get shipping taxes if (isset($items['shipping_method_id'])) { $matched_tax_rates = array(); $tax_rates = WC_Tax::find_rates(array('country' => $country, 'state' => $state, 'postcode' => $postcode, 'city' => $city, 'tax_class' => '')); if ($tax_rates) { foreach ($tax_rates as $key => $rate) { if (isset($rate['shipping']) && 'yes' == $rate['shipping']) { $matched_tax_rates[$key] = $rate; } } } $shipping_cost = $shipping_taxes = array(); foreach ($items['shipping_method_id'] as $item_id) { $item_id = absint($item_id); $shipping_cost[$item_id] = isset($items['shipping_cost'][$item_id]) ? wc_format_decimal($items['shipping_cost'][$item_id]) : 0; $_shipping_taxes = WC_Tax::calc_shipping_tax($shipping_cost[$item_id], $matched_tax_rates); // Set the new shipping_taxes foreach ($_shipping_taxes as $_tax_id => $_tax_value) { $items['shipping_taxes'][$item_id][$_tax_id] = $_tax_value; $shipping_taxes[$_tax_id] = isset($shipping_taxes[$_tax_id]) ? $shipping_taxes[$_tax_id] + $_tax_value : $_tax_value; } } } // Remove old tax rows $order->remove_order_items('tax'); // Add tax rows foreach (array_keys($taxes + $shipping_taxes) as $tax_rate_id) { $order->add_tax($tax_rate_id, isset($taxes[$tax_rate_id]) ? $taxes[$tax_rate_id] : 0, isset($shipping_taxes[$tax_rate_id]) ? $shipping_taxes[$tax_rate_id] : 0); } // Create the new order_taxes foreach ($order->get_taxes() as $tax_id => $tax_item) { $items['order_taxes'][$tax_id] = absint($tax_item['rate_id']); } $items = apply_filters('woocommerce_ajax_after_calc_line_taxes', $items, $order_id, $country, $_POST); // Save order items wc_save_order_items($order_id, $items); // Return HTML items $order = wc_get_order($order_id); $data = get_post_meta($order_id); include 'admin/meta-boxes/views/html-order-items.php'; die; }
/** * Calculate wcmp vendor shipping tax * * @param double $shipping_amount * @param object $order */ public function calculate_shipping_tax($shipping_amount, $order) { global $WCMp, $woocommerce; $wc_tax_enabled = get_option('woocommerce_calc_taxes'); if ('no' === $wc_tax_enabled) { return 0; } $tax_based_on = get_option('woocommerce_tax_based_on'); $WC_Tax = new WC_Tax(); if ('base' === $tax_based_on) { $default = wc_get_base_location(); $country = $default['country']; $state = $default['state']; $postcode = ''; $city = ''; } elseif ('billing' === $tax_based_on) { $country = $order->billing_country; $state = $order->billing_state; $postcode = $order->billing_postcode; $city = $order->billing_city; } else { $country = $order->shipping_country; $state = $order->shipping_state; $postcode = $order->shipping_postcode; $city = $order->shipping_city; } $matched_tax_rates = array(); $tax_rates = $WC_Tax->find_rates(array('country' => $country, 'state' => $state, 'postcode' => $postcode, 'city' => $city, 'tax_class' => '')); if ($tax_rates) { foreach ($tax_rates as $key => $rate) { if (isset($rate['shipping']) && 'yes' === $rate['shipping']) { $matched_tax_rates[$key] = $rate; } } } $vendor_shipping_taxes = $WC_Tax->calc_shipping_tax($shipping_amount, $matched_tax_rates); $vendor_shipping_tax_total = $WC_Tax->round(array_sum($vendor_shipping_taxes)); return $vendor_shipping_tax_total; }
function maybe_exclude_vat() { if ((is_checkout() || is_cart() || defined('WOOCOMMERCE_CHECKOUT') || defined('WOOCOMMERCE_CART') || defined('DOING_AJAX') && DOING_AJAX) && !empty(WC()->customer) && 'yes' === get_option('wcj_eu_vat_number_validate', 'yes') && 'yes' === get_option('wcj_eu_vat_number_disable_for_valid', 'yes') && isset($_SESSION['wcj_is_eu_vat_number_valid']) && true === $_SESSION['wcj_is_eu_vat_number_valid'] && isset($_SESSION['wcj_eu_vat_number_to_check'])) { $preserve_base_country_check_passed = true; if ('yes' === apply_filters('booster_get_option', 'no', get_option('wcj_eu_vat_number_preserve_in_base_country', 'no'))) { $location = wc_get_base_location(); if (empty($location['country'])) { $location = wc_format_country_state_string(apply_filters('woocommerce_customer_default_location', get_option('woocommerce_default_country'))); } $selected_country = substr($_SESSION['wcj_eu_vat_number_to_check'], 0, 2); if ('EL' === $selected_country) { $selected_country = 'GR'; } $preserve_base_country_check_passed = $location['country'] !== $selected_country ? true : false; } if ($preserve_base_country_check_passed) { /* $modified_matched_tax_rates = array(); foreach ( $matched_tax_rates as $i => $matched_tax_rate ) { $matched_tax_rate['rate'] = 0; $modified_matched_tax_rates[ $i ] = $matched_tax_rate; } return $modified_matched_tax_rates; */ WC()->customer->set_is_vat_exempt(true); } else { WC()->customer->set_is_vat_exempt(false); } } else { if (!empty(WC()->customer)) { WC()->customer->set_is_vat_exempt(false); } } // return $matched_tax_rates; }
/** * Calculate taxes for all line items and shipping, and store the totals and tax rows. * * Will use the base country unless customer addresses are set. * * @return bool success or fail */ public function calculate_taxes() { $tax_total = 0; $taxes = array(); $tax_based_on = get_option('woocommerce_tax_based_on'); if ('billing' === $tax_based_on) { $country = $this->billing_country; $state = $this->billing_state; $postcode = $this->billing_postcode; $city = $this->billing_city; } elseif ('shipping' === $tax_based_on) { $country = $this->shipping_country; $state = $this->shipping_state; $postcode = $this->shipping_postcode; $city = $this->shipping_city; } // Default to base if ('base' === $tax_based_on || empty($country)) { $default = wc_get_base_location(); $country = $default['country']; $state = $default['state']; $postcode = ''; $city = ''; } // Get items foreach ($this->get_items(array('line_item', 'fee')) as $item_id => $item) { $product = $this->get_product_from_item($item); $line_total = isset($item['line_total']) ? $item['line_total'] : 0; $line_subtotal = isset($item['line_subtotal']) ? $item['line_subtotal'] : 0; $tax_class = $item['tax_class']; $item_tax_status = $product ? $product->get_tax_status() : 'taxable'; if ('0' !== $tax_class && 'taxable' === $item_tax_status) { $tax_rates = WC_Tax::find_rates(array('country' => $country, 'state' => $state, 'postcode' => $postcode, 'city' => $city, 'tax_class' => $tax_class)); $line_subtotal_taxes = WC_Tax::calc_tax($line_subtotal, $tax_rates, false); $line_taxes = WC_Tax::calc_tax($line_total, $tax_rates, false); $line_subtotal_tax = max(0, array_sum($line_subtotal_taxes)); $line_tax = max(0, array_sum($line_taxes)); $tax_total += $line_tax; wc_update_order_item_meta($item_id, '_line_subtotal_tax', wc_format_decimal($line_subtotal_tax)); wc_update_order_item_meta($item_id, '_line_tax', wc_format_decimal($line_tax)); wc_update_order_item_meta($item_id, '_line_tax_data', array('total' => $line_taxes, 'subtotal' => $line_subtotal_taxes)); // Sum the item taxes foreach (array_keys($taxes + $line_taxes) as $key) { $taxes[$key] = (isset($line_taxes[$key]) ? $line_taxes[$key] : 0) + (isset($taxes[$key]) ? $taxes[$key] : 0); } } } // Now calculate shipping tax $matched_tax_rates = array(); $tax_rates = WC_Tax::find_rates(array('country' => $country, 'state' => $state, 'postcode' => $postcode, 'city' => $city, 'tax_class' => '')); if (!empty($tax_rates)) { foreach ($tax_rates as $key => $rate) { if (isset($rate['shipping']) && 'yes' === $rate['shipping']) { $matched_tax_rates[$key] = $rate; } } } $shipping_taxes = WC_Tax::calc_shipping_tax($this->order_shipping, $matched_tax_rates); $shipping_tax_total = WC_Tax::round(array_sum($shipping_taxes)); // Save tax totals $this->set_total($shipping_tax_total, 'shipping_tax'); $this->set_total($tax_total, 'tax'); // Tax rows $this->remove_order_items('tax'); // Now merge to keep tax rows foreach (array_keys($taxes + $shipping_taxes) as $tax_rate_id) { $this->add_tax($tax_rate_id, isset($taxes[$tax_rate_id]) ? $taxes[$tax_rate_id] : 0, isset($shipping_taxes[$tax_rate_id]) ? $shipping_taxes[$tax_rate_id] : 0); } return true; }
/** * Output manual country select form */ public function country_select() { $all_countries = WC()->countries->get_countries(); $base_country = wc_get_base_location(); $countries[$base_country['country']] = $all_countries[$base_country['country']]; foreach (WCPBC()->get_regions() as $region) { foreach ($region['countries'] as $country) { if (!array_key_exists($country, $countries)) { $countries[$country] = $all_countries[$country]; } } } asort($countries); $other_country = key(array_diff_key($all_countries, $countries)); $countries[$other_country] = apply_filters('wcpbc_other_countries_text', __('Other countries')); wc_get_template('country-selector.php', array('countries' => $countries), 'woocommerce-product-price-based-on-countries/', WCPBC()->plugin_path() . '/templates/'); }
/** * Simplify Commerce is being removed from core. */ public static function simplify_commerce_notice() { $location = wc_get_base_location(); if (class_exists('WC_Gateway_Simplify_Commerce_Loader') || !in_array($location['country'], apply_filters('woocommerce_gateway_simplify_commerce_supported_countries', array('US', 'IE')))) { self::remove_notice('simplify_commerce'); return; } if (empty($_GET['action'])) { include 'views/html-notice-simplify-commerce.php'; } }
public function get_paper_size() { $paper_size = NULL; //$this->settings_store->get_preferred_paper_size(); if ($paper_size) { return $paper_size; } // According to https://en.wikipedia.org/wiki/Letter_(paper_size) US, Mexico, Canada and Dominican Republic // use "Letter" size, and pretty much all the rest of the world use A4, so those are sensible defaults $base_location = wc_get_base_location(); if (in_array($base_location['country'], array('US', 'CA', 'MX', 'DO'))) { return 'letter'; } return 'a4'; }
public function admin_options() { $current_user = wp_get_current_user(); $section_slug = strtolower(get_class($this)); $production_connect_url = 'https://connect.woocommerce.com/login/braintree'; $sandbox_connect_url = 'https://connect.woocommerce.com/login/braintreesandbox'; $redirect_url = add_query_arg(array('page' => 'wc-settings', 'tab' => 'checkout', 'section' => $section_slug), admin_url('admin.php')); $redirect_url = wp_nonce_url($redirect_url, 'connect_paypal_braintree', 'wc_paypal_braintree_admin_nonce'); // Note: We doubly urlencode the redirect url to avoid Braintree's server // decoding it which would cause loss of query params on the final redirect // Note: Although the Partner API expects an array // ( per https://developers.braintreepayments.com/guides/partner-api/sign-up/php ) // our middleware presently wants things flattened, so instead of passing a user // array and a business array, we pass selected fields with user_ and business_ // prepended $query_args = array('redirect' => urlencode(urlencode($redirect_url)), 'scopes' => 'read_write'); $current_user = wp_get_current_user(); $query_args['user_email'] = $current_user->user_email; if (!empty($current_user->user_firstname)) { $query_args['user_firstName'] = $current_user->user_firstname; } if (!empty($current_user->user_lastname)) { $query_args['user_lastName'] = $current_user->user_lastname; } $query_args['business_currency'] = get_woocommerce_currency(); // Let's go ahead and assume the user and business are in the same region and country, // because they probably are. If not, they can edit these anyways $base_location = wc_get_base_location(); if (array_key_exists('country', $base_location)) { $country = $base_location['country']; if (!empty($country)) { $query_args['business_country'] = $country; $query_args['user_country'] = $country; } } if (array_key_exists('state', $base_location)) { $state = $base_location['state']; if (!empty($state)) { $query_args['business_region'] = $state; $query_args['user_region'] = $state; } } $site_name = get_bloginfo('name'); if (!empty($site_name)) { $query_args['business_name'] = $site_name; } $site_description = get_bloginfo('description'); if (!empty($site_description)) { $query_args['business_description'] = $site_description; } $query_args['business_website'] = get_bloginfo('url'); $production_connect_url = add_query_arg($query_args, $production_connect_url); $sandbox_connect_url = add_query_arg($query_args, $sandbox_connect_url); $disconnect_url = add_query_arg(array('page' => 'wc-settings', 'tab' => 'checkout', 'section' => $section_slug, 'disconnect_paypal_braintree' => 1), admin_url('admin.php')); $disconnect_url = wp_nonce_url($disconnect_url, 'disconnect_paypal_braintree', 'wc_paypal_braintree_admin_nonce'); ?> <div class='paypal-braintree-admin-header'> <div class='paypal-braintree-admin-brand'> <img src="<?php echo plugins_url('../assets/images/branding/paypal-braintree-horizontal.png', __FILE__); ?> " /> </div> <div class='paypal-braintree-admin-payment-methods'> <img src="<?php echo plugins_url('../assets/images/payments/visa.png', __FILE__); ?> " /> <img src="<?php echo plugins_url('../assets/images/payments/master-card.png', __FILE__); ?> " /> <img src="<?php echo plugins_url('../assets/images/payments/discover.png', __FILE__); ?> " /> <img src="<?php echo plugins_url('../assets/images/payments/american-express.png', __FILE__); ?> " /> <img src="<?php echo plugins_url('../assets/images/payments/paypal.png', __FILE__); ?> " /> </div> </div> <?php if (empty($this->merchant_access_token)) { ?> <p class='paypal-braintree-admin-connect-prompt'> <?php echo esc_html('Connect with Braintree to start accepting credit and debit card payments in your checkout.', 'woocommerce-gateway-paypal-braintree'); ?> <br/> <a href="https://www.braintreepayments.com/partners/learn-more" target="_blank"> <?php echo esc_html('Learn more', 'woocommerce-gateway-paypal-braintree'); ?> </a> </p> <?php } ?> <table class="form-table"> <tbody> <tr> <th> <?php _e('Connect/Disconnect', 'woocommerce-gateway-paypal-braintree'); ?> <?php $connect_help_tip = __('Click button to create an account with Braintree and start transacting.', 'woocommerce-gateway-paypal-braintree'); if (!empty($this->merchant_access_token)) { $connect_help_tip = sprintf('%s<br><br>%s<br><br>%s', __('You just connected your Braintree account to WooCommerce. You can start taking payments now.', 'woocommerce-gateway-paypal-braintree'), __('Once you have processed a payment, PayPal will review your application for final approval. Before you ship any goods make sure you have received a final approval for your Braintree account.', 'woocommerce-gateway-paypal-braintree'), __('Questions? We are a phone call away: 1-855-489-0345.', 'woocommerce-gateway-paypal-braintree')); } echo wc_help_tip($connect_help_tip); ?> </th> <td> <?php if (!empty($this->merchant_access_token)) { ?> <a href="<?php echo esc_attr($disconnect_url); ?> " class='button-primary'> <?php echo esc_html__('Disconnect from PayPal Powered by Braintree', 'woocommerce-gateway-paypal-braintree'); ?> </a> <?php } else { ?> <a href="<?php echo esc_attr($production_connect_url); ?> "> <img src="<?php echo plugins_url('../assets/images/button/connect-braintree.png', __FILE__); ?> "/> </a> <br/> <br/> <a href="<?php echo esc_attr($sandbox_connect_url); ?> "> <?php echo esc_html__('Not ready to accept live payments? Click here to connect using sandbox mode.', 'woocommerce-gateway-paypal-braintree'); ?> </a> <?php } ?> </td> </tr> </tbody> </table> <table class="form-table"> <?php $this->generate_settings_html(); ?> </table> <?php }
protected function _calculate_fee_taxes(&$order) { $tax_total = 0; $shipping_tax_total = 0; $taxes = array(); $shipping_taxes = array(); $tax_based_on = get_option('woocommerce_tax_based_on'); // If is_vat_exempt is 'yes', or wc_tax_enabled is false, return and do nothing. if ('yes' === $order->is_vat_exempt || !wc_tax_enabled()) { return false; } if ('billing' === $tax_based_on) { $country = $order->billing_country; $state = $order->billing_state; $postcode = $order->billing_postcode; $city = $order->billing_city; } elseif ('shipping' === $tax_based_on) { $country = $order->shipping_country; $state = $order->shipping_state; $postcode = $order->shipping_postcode; $city = $order->shipping_city; } // Default to base if ('base' === $tax_based_on || empty($country)) { $default = wc_get_base_location(); $country = $default['country']; $state = $default['state']; $postcode = ''; $city = ''; } // Get items foreach ($order->get_items(array('fee')) as $item_id => $item) { $product = $order->get_product_from_item($item); $line_total = isset($item['line_total']) ? $item['line_total'] : 0; $line_subtotal = isset($item['line_subtotal']) ? $item['line_subtotal'] : 0; $tax_class = $item['tax_class']; $item_tax_status = $product ? $product->get_tax_status() : 'taxable'; if ('0' !== $tax_class && 'taxable' === $item_tax_status) { $tax_rates = WC_Tax::find_rates(array('country' => $country, 'state' => $state, 'postcode' => $postcode, 'city' => $city, 'tax_class' => $tax_class)); $line_subtotal_taxes = WC_Tax::calc_tax($line_subtotal, $tax_rates, false); $line_taxes = WC_Tax::calc_tax($line_total, $tax_rates, false); $line_subtotal_tax = max(0, array_sum($line_subtotal_taxes)); $line_tax = max(0, array_sum($line_taxes)); $tax_total += $line_tax; wc_update_order_item_meta($item_id, '_line_subtotal_tax', wc_format_decimal($line_subtotal_tax)); wc_update_order_item_meta($item_id, '_line_tax', wc_format_decimal($line_tax)); wc_update_order_item_meta($item_id, '_line_tax_data', array('total' => $line_taxes, 'subtotal' => $line_subtotal_taxes)); // Sum the item taxes foreach (array_keys($taxes + $line_taxes) as $key) { $taxes[$key] = (isset($line_taxes[$key]) ? $line_taxes[$key] : 0) + (isset($taxes[$key]) ? $taxes[$key] : 0); } } } }
/** * Output the settings */ public function output() { global $current_section; if ($current_section) { $base_country = wc_get_base_location(); $not_available_countries = array(); $regions = get_option('wc_price_based_country_regions', array()); foreach ($regions as $key => $value) { foreach ($value['countries'] as $code) { if ($current_section !== $key) { $not_available_countries[] = $code; } } } if ($current_section == 'new_group') { $this->section_settings($not_available_countries); } else { if (isset($regions[$current_section])) { $this->section_settings($not_available_countries, $regions[$current_section]); } } } else { parent::output(); } }
/** * Test wc_get_base_location(). * * @since 2.3.0 */ public function test_wc_get_base_location() { $default = wc_get_base_location(); $this->assertEquals('GB', $default['country']); $this->assertEquals('', $default['state']); }
public static function calculate_shipping_tax($shipping_amount, $order) { $tax_based_on = get_option('woocommerce_tax_based_on'); $wc_tax_enabled = get_option('woocommerce_calc_taxes'); // if taxes aren't enabled don't calculate them if ('no' === $wc_tax_enabled) { return 0; } if ('base' === $tax_based_on) { $default = wc_get_base_location(); $country = $default['country']; $state = $default['state']; $postcode = ''; $city = ''; } elseif ('billing' === $tax_based_on) { $country = $order->billing_country; $state = $order->billing_state; $postcode = $order->billing_postcode; $city = $order->billing_city; } else { $country = $order->shipping_country; $state = $order->shipping_state; $postcode = $order->shipping_postcode; $city = $order->shipping_city; } // Now calculate shipping tax $matched_tax_rates = array(); $tax_rates = WC_Tax::find_rates(array('country' => $country, 'state' => $state, 'postcode' => $postcode, 'city' => $city, 'tax_class' => '')); if ($tax_rates) { foreach ($tax_rates as $key => $rate) { if (isset($rate['shipping']) && 'yes' === $rate['shipping']) { $matched_tax_rates[$key] = $rate; } } } $shipping_taxes = WC_Tax::calc_shipping_tax($shipping_amount, $matched_tax_rates); $shipping_tax_total = WC_Tax::round(array_sum($shipping_taxes)); return $shipping_tax_total; }
/** * Calculate taxes for all line items and shipping, and store the totals and tax rows. * * Will use the base country unless customer addresses are set. * * @return bool success or fail. */ public function calculate_taxes() { $tax_total = 0; $shipping_tax_total = 0; $taxes = array(); $shipping_taxes = array(); $tax_based_on = get_option('woocommerce_tax_based_on'); // If is_vat_exempt is 'yes', or wc_tax_enabled is false, return and do nothing. if ('yes' === $this->is_vat_exempt || !wc_tax_enabled()) { return false; } if ('billing' === $tax_based_on) { $country = $this->billing_country; $state = $this->billing_state; $postcode = $this->billing_postcode; $city = $this->billing_city; } elseif ('shipping' === $tax_based_on) { $country = $this->shipping_country; $state = $this->shipping_state; $postcode = $this->shipping_postcode; $city = $this->shipping_city; } // Default to base if ('base' === $tax_based_on || empty($country)) { $default = wc_get_base_location(); $country = $default['country']; $state = $default['state']; $postcode = ''; $city = ''; } // Get items foreach ($this->get_items(array('line_item', 'fee')) as $item_id => $item) { $product = $this->get_product_from_item($item); $line_total = isset($item['line_total']) ? $item['line_total'] : 0; $line_subtotal = isset($item['line_subtotal']) ? $item['line_subtotal'] : 0; $tax_class = $item['tax_class']; $item_tax_status = $product ? $product->get_tax_status() : 'taxable'; if ('0' !== $tax_class && 'taxable' === $item_tax_status) { $tax_rates = WC_Tax::find_rates(array('country' => $country, 'state' => $state, 'postcode' => $postcode, 'city' => $city, 'tax_class' => $tax_class)); $line_subtotal_taxes = WC_Tax::calc_tax($line_subtotal, $tax_rates, false); $line_taxes = WC_Tax::calc_tax($line_total, $tax_rates, false); $line_subtotal_tax = max(0, array_sum($line_subtotal_taxes)); $line_tax = max(0, array_sum($line_taxes)); $tax_total += $line_tax; wc_update_order_item_meta($item_id, '_line_subtotal_tax', wc_format_decimal($line_subtotal_tax)); wc_update_order_item_meta($item_id, '_line_tax', wc_format_decimal($line_tax)); wc_update_order_item_meta($item_id, '_line_tax_data', array('total' => $line_taxes, 'subtotal' => $line_subtotal_taxes)); // Sum the item taxes foreach (array_keys($taxes + $line_taxes) as $key) { $taxes[$key] = (isset($line_taxes[$key]) ? $line_taxes[$key] : 0) + (isset($taxes[$key]) ? $taxes[$key] : 0); } } } // Calc taxes for shipping foreach ($this->get_shipping_methods() as $item_id => $item) { $shipping_tax_class = get_option('woocommerce_shipping_tax_class'); // Inherit tax class from items if ('' === $shipping_tax_class) { $tax_classes = WC_Tax::get_tax_classes(); foreach ($tax_classes as $tax_class) { $tax_class = sanitize_title($tax_class); if (in_array($tax_class, $found_tax_classes)) { $tax_rates = WC_Tax::find_shipping_rates(array('country' => $country, 'state' => $state, 'postcode' => $postcode, 'city' => $city, 'tax_class' => $tax_class)); break; } } } else { $tax_rates = WC_Tax::find_shipping_rates(array('country' => $country, 'state' => $state, 'postcode' => $postcode, 'city' => $city, 'tax_class' => 'standard' === $shipping_tax_class ? '' : $shipping_tax_class)); } $line_taxes = WC_Tax::calc_tax($item['cost'], $tax_rates, false); $line_tax = max(0, array_sum($line_taxes)); $shipping_tax_total += $line_tax; wc_update_order_item_meta($item_id, '_line_tax', wc_format_decimal($line_tax)); wc_update_order_item_meta($item_id, '_line_tax_data', array('total' => $line_taxes)); // Sum the item taxes foreach (array_keys($shipping_taxes + $line_taxes) as $key) { $shipping_taxes[$key] = (isset($line_taxes[$key]) ? $line_taxes[$key] : 0) + (isset($shipping_taxes[$key]) ? $shipping_taxes[$key] : 0); } } // Save tax totals $this->set_total($shipping_tax_total, 'shipping_tax'); $this->set_total($tax_total, 'tax'); // Tax rows $this->remove_order_items('tax'); // Now merge to keep tax rows foreach (array_keys($taxes + $shipping_taxes) as $tax_rate_id) { $this->add_tax($tax_rate_id, isset($taxes[$tax_rate_id]) ? $taxes[$tax_rate_id] : 0, isset($shipping_taxes[$tax_rate_id]) ? $shipping_taxes[$tax_rate_id] : 0); } return true; }
/** * Calculate taxes for all line items and shipping, and store the totals and tax rows. * * Will use the base country unless customer addresses are set. * @param $args array Added in 2.7.0 to pass things like location. */ public function calculate_taxes($args = array()) { $tax_based_on = get_option('woocommerce_tax_based_on'); $args = wp_parse_args($args, array('country' => 'billing' === $tax_based_on ? $this->get_billing_country() : $this->get_shipping_country(), 'state' => 'billing' === $tax_based_on ? $this->get_billing_state() : $this->get_shipping_state(), 'postcode' => 'billing' === $tax_based_on ? $this->get_billing_postcode() : $this->get_shipping_postcode(), 'city' => 'billing' === $tax_based_on ? $this->get_billing_city() : $this->get_shipping_city())); // Default to base if ('base' === $tax_based_on || empty($args['country'])) { $default = wc_get_base_location(); $args['country'] = $default['country']; $args['state'] = $default['state']; $args['postcode'] = ''; $args['city'] = ''; } // Calc taxes for line items foreach ($this->get_items(array('line_item', 'fee')) as $item_id => $item) { $tax_class = $item->get_tax_class(); $tax_status = $item->get_tax_status(); if ('0' !== $tax_class && 'taxable' === $tax_status) { $tax_rates = WC_Tax::find_rates(array('country' => $args['country'], 'state' => $args['state'], 'postcode' => $args['postcode'], 'city' => $args['city'], 'tax_class' => $tax_class)); $total = $item->get_total(); $taxes = WC_Tax::calc_tax($total, $tax_rates, false); if ($item->is_type('line_item')) { $subtotal = $item->get_subtotal(); $subtotal_taxes = WC_Tax::calc_tax($subtotal, $tax_rates, false); $item->set_taxes(array('total' => $taxes, 'subtotal' => $subtotal_taxes)); } else { $item->set_taxes(array('total' => $taxes)); } $item->save(); } } // Calc taxes for shipping foreach ($this->get_shipping_methods() as $item_id => $item) { $shipping_tax_class = get_option('woocommerce_shipping_tax_class'); // Inherit tax class from items if ('' === $shipping_tax_class) { $tax_rates = array(); $tax_classes = array_merge(array(''), WC_Tax::get_tax_classes()); $found_tax_classes = $this->get_items_tax_classes(); foreach ($tax_classes as $tax_class) { $tax_class = sanitize_title($tax_class); if (in_array($tax_class, $found_tax_classes)) { $tax_rates = WC_Tax::find_shipping_rates(array('country' => $args['country'], 'state' => $args['state'], 'postcode' => $args['postcode'], 'city' => $args['city'], 'tax_class' => $tax_class)); break; } } } else { $tax_rates = WC_Tax::find_shipping_rates(array('country' => $args['country'], 'state' => $args['state'], 'postcode' => $args['postcode'], 'city' => $args['city'], 'tax_class' => 'standard' === $shipping_tax_class ? '' : $shipping_tax_class)); } $item->set_taxes(array('total' => WC_Tax::calc_tax($item->get_total(), $tax_rates, false))); $item->save(); } $this->update_taxes(); }
/** * init function. * * @access public * @return void */ private function init() { // Load the settings. $this->init_form_fields(); $this->init_settings(); // Enqueue UPS Scripts wp_enqueue_script('ups-admin-js'); // Define user set variables $this->enabled = $this->get_option('enabled', $this->enabled); $this->title = $this->get_option('title', $this->method_title); $this->availability = $this->get_option('availability', 'all'); $this->countries = $this->get_option('countries', array()); $this->simple_advanced = $this->get_option('simple_advanced', 'simple'); // API Settings $this->user_id = $this->get_option('user_id'); $this->password = $this->get_option('password'); $this->access_key = $this->get_option('access_key'); $this->shipper_number = $this->get_option('shipper_number'); $this->negotiated = ($bool = $this->get_option('negotiated')) && $bool == 'yes' ? true : false; $this->origin_addressline = ($bool = $this->get_option('origin_addressline')) && $bool == 'yes' ? true : false; $this->origin_city = $this->get_option('origin_city'); $this->origin_postcode = $this->get_option('origin_postcode'); $this->origin_country_state = $this->get_option('origin_country_state'); $this->debug = ($bool = $this->get_option('debug')) && $bool == 'yes' ? true : false; // Pickup and Destination $this->pickup = $this->get_option('pickup', '01'); $this->residential = ($bool = $this->get_option('residential')) && $bool == 'yes' ? true : false; // Services and Packaging $this->offer_rates = $this->get_option('offer_rates', 'all'); $this->fallback = $this->get_option('fallback'); $this->packing_method = $this->get_option('packing_method', 'per_item'); $this->ups_packaging = $this->get_option('ups_packaging', array()); $this->custom_services = $this->get_option('services', array()); $this->boxes = $this->get_option('boxes', array()); $this->insuredvalue = ($bool = $this->get_option('insuredvalue')) && $bool == 'yes' ? true : false; $this->signature = $this->get_option('signature', 'none'); // Units $this->units = $this->get_option('units', 'imperial'); if ($this->units == 'metric') { $this->weight_unit = 'KGS'; $this->dim_unit = 'CM'; } else { $this->weight_unit = 'LBS'; $this->dim_unit = 'IN'; } /** * If no origin country / state saved / exists, set it to store base country: * Includes compat for < 2.3. */ if (!$this->origin_country_state) { if (defined('WC_VERSION') && version_compare(WC_VERSION, '2.3', '>=')) { $origin = wc_get_base_location(); $this->origin_country = $origin['country']; $this->origin_state = $origin['state']; } else { $base_country = get_option('woocommerce_default_country'); $this->split_country_state($base_country); } } else { $this->split_country_state($this->origin_country_state); } add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options')); add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'clear_transients')); add_action('admin_enqueue_scripts', array($this, 'assets')); }
/** * Is customer outside base country (for tax purposes)? * @return bool */ public function is_customer_outside_base() { list($country, $state) = $this->get_taxable_address(); if ($country) { $default = wc_get_base_location(); if ($default['country'] !== $country) { return true; } if ($default['state'] && $default['state'] !== $state) { return true; } } return false; }
/** * maybe_exclude_vat. */ function maybe_exclude_vat($matched_tax_rates, $tax_class) { /* wcj_log( explode( '&', $_POST['post_data'] ) ); */ /* if ( ! isset( $_POST['billing_eu_vat_number'] ) ) return $matched_tax_rates; */ if ('yes' === get_option('wcj_eu_vat_number_validate', 'yes') && 'yes' === get_option('wcj_eu_vat_number_disable_for_valid', 'yes') && isset($_SESSION['wcj_is_eu_vat_number_valid']) && true === $_SESSION['wcj_is_eu_vat_number_valid'] && isset($_SESSION['wcj_eu_vat_number_to_check'])) { $preserve_base_country_check_passed = true; if ('yes' === apply_filters('wcj_get_option_filter', 'no', get_option('wcj_eu_vat_number_preserve_in_base_country', 'no'))) { $location = wc_get_base_location(); if (empty($location['country'])) { $location = wc_format_country_state_string(apply_filters('woocommerce_customer_default_location', get_option('woocommerce_default_country'))); } $selected_country = substr($_SESSION['wcj_eu_vat_number_to_check'], 0, 2); $preserve_base_country_check_passed = $location['country'] !== $selected_country ? true : false; } if ($preserve_base_country_check_passed) { $modified_matched_tax_rates = array(); foreach ($matched_tax_rates as $i => $matched_tax_rate) { $matched_tax_rate['rate'] = 0; $modified_matched_tax_rates[$i] = $matched_tax_rate; } return $modified_matched_tax_rates; } } return $matched_tax_rates; }