From 326bdd400f8edaba1ef20434dea3e3cfef1b3463 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Thu, 1 Dec 2011 11:34:26 +0000 Subject: [PATCH] Trim zeros off prices (optional) Closes #297. --- admin/admin-settings.php | 8 ++++++++ classes/checkout.class.php | 2 +- classes/countries.class.php | 2 +- readme.txt | 1 + woocommerce.php | 8 +++++++- 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/admin/admin-settings.php b/admin/admin-settings.php index 221a90a0a2a..4f64ccf6bcc 100644 --- a/admin/admin-settings.php +++ b/admin/admin-settings.php @@ -568,6 +568,14 @@ $woocommerce_settings['catalog'] = apply_filters('woocommerce_catalog_settings', 'type' => 'text', ), + array( + 'name' => __( 'Trim zeros', 'woothemes' ), + 'desc' => __( 'Trim zeros after the decimal point when displaying prices', 'woothemes' ), + 'id' => 'woocommerce_price_trim_zeros', + 'std' => 'yes', + 'type' => 'checkbox' + ), + array( 'type' => 'sectionend', 'id' => 'pricing_options' ), array( 'name' => __( 'Image Options', 'woothemes' ), 'type' => 'title','desc' => sprintf(__('These settings affect the actual dimensions of images in your catalog - the display on the front-end will still be affected by CSS styles. After changing these settings you may need to regenerate your thumbnails.', 'woothemes'), 'http://wordpress.org/extend/plugins/regenerate-thumbnails/'), 'id' => 'image_options' ), diff --git a/classes/checkout.class.php b/classes/checkout.class.php index 4d078271c62..f276822ebc7 100644 --- a/classes/checkout.class.php +++ b/classes/checkout.class.php @@ -700,7 +700,7 @@ class woocommerce_checkout { 'name' => $_product->get_title(), 'qty' => (int) $values['quantity'], 'base_cost' => $_product->get_price_excluding_tax( false ), - 'cost' => trim(trim(number_format($cost, 4, '.', ''), '0'), '.'), + 'cost' => rtrim(rtrim(number_format($cost, 4, '.', ''), '0'), '.'), 'taxrate' => $rate, 'item_meta' => $item_meta->meta ), $values); diff --git a/classes/countries.class.php b/classes/countries.class.php index e7a18e38553..f5b9ec3381f 100644 --- a/classes/countries.class.php +++ b/classes/countries.class.php @@ -596,7 +596,7 @@ class woocommerce_countries { global $woocommerce; if ( $rate > 0 || $rate === 0 ) : - $rate = trim(trim($rate, '0'), '.'); + $rate = rtrim(rtrim($rate, '0'), '.'); if (!$rate) $rate = 0; $return = ( in_array($this->get_base_country(), $this->get_european_union_countries()) ) ? sprintf(__('(inc. %s%% VAT)', 'woothemes'), $rate) : sprintf(__('(inc. %s%% tax)', 'woothemes'), $rate); else : diff --git a/readme.txt b/readme.txt index 1aa15eceac4..ba506ba0f60 100644 --- a/readme.txt +++ b/readme.txt @@ -114,6 +114,7 @@ Yes you can! Join in on our GitHub repository :) https://github.com/woothemes/wo * Free shipping/Flat rate uses setting API * Free shipping coupons * Ship to billing default option +* Trim zeros off prices (optional) = 1.2.4 - 18/11/2011 = * More sale price logic fixes for variations. Now correctly compares variation's prices. diff --git a/woocommerce.php b/woocommerce.php index 5c5dabb5829..e08bfa287ac 100644 --- a/woocommerce.php +++ b/woocommerce.php @@ -469,6 +469,12 @@ function woocommerce_price( $price, $args = array() ) { $currency_symbol = get_woocommerce_currency_symbol(); $price = number_format( (double) $price, $num_decimals, get_option('woocommerce_price_decimal_sep'), get_option('woocommerce_price_thousand_sep') ); + if (get_option('woocommerce_price_trim_zeros')=='yes') : + $trimmed_price = rtrim(rtrim($price, '0'), get_option('woocommerce_price_decimal_sep')); + $after_decimal = explode(get_option('woocommerce_price_decimal_sep'), $trimmed_price); + if (!isset($after_decimal[1]) || (isset($after_decimal[1]) && (strlen($after_decimal[1]) == 0 && strlen($after_decimal[1]) == $num_decimals))) $price = $trimmed_price; + endif; + switch ($currency_pos) : case 'left' : $return = $currency_symbol . $price; @@ -483,7 +489,7 @@ function woocommerce_price( $price, $args = array() ) { $return = $price . ' ' . $currency_symbol; break; endswitch; - + if ($ex_tax_label && get_option('woocommerce_calc_taxes')=='yes') $return .= ' '.$woocommerce->countries->ex_tax_or_vat().''; return $return;