/**
  * get_order_minimum_amount_with_user_roles.
  *
  * @version 2.5.3
  * @since   2.5.3
  */
 function get_order_minimum_amount_with_user_roles()
 {
     $minimum = get_option('wcj_order_minimum_amount');
     $current_user_role = wcj_get_current_user_first_role();
     foreach (wcj_get_user_roles() as $role_key => $role_data) {
         if ($role_key === $current_user_role) {
             $order_minimum_amount_by_user_role = get_option('wcj_order_minimum_amount_by_user_role_' . $role_key, 0);
             if ($order_minimum_amount_by_user_role > 0) {
                 $minimum = $order_minimum_amount_by_user_role;
             }
             break;
         }
     }
     return $minimum;
 }
 /**
  * available_payment_gateways.
  *
  * @version 2.5.3
  * @since   2.5.3
  */
 function available_payment_gateways($_available_gateways)
 {
     foreach ($_available_gateways as $key => $gateway) {
         $customer_role = wcj_get_current_user_first_role();
         $include_roles = get_option('wcj_gateways_user_roles_include_' . $key, '');
         if (!empty($include_roles) && !in_array($customer_role, $include_roles)) {
             unset($_available_gateways[$key]);
             continue;
         }
         $exclude_roles = get_option('wcj_gateways_user_roles_exclude_' . $key, '');
         if (!empty($exclude_roles) && in_array($customer_role, $exclude_roles)) {
             unset($_available_gateways[$key]);
             continue;
         }
     }
     return $_available_gateways;
 }
 /**
  * get_discount_by_quantity.
  *
  * @version 2.5.5
  */
 private function get_discount_by_quantity($quantity, $product_id)
 {
     // Check for user role options
     $role_option_name_addon = '';
     $user_roles = get_option('wcj_wholesale_price_by_user_role_roles', '');
     if (!empty($user_roles)) {
         $current_user_role = wcj_get_current_user_first_role();
         foreach ($user_roles as $user_role_key) {
             if ($current_user_role === $user_role_key) {
                 $role_option_name_addon = '_' . $user_role_key;
                 break;
             }
         }
     }
     // Get discount
     $max_qty_level = 1;
     $discount = 0;
     if (wcj_is_product_wholesale_enabled_per_product($product_id)) {
         for ($i = 1; $i <= apply_filters('booster_get_option', 1, get_post_meta($product_id, '_' . 'wcj_wholesale_price_levels_number' . $role_option_name_addon, true)); $i++) {
             $level_qty = get_post_meta($product_id, '_' . 'wcj_wholesale_price_level_min_qty' . $role_option_name_addon . '_' . $i, true);
             if ($quantity >= $level_qty && $level_qty >= $max_qty_level) {
                 $max_qty_level = $level_qty;
                 $discount = get_post_meta($product_id, '_' . 'wcj_wholesale_price_level_discount' . $role_option_name_addon . '_' . $i, true);
             }
         }
     } else {
         for ($i = 1; $i <= apply_filters('booster_get_option', 1, get_option('wcj_wholesale_price_levels_number' . $role_option_name_addon, 1)); $i++) {
             $level_qty = get_option('wcj_wholesale_price_level_min_qty' . $role_option_name_addon . '_' . $i, PHP_INT_MAX);
             if ($quantity >= $level_qty && $level_qty >= $max_qty_level) {
                 $max_qty_level = $level_qty;
                 $discount = get_option('wcj_wholesale_price_level_discount_percent' . $role_option_name_addon . '_' . $i, 0);
             }
         }
     }
     return $discount;
 }
 /**
  * get_variation_prices_hash.
  *
  * @version 2.5.3
  * @since   2.5.0
  */
 function get_variation_prices_hash($price_hash, $_product, $display)
 {
     $user_role = wcj_get_current_user_first_role();
     $koef = get_option('wcj_price_by_user_role_' . $user_role, 1);
     $is_empty = get_option('wcj_price_by_user_role_empty_price_' . $user_role, 'no');
     $price_hash['wcj_user_role'] = array($user_role, $koef, $is_empty, get_option('wcj_price_by_user_role_per_product_enabled', 'yes'));
     return $price_hash;
 }
 /**
  * get_current_currency_code.
  *
  * @version 2.5.5
  */
 function get_current_currency_code($default_currency = '')
 {
     if (isset($_SESSION['wcj-currency'])) {
         return $_SESSION['wcj-currency'];
     } else {
         $module_roles = get_option('wcj_multicurrency_role_defaults_roles', '');
         if (!empty($module_roles)) {
             $current_user_role = wcj_get_current_user_first_role();
             if (in_array($current_user_role, $module_roles)) {
                 $roles_default_currency = get_option('wcj_multicurrency_role_defaults_' . $current_user_role, '');
                 if ('' != $roles_default_currency) {
                     return $roles_default_currency;
                 }
             }
         }
     }
     return $default_currency;
 }
 /**
  * get_currency_selector.
  *
  * @version 2.5.5
  * @since   2.4.5
  */
 private function get_currency_selector($atts, $content, $type = 'select')
 {
     // Start
     $html = '';
     $form_method = $atts['form_method'];
     $class = $atts['class'];
     $style = $atts['style'];
     $html .= '<form action="" method="' . $form_method . '">';
     if ('select' === $type) {
         $html .= '<select name="wcj-currency" id="wcj-currency-select" style="' . $style . '" class="' . $class . '" onchange="this.form.submit()">';
     }
     $shortcode_currencies = $this->get_shortcode_currencies($atts);
     // Options
     $currencies = wcj_get_currencies_names_and_symbols();
     $selected_currency = '';
     if (isset($_SESSION['wcj-currency'])) {
         $selected_currency = $_SESSION['wcj-currency'];
     } else {
         $module_roles = get_option('wcj_multicurrency_role_defaults_roles', '');
         if (!empty($module_roles)) {
             $current_user_role = wcj_get_current_user_first_role();
             if (in_array($current_user_role, $module_roles)) {
                 $selected_currency = get_option('wcj_multicurrency_role_defaults_' . $current_user_role, '');
             }
         }
     }
     foreach ($shortcode_currencies as $currency_code) {
         if (isset($currencies[$currency_code])) {
             if ('' == $selected_currency) {
                 $selected_currency = $currency_code;
             }
             if ('select' === $type) {
                 $html .= '<option value="' . $currency_code . '" ' . selected($currency_code, $selected_currency, false) . '>' . $currencies[$currency_code] . '</option>';
             } elseif ('radio' === $type) {
                 $html .= '<input type="radio" name="wcj-currency" id="wcj-currency-radio" value="' . $currency_code . '" ' . checked($currency_code, $selected_currency, false) . ' onclick="this.form.submit()"> ' . $currencies[$currency_code] . '<br>';
             }
         }
     }
     // End
     if ('select' === $type) {
         $html .= '</select>';
     }
     $html .= '</form>';
     return $html;
 }
 /**
  * wcj_product_wholesale_price_table.
  *
  * @version 2.5.7
  */
 function wcj_product_wholesale_price_table($atts)
 {
     if (!wcj_is_product_wholesale_enabled($this->the_product->id)) {
         return '';
     }
     // Check for user role options
     $role_option_name_addon = '';
     $user_roles = get_option('wcj_wholesale_price_by_user_role_roles', '');
     if (!empty($user_roles)) {
         $current_user_role = wcj_get_current_user_first_role();
         foreach ($user_roles as $user_role_key) {
             if ($current_user_role === $user_role_key) {
                 $role_option_name_addon = '_' . $user_role_key;
                 break;
             }
         }
     }
     $wholesale_price_levels = array();
     if (wcj_is_product_wholesale_enabled_per_product($this->the_product->id)) {
         for ($i = 1; $i <= apply_filters('booster_get_option', 1, get_post_meta($this->the_product->id, '_' . 'wcj_wholesale_price_levels_number' . $role_option_name_addon, true)); $i++) {
             $level_qty = get_post_meta($this->the_product->id, '_' . 'wcj_wholesale_price_level_min_qty' . $role_option_name_addon . '_' . $i, true);
             $discount = get_post_meta($this->the_product->id, '_' . 'wcj_wholesale_price_level_discount' . $role_option_name_addon . '_' . $i, true);
             $wholesale_price_levels[] = array('quantity' => $level_qty, 'discount' => $discount);
         }
     } else {
         for ($i = 1; $i <= apply_filters('booster_get_option', 1, get_option('wcj_wholesale_price_levels_number' . $role_option_name_addon, 1)); $i++) {
             $level_qty = get_option('wcj_wholesale_price_level_min_qty' . $role_option_name_addon . '_' . $i, PHP_INT_MAX);
             $discount = get_option('wcj_wholesale_price_level_discount_percent' . $role_option_name_addon . '_' . $i, 0);
             $wholesale_price_levels[] = array('quantity' => $level_qty, 'discount' => $discount);
         }
     }
     $discount_type = wcj_is_product_wholesale_enabled_per_product($this->the_product->id) ? get_post_meta($this->the_product->id, '_' . 'wcj_wholesale_price_discount_type', true) : get_option('wcj_wholesale_price_discount_type', 'percent');
     $data_qty = array();
     $data_price = array();
     $data_discount = array();
     $columns_styles = array();
     $i = -1;
     foreach ($wholesale_price_levels as $wholesale_price_level) {
         $i++;
         if (0 == $wholesale_price_level['quantity'] && 'yes' === $atts['hide_if_zero_quantity']) {
             continue;
         }
         $the_price = '';
         if ($this->the_product->is_type('variable')) {
             // Variable
             $prices = $this->the_product->get_variation_prices(false);
             $min_key = key($prices['price']);
             end($prices['price']);
             $max_key = key($prices['price']);
             $min_product = wc_get_product($min_key);
             $max_product = wc_get_product($max_key);
             $get_price_method = 'get_price_' . get_option('woocommerce_tax_display_shop') . 'uding_tax';
             $min = $min_product->{$get_price_method}();
             $max = $max_product->{$get_price_method}();
             $min_original = $min;
             $max_original = $max;
             if ('fixed' === $discount_type) {
                 $min = $min - $wholesale_price_level['discount'];
                 $max = $max - $wholesale_price_level['discount'];
             } else {
                 $coefficient = 1.0 - $wholesale_price_level['discount'] / 100.0;
                 $min = $min * $coefficient;
                 $max = $max * $coefficient;
             }
             if ('yes' !== $atts['hide_currency']) {
                 $min = wc_price($min);
                 $max = wc_price($max);
                 $min_original = wc_price($min_original);
                 $max_original = wc_price($max_original);
             }
             $the_price = $min != $max ? sprintf('%s-%s', $min, $max) : $min;
             $the_price_original = $min_original != $max_original ? sprintf('%s-%s', $min_original, $max_original) : $min_original;
         } else {
             // Simple etc.
             $get_price_method = 'get_price_' . get_option('woocommerce_tax_display_shop') . 'uding_tax';
             $the_price = $this->the_product->{$get_price_method}();
             $the_price = apply_filters('wcj_product_wholesale_price_table_price_before', $the_price, $this->the_product);
             $the_price_original = $the_price;
             if ('price_directly' === $discount_type) {
                 $the_price = $wholesale_price_level['discount'];
             } elseif ('fixed' === $discount_type) {
                 $the_price = $the_price - $wholesale_price_level['discount'];
             } else {
                 // 'percent'
                 $coefficient = 1.0 - $wholesale_price_level['discount'] / 100.0;
                 $the_price = $the_price * $coefficient;
             }
             $the_price_original = apply_filters('wcj_product_wholesale_price_table_price_after', $the_price_original, $this->the_product);
             $the_price = apply_filters('wcj_product_wholesale_price_table_price_after', $the_price, $this->the_product);
             if ('yes' !== $atts['hide_currency']) {
                 $the_price = wc_price($the_price);
                 $the_price_original = wc_price($the_price_original);
             }
         }
         $level_max_qty = isset($wholesale_price_levels[$i + 1]['quantity']) ? $atts['before_level_max_qty'] . ($wholesale_price_levels[$i + 1]['quantity'] - 1) : $atts['last_level_max_qty'];
         $data_qty[] = str_replace(array('%level_qty%', '%level_min_qty%', '%level_max_qty%'), array($wholesale_price_level['quantity'], $wholesale_price_level['quantity'], $level_max_qty), $atts['heading_format']);
         if ('yes' === $atts['add_price_row']) {
             $data_price[] = str_replace(array('%old_price%', '%price%'), array($the_price_original, $the_price), $atts['price_row_format']);
         }
         if ('yes' === $atts['add_percent_row']) {
             if ('percent' === $discount_type) {
                 $data_discount[] = '-' . $wholesale_price_level['discount'] . '%';
             } else {
                 // 'fixed' or 'price_directly'
                 // todo (maybe)
             }
         }
         if ('yes' === $atts['add_discount_row']) {
             if ('fixed' === $discount_type) {
                 $data_discount[] = '-' . wc_price($wholesale_price_level['discount']);
             } else {
                 // 'percent' or 'price_directly'
                 // todo (maybe)
             }
         }
         $columns_styles[] = 'text-align: center;';
     }
     $table_rows = array($data_qty);
     if ('yes' === $atts['add_price_row']) {
         $table_rows[] = $data_price;
     }
     if ('yes' === $atts['add_percent_row']) {
         $table_rows[] = $data_discount;
     }
     if ('vertical' === $atts['table_format']) {
         $table_rows_modified = array();
         foreach ($table_rows as $row_number => $table_row) {
             foreach ($table_row as $column_number => $cell) {
                 $table_rows_modified[$column_number][$row_number] = $cell;
             }
         }
         $table_rows = $table_rows_modified;
     }
     return wcj_get_table_html($table_rows, array('table_class' => 'wcj_product_wholesale_price_table', 'columns_styles' => $columns_styles, 'table_heading_type' => $atts['table_format']));
 }