0

Sorry for the vague title, but I'm using a PHP snippet on a WooCommerce shop which shows the difference between an old price and a new price (sale price) in a percentage bubble on the page. The thing is it shows also percentages with decimals behind a comma if it's not a round number. I would like to have it without the comma and decimals, so that it always shows a round number. How would I alter this in the below script? There are 2 variations in it by the way. One is for a single price and the other for a variable price (e.g. different sizes or colors of a product).

<?php if ($product->is_on_sale() && $product->product_type == 'variable') : ?> <div class="bubble"> <div class="inside"> <div class="inside-text"> <?php 
            $available_variations = $product->get_available_variations();                               
            $maximumper = 0;
            for ($i = 0; $i < count($available_variations); ++$i) {
                $variation_id=$available_variations[$i]['variation_id'];
                $variable_product1= new WC_Product_Variation( $variation_id );
                $regular_price = $variable_product1 ->regular_price;
                $sales_price = $variable_product1 ->sale_price;
                $percentage= round((( ( $regular_price - $sales_price ) / $regular_price ) * 100),1) ;
                    if ($percentage > $maximumper) {
                        $maximumper = $percentage;
                    }
                }
                echo $price . sprintf( __('%s', 'woocommerce' ), $maximumper . '%' ); ?></div> </div> </div><!-- end callout --> <?php elseif($product->is_on_sale() && $product->product_type == 'simple') : ?> <div class="bubble"> <div class="inside"> <div class="inside-text"> <?php 
                $percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
                echo $price . sprintf( __('%s', 'woocommerce' ), $percentage . '%' ); ?></div> </div> </div><!-- end bubble --> <?php endif; ?> 

Edited by gentlemedia

2
Contributors
5
Replies
19
Views
1 Year
Discussion Span
Last Post by gentlemedia
0

Erm, not sure what your issue is here.

round() without a precision parameter rounds to the closest integer.

0

I get also percentages like 33,3% and I would like to have it as 33%. I know the answer should be somewhere within the methods parseInt() or parseFloat(), but don't know how. I also tried to find a jQuery solution, but no luck so far.

Edited by gentlemedia

1

Where is js comming in here? ANyhow...

$r = (float) $variable_product1 ->regular_price;
$s = (float) $variable_product1 ->sale_price;
$discount = (( $r - $s ) / $r ) * 100;
$value = round($discount) . '%';

Doesn't work?

0

Yeah, sorry, I was confused :) ... those are of course jQuery methods. I will try your solution and see if it works!

0

Aight.... that did work indeed. I had double percentage symbols, so I have now this line instead $percentage = round($discount) . '';

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.