Round discounted prices?
-
I have a store with this plugin and we’d like the discounted prices to be a whole number / integer.
Right now a product that costs 1199 SEK with 20% discount becomes 959,20 SEK. We’d like it to be 959.
Could you please help us?
On 3.3.9 of the plugin, WordPress 4.9.5 and WooCommerce 3.3.3.
-
Hi,
You can change the woocommerce settings so show 0 decimal place in General settings.
Regards,
Thanks for the quick reply.
I’m aware of this and have tried it, but our payment gateway (Klarna) still uses the price given by WooCommerce.
We’d really appreciate if you could help us to make the actual prices rounded to nearest integer.
Hi,
Currently we don’t have a solution, but here’s a code snippet for rounding the value, its actually for another plugin, but give it a go, add it in functions.php
Code snippet:
add_filter(‘woocommerce_product_get_price’,’round_get_price’,200,2);
function round_get_price($price = ”, $product = null){
$obj = new Class_Eh_Price_Discount_Admin(false);
$pid = $obj->get_product_id($product);
$temp_data = $obj->get_product_type($product);
if ($temp_data == ‘variation’) {
$pid = $obj->get_product_parent_id($product);
}
if (is_array($obj->individual_product_adjustment_roles) && in_array($obj->current_user_role, $obj->individual_product_adjustment_roles)) {
//Role Based Price (individual product page price change)
$product_user_price = get_post_meta($pid, ‘product_role_based_price’);
if (is_array($product_user_price) && isset($product_user_price[0]) && !empty($product_user_price[0])) {
$product_user_price = $product_user_price[0];
}
if (!empty($product_user_price)) {
if (isset($product_user_price[$obj->current_user_role])) {
$product_user_price_value = $product_user_price[$obj->current_user_role][‘role_price’];
if (is_numeric($product_user_price_value)) {
$price = ceil($product_user_price_value);
}
}
}//individual product page price adjustment (discount/markup from settings page))
$enforce_button_check_for_product = get_post_meta($pid, ‘product_based_price_adjustment’, true);
$product_price_adjustment = get_post_meta($pid, ‘product_price_adjustment’, true);if ($enforce_button_check_for_product == ‘yes’ && isset($product_price_adjustment[$obj->current_user_role]) && isset($product_price_adjustment[$obj->current_user_role][‘role_price’]) && $product_price_adjustment[$obj->current_user_role][‘role_price’] == ‘on’) {
$current_user_product_rule = $product_price_adjustment[$obj->current_user_role];
if (!empty($current_user_product_rule[‘adjustment_price’]) && is_numeric($current_user_product_rule[‘adjustment_price’])) {
$adjustment_value += (float) $current_user_product_rule[‘adjustment_price’];
}
if (!empty($current_user_product_rule[‘adjustment_percent’]) && is_numeric($current_user_product_rule[‘adjustment_percent’])) {
$adjustment_value += $price * ((float) $current_user_product_rule[‘adjustment_percent’]) / 100;
}
//discount/markup ajustment to $price
$price += $adjustment_value;
return $price;
}
}
if ($temp_data === ‘variation’) {
$prdct_id = $obj->get_product_category_using_id($obj->get_product_parent_id($product));
}else {
if (WC()->version < ‘2.7.0’) {
$temp_post_id = $product->post->ID;
} else {
$temp_post_id = $product->get_id();
}
$prdct_id = $obj->get_product_category_using_id($temp_post_id);
}
$adjustment_value = 0;
$common_price_adjustment_table = get_option(‘eh_pricing_discount_price_adjustment_options’, array());
if (isset($common_price_adjustment_table[$obj->current_user_role]) && isset($common_price_adjustment_table[$obj->current_user_role][‘role_price’]) && $common_price_adjustment_table[$obj->current_user_role][‘role_price’] == ‘on’) {
$current_user_product_rule = $common_price_adjustment_table[$obj->current_user_role];
if (!empty($current_user_product_rule[‘adjustment_price’]) && is_numeric($current_user_product_rule[‘adjustment_price’])) {
if (isset($obj->role_price_adjustment[$obj->current_user_role][‘category’])) {
$cat_display = $obj->role_price_adjustment[$obj->current_user_role][‘category’];
if ($temp_data != ‘grouped’)
$result_chk = array_intersect($prdct_id, $cat_display);
if (empty($result_chk)) {
$adjustment_value = 0;
} else {
$adjustment_value += (float) $current_user_product_rule[‘adjustment_price’];
}
} else {
$adjustment_value += (float) $current_user_product_rule[‘adjustment_price’];
}
}
if (!empty($current_user_product_rule[‘adjustment_percent’]) && is_numeric($current_user_product_rule[‘adjustment_percent’])) {
if (isset($obj->role_price_adjustment[$obj->current_user_role][‘category’])) {
$cat_display = $obj->role_price_adjustment[$obj->current_user_role][‘category’];
if ($temp_data != ‘grouped’){
$result_chk = array_intersect($prdct_id, $cat_display);
}
if (empty($result_chk)) {
$adjustment_value = 0;
} else {
$adjustment_value += $price * ((float) $current_user_product_rule[‘adjustment_percent’]) / 100;
}
} else {
$adjustment_value += $price * ((float) $current_user_product_rule[‘adjustment_percent’]) / 100;
}
}
//discount/markup ajustment to $price
$price += $adjustment_value;
}
return $price;
}Thanks for helping me out.
Uncaught Error: Class ‘Class_Eh_Price_Discount_Admin’ not found in wp-content/themes/creamshop/functions.php:6
Stack trace:
#0 wp-includes/class-wp-hook.php(286): round_get_price(‘1099.00’, Object(WC_Product_Variable))
#1 wp-includes/plugin.php(203): WP_Hook->apply_filters(‘1099.00’, Array)
#2 wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-data.php(694): apply_filters(‘woocommerce_pro…’, ‘1099.00’, Object(WC_Product_Variable))
#3 wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-product.php(275): WC_Data->get_prop(‘price’, ‘view’)
#4 wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-product.php(1458): WC_Product->get_price()Hi,
I guess the snippet don’t work for this plugin, I’ll check with our team if they can write a code snippet to round the values. compatible with our plugin.
Regards,
Thank you so, so much!
Hi,
Please try using the below code snippet:
add_action(‘woocommerce_cart_total’, ‘cart_totals’, 99, 1);
function cart_totals($wc_price)
{
global $woocommerce;
$new_price = round($woocommerce->cart->total);return wc_price($new_price);
}
- The topic ‘Round discounted prices?’ is closed to new replies.