/** * wholesale_price. */ function wholesale_price($price, $product) { if (!wcj_is_product_wholesale_enabled($product->id)) { return $price; } // Show only on checkout and cart pages $is_ajax = is_admin() && (defined('DOING_AJAX') && DOING_AJAX) ? true : false; if (!(is_checkout() || is_cart() || $is_ajax)) { return $price; } // Get quanitity from cart $quanitities = array(); $the_cart = WC()->cart->get_cart(); foreach ($the_cart as $cart_item_key => $values) { if (!isset($quanitities[$values['product_id']])) { $quanitities[$values['product_id']] = 0; } $quanitities[$values['product_id']] += $values['quantity']; } foreach ($quanitities as $product_id => $product_quantity) { if ($product_id == $product->id) { // Found matching product in the cart if ($product_quantity > 1) { // Setting wholesale price $price = $this->get_wholesale_price($price, $product_quantity); $precision = get_option('woocommerce_price_num_decimals', 2); return round($price, $precision); } } } return $price; }
/** * wcj_product_wholesale_price_table. */ function wcj_product_wholesale_price_table($atts) { if (!wcj_is_product_wholesale_enabled($this->the_product->id)) { return ''; } $wholesale_price_levels = array(); for ($i = 1; $i <= apply_filters('wcj_get_option_filter', 1, get_option('wcj_wholesale_price_levels_number', 1)); $i++) { $level_qty = get_option('wcj_wholesale_price_level_min_qty_' . $i, PHP_INT_MAX); $discount_percent = get_option('wcj_wholesale_price_level_discount_percent_' . $i, 0); $discount_koef = 1.0 - $discount_percent / 100.0; $wholesale_price_levels[] = array('quantity' => $level_qty, 'koef' => $discount_koef, 'discount_percent' => $discount_percent); } $data_qty = array(); $data_price = array(); foreach ($wholesale_price_levels as $wholesale_price_level) { $the_price = ''; // Variable if ($this->the_product->is_type('variable')) { $min = $this->the_product->get_variation_price('min', false); $max = $this->the_product->get_variation_price('max', false); if ('' !== $wholesale_price_level['koef'] && is_numeric($wholesale_price_level['koef'])) { $min = $min * $wholesale_price_level['koef']; $max = $max * $wholesale_price_level['koef']; } if ('yes' !== $atts['hide_currency']) { $min = wc_price($min); $max = wc_price($max); } $the_price = sprintf('%s-%s', $min, $max); } else { //$the_price = wc_price( round( $this->the_product->get_price() * $wholesale_price_level['koef'], $precision ) ); $the_price = $this->the_product->get_price(); if ('' !== $wholesale_price_level['koef'] && is_numeric($wholesale_price_level['koef'])) { $the_price = $the_price * $wholesale_price_level['koef']; } if ('yes' !== $atts['hide_currency']) { $the_price = wc_price($the_price); } } $data_qty[] = str_replace('%level_qty%', $wholesale_price_level['quantity'], $atts['heading_format']); $data_price[] = $the_price; if ('yes' === $atts['add_percent_row']) { $data_discount_percent[] = '-' . $wholesale_price_level['discount_percent'] . '%'; } } $table_rows = array($data_qty, $data_price); if ('yes' === $atts['add_percent_row']) { $table_rows[] = $data_discount_percent; } $table_styles = array('columns_styles' => array('text-align: center;', 'text-align: center;', 'text-align: center;')); return wcj_get_table_html($table_rows, $table_styles); }
/** * wholesale_price. * * @version 2.5.0 */ function wholesale_price($price, $_product) { return wcj_is_product_wholesale_enabled($_product->id) && isset($_product->wcj_wholesale_price) ? $_product->wcj_wholesale_price : $price; }
/** * wholesale_price. * * @version 2.2.6 */ function wholesale_price($price, $_product) { if (!wcj_is_product_wholesale_enabled($_product->id)) { return $price; } // Show only on checkout and cart pages //$is_ajax = ( is_admin() && ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) ? true : false; if (!(is_checkout() || is_cart())) { return $price; } // If other discount was applied in cart... if ('yes' === get_option('wcj_wholesale_price_apply_only_if_no_other_discounts', 'no')) { if (WC()->cart->get_total_discount() > 0 || sizeof(WC()->cart->applied_coupons) > 0) { return $price; } } // Maybe set wholesale price $the_quantity = $this->get_wholesale_quantity($_product); if ($the_quantity > 1) { $wholesale_price = $this->get_wholesale_price($price, $the_quantity); if ($wholesale_price != $price) { // Setting wholesale price $precision = get_option('woocommerce_price_num_decimals', 2); return round($wholesale_price, $precision); } } // No changes to the price return $price; }
/** * 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'])); }