/** * product_by_country. * * @version 2.5.0 * @since 2.5.0 */ function product_by_country($visible, $product_id) { // Get the country by IP $location = WC_Geolocation::geolocate_ip(); // Base fallback if (empty($location['country'])) { $location = wc_format_country_state_string(apply_filters('woocommerce_customer_default_location', get_option('woocommerce_default_country'))); } $country = isset($location['country']) ? $location['country'] : ''; $visible_countries = get_post_meta($product_id, '_' . 'wcj_product_by_country_visible', true); if (is_array($visible_countries) && !in_array($country, $visible_countries)) { return false; } return $visible; }
/** * Get the customer's default location. Filtered, and set to base location or left blank. * * If cache-busting, this should only be used when 'location' is set in the querystring. * * @todo should the woocommerce_default_country option be renamed to contain 'base'? * @since 2.3.0 * @return array */ function wc_get_customer_default_location() { switch (get_option('woocommerce_default_customer_address')) { case 'geolocation_ajax': case 'geolocation': $location = WC_Geolocation::geolocate_ip(); // Base fallback if (empty($location['country'])) { $location = wc_format_country_state_string(apply_filters('woocommerce_customer_default_location', get_option('woocommerce_default_country'))); } break; case 'base': $location = wc_format_country_state_string(apply_filters('woocommerce_customer_default_location', get_option('woocommerce_default_country'))); break; default: $location = wc_format_country_state_string(apply_filters('woocommerce_customer_default_location', '')); break; } return $location; }
/** * Test wc_format_country_state_string(). * * @since 2.6.0 */ public function test_wc_format_country_state_string() { // Test with correct values. $this->assertEquals(array('country' => 'US', 'state' => 'CA'), wc_format_country_state_string('US:CA')); // Test what happens when we pass an incorrect value. $this->assertEquals(array('country' => 'US-CA', 'state' => ''), wc_format_country_state_string('US-CA')); }
/** * wcj_get_user_location. * * @version 2.5.0 */ function wcj_get_user_location() { $country = ''; if (isset($_GET['country']) && '' != $_GET['country'] && wcj_is_user_role('administrator')) { $country = $_GET['country']; } else { // Get the country by IP $location = WC_Geolocation::geolocate_ip(); // Base fallback if (empty($location['country'])) { $location = wc_format_country_state_string(apply_filters('woocommerce_customer_default_location', get_option('woocommerce_default_country'))); } $country = isset($location['country']) ? $location['country'] : ''; } return $country; }
/** * 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; }
/** * Get the customer's default location. * * Filtered, and set to base location or left blank. If cache-busting, * this should only be used when 'location' is set in the querystring. * * @todo should the woocommerce_default_country option be renamed to contain 'base'? * @todo deprecate woocommerce_customer_default_location and support an array filter only to cover all cases. * @since 2.3.0 * @return array */ function wc_get_customer_default_location() { $location = array(); switch (get_option('woocommerce_default_customer_address')) { case 'geolocation_ajax': case 'geolocation': // Exclude common bots from geolocation by user agent. $ua = wc_get_user_agent(); if (!strstr($ua, 'bot') && !strstr($ua, 'spider') && !strstr($ua, 'crawl')) { $location = WC_Geolocation::geolocate_ip('', true, false); } // Base fallback. if (empty($location['country'])) { $location = wc_format_country_state_string(apply_filters('woocommerce_customer_default_location', get_option('woocommerce_default_country'))); } break; case 'base': $location = wc_format_country_state_string(apply_filters('woocommerce_customer_default_location', get_option('woocommerce_default_country'))); break; default: $location = wc_format_country_state_string(apply_filters('woocommerce_customer_default_location', '')); break; } return apply_filters('woocommerce_customer_default_location_array', $location); }
/** * get_debug_info. * function get_debug_info( $args ) { $html = ''; if ( 'yes' === get_option( 'wcj_price_by_country_local_enabled' ) ) { $html .= '<p>'; $html .= __( 'Price by Country on per Product Basis is enabled.', 'woocommerce-jetpack' ); $html .= '</p>'; } $data = array(); $data[] = array( '#', __( 'Countries', 'woocommerce-jetpack' ), __( 'Focus Country', 'woocommerce-jetpack' ), __( 'Regular Price', 'woocommerce-jetpack' ), __( 'Sale Price', 'woocommerce-jetpack' ) ); global $product; for ( $i = 1; $i <= apply_filters( 'wcj_get_option_filter', 1, get_option( 'wcj_price_by_country_total_groups_number', 1 ) ); $i++ ) { $row = array(); $row[] = $i; $country_exchange_rate_group = get_option( 'wcj_price_by_country_exchange_rate_countries_group_' . $i ); $country_exchange_rate_group = str_replace( ' ', '', $country_exchange_rate_group ); $row[] = $country_exchange_rate_group; $country_exchange_rate_group = explode( ',', $country_exchange_rate_group ); $_GET['country'] = $country_exchange_rate_group[0]; $row[] = $country_exchange_rate_group[0]; $currency_code = wcj_get_currency_symbol( get_option( 'wcj_price_by_country_exchange_rate_currency_group_' . $i ) ); $row[] = $product->get_regular_price() . ' ' . $currency_code; $row[] = $product->get_sale_price() . ' ' . $currency_code; $data[] = $row; } //$html .= wcj_get_table_html( $data, '', false ); $html = wcj_get_table_html( $data, array( 'table_heading_type' => 'vertical', ) ); return $html; } /** * fix_variable_product_price_on_sale. * public function fix_variable_product_price_on_sale( $price, $product ) { if ( $product->is_type( 'variable' ) ) { if ( ! $product->is_on_sale() ) { $start_position = strpos( $price, '<del>' ); $length = strpos( $price, '</del>' ) - $start_position; // Fixing the price, i.e. removing the sale tags return substr_replace( $price, '', $start_position, $length ); } } // No changes return $price; } /** * get_customer_country_group_id. * * @version 2.2.6 */ public function get_customer_country_group_id() { // We already know the group - nothing to calculate - return group // if ( null != $this->customer_country_group_id && $this->customer_country_group_id > 0 ) // return $this->customer_country_group_id; // We've already tried - no country was detected, no need to try again /* if ( -1 === $this->customer_country_group_id ) return null; */ if (isset($_GET['country']) && '' != $_GET['country'] && is_super_admin()) { $country = $_GET['country']; } elseif ('yes' === get_option('wcj_price_by_country_override_on_checkout_with_billing_country', 'no') && is_checkout() && '' != WC()->customer->get_country()) { $country = WC()->customer->get_country(); } else { if ('by_ip' === get_option('wcj_price_by_country_customer_country_detection_method', 'by_ip')) { // Get the country by IP $location = WC_Geolocation::geolocate_ip(); // Base fallback if (empty($location['country'])) { $location = wc_format_country_state_string(apply_filters('woocommerce_customer_default_location', get_option('woocommerce_default_country'))); } $country = isset($location['country']) ? $location['country'] : null; } elseif ('by_user_selection' === get_option('wcj_price_by_country_customer_country_detection_method', 'by_ip')) { /* $form_method = get_option( 'wcj_price_by_country_country_selection_box_method', 'get' ); if ( 'get' == $form_method ) { $country = ( isset( $_GET[ 'wcj-country' ] ) ) ? $_GET[ 'wcj-country' ] : null; } else { $country = ( isset( $_POST[ 'wcj-country' ] ) ) ? $_POST[ 'wcj-country' ] : null; } */ $country = isset($_SESSION['wcj-country']) ? $_SESSION['wcj-country'] : null; } elseif ('by_wpml' === get_option('wcj_price_by_country_customer_country_detection_method', 'by_ip')) { $country = defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : null; } } if (null === $country) { $this->customer_country_group_id = -1; return null; } // Get the country group id - go through all the groups, first found group is returned for ($i = 1; $i <= apply_filters('wcj_get_option_filter', 1, get_option('wcj_price_by_country_total_groups_number', 1)); $i++) { $country_exchange_rate_group = get_option('wcj_price_by_country_exchange_rate_countries_group_' . $i); $country_exchange_rate_group = str_replace(' ', '', $country_exchange_rate_group); $country_exchange_rate_group = explode(',', $country_exchange_rate_group); if (in_array($country, $country_exchange_rate_group)) { $this->customer_country_group_id = $i; //wcj_log( 'customer_country_group_id=' . $this->customer_country_group_id ); return $i; } } // No country group found $this->customer_country_group_id = -1; return null; }
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; }
/** * get_customer_country_by_ip. * * @version 2.5.1 * @since 2.5.0 */ function get_customer_country_by_ip() { if (class_exists('WC_Geolocation')) { // Get the country by IP $location = WC_Geolocation::geolocate_ip(); // Base fallback if (empty($location['country'])) { $location = wc_format_country_state_string(apply_filters('woocommerce_customer_default_location', get_option('woocommerce_default_country'))); } return isset($location['country']) ? $location['country'] : null; } else { return null; } }
/** * get_customer_country_group_id. */ public function get_customer_country_group_id() { // We already know the group - nothing to calculate - return group // if ( null != $this->customer_country_group_id && $this->customer_country_group_id > 0 ) // return $this->customer_country_group_id; // We've already tried - no country was detected, no need to try again if (-1 === $this->customer_country_group_id) { return null; } if (isset($_GET['country']) && '' != $_GET['country'] && is_super_admin()) { $country = $_GET['country']; } else { // Get the country by IP $location = WC_Geolocation::geolocate_ip(); // Base fallback if (empty($location['country'])) { $location = wc_format_country_state_string(apply_filters('woocommerce_customer_default_location', get_option('woocommerce_default_country'))); } $country = isset($location['country']) ? $location['country'] : null; } if (null === $country) { $this->customer_country_group_id = -1; return null; } // Get the country group id - go through all the groups, first found group is returned for ($i = 1; $i <= apply_filters('wcj_get_option_filter', 1, get_option('wcj_price_by_country_total_groups_number', 1)); $i++) { $country_exchange_rate_group = get_option('wcj_price_by_country_exchange_rate_countries_group_' . $i); $country_exchange_rate_group = str_replace(' ', '', $country_exchange_rate_group); $country_exchange_rate_group = explode(',', $country_exchange_rate_group); if (in_array($country, $country_exchange_rate_group)) { $this->customer_country_group_id = $i; //wcj_log( 'customer_country_group_id=' . $this->customer_country_group_id ); return $i; } } // No country group found $this->customer_country_group_id = -1; return null; }