function wcj_is_product_wholesale_enabled($product_id) { if (wcj_is_module_enabled('wholesale_price')) { if (wcj_is_product_wholesale_enabled_per_product($product_id)) { return true; } else { $products_to_include_passed = false; $products_to_include = get_option('wcj_wholesale_price_products_to_include', array()); if (empty($products_to_include)) { $products_to_include_passed = true; } else { foreach ($products_to_include as $id) { if ($product_id == $id) { $products_to_include_passed = true; } } } $products_to_exclude_passed = false; $products_to_exclude = get_option('wcj_wholesale_price_products_to_exclude', array()); if (empty($products_to_exclude)) { $products_to_exclude_passed = true; } else { foreach ($products_to_exclude as $id) { if ($product_id == $id) { $products_to_exclude_passed = false; } } } return $products_to_include_passed && $products_to_exclude_passed; } } return false; }
/** * get_wholesale_price. * * @version 2.5.7 */ private function get_wholesale_price($price, $quantity, $product_id) { $discount = $this->get_discount_by_quantity($quantity, $product_id); $discount_type = wcj_is_product_wholesale_enabled_per_product($product_id) ? get_post_meta($product_id, '_' . 'wcj_wholesale_price_discount_type', true) : get_option('wcj_wholesale_price_discount_type', 'percent'); if ('price_directly' === $discount_type) { return 0 != $discount ? apply_filters('wcj_get_wholesale_price', $discount, $product_id) : $price; } elseif ('percent' === $discount_type) { return $price * (1.0 - $discount / 100.0); } else { $discounted_price = $price - $discount; return $discounted_price >= 0 ? $discounted_price : 0; } }
/** * 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'])); }