• Resolved muszkin

    (@muszkin)


    Hello,
    I have little problem. I want to make a progress bar for free shiping in shop based on woocommerce plugin. I manage to get how much money is in cart ,but if statement doesn’t work.

    code below:

    <?php
    	  global $woocommerce;
    	  $total = $woocommerce->cart->get_cart_total();
    	  //$cartnumber = str_replace(".00 zł", "", $total);
    	  //$cartnumber = substr('$total',-6,0);
    	  $cartnumber = $total;
    	  $cartnumber = substr($cartnumber, 0, strpos($cartnumber,'.'));
    	  if ($cartnumber >= 1000) {
    		$prog = 100;
    		echo do_shortcode('[wppb progress='.$prog.'/100 option=red text="Darmowa dostawa !"]');
    		}
    		else
    		{
    		$brak = 1000 - $cartnumber ;
    		$prog = $cartnumber / 10 ;
    		echo do_shortcode('[wppb progress='.$prog.'/100 option=red text="Brakuje '.$brak.'zł do darmowej dostawy."]');
    		}
    	  printf('<h2>%1$s</h2>',                        __('W koszyku masz towar za '.$cartnumber.' !')
    	  )
    	  ?>

    its look like $brak have always 1000 value ,because php cant subtraction “1000 – $cartnumber” .

    Anyone can help ?

Viewing 7 replies - 1 through 7 (of 7 total)
  • I think your problem is that you are trying to do numerical evaluations on $cartnumber, which is actually a string. You probably need to convert $cartnumber to an actual number first like:

    $cartnumber = "999.99";
    $int = (int)$num;
    $float = (float)$num;

    You might want to convert it to a float for decimal places.

    Thread Starter muszkin

    (@muszkin)

    Hello ,
    I did what you write in your post but this seems to not work too.

    <?php
    	  global $woocommerce;
    	  $total = $woocommerce->cart->get_cart_total();
    	  $cartnumber = $total;
    	  $cartnumber = substr($cartnumber, 0, strpos($cartnumber,'.'));
    	  $flota = (int)$cartnumber;
    	  if ($flota >= 1000) {
    		$prog = 100;
    		echo do_shortcode('[wppb progress='.$prog.'/100 option=red text="Darmowa dostawa !"]');
    		}
    		else
    		{
    		$brak = 1000 - $flota ;
    		$prog = $flota / 10 ;
    		echo do_shortcode('[wppb progress='.$prog.'/100 option=red text="Brakuje '.$brak.'zł do darmowej dostawy."]');
    		}
    	  printf('<h2>%1$s</h2>',                        __('W koszyku masz towar za '.$flota.' !')
    	  )
    	  ?>

    in var $flota is still 0 .

    What value does $cartnumber have right before the if statement?
    What value is in $total at the same point?

    Hi,
    I think you are trying to truncate the digits after the decimal. Instead of using the substr() in $cartnumber = substr($cartnumber, 0, strpos($cartnumber,'.'));, you may use $cartnumber=floor($cartnumber).
    Hope that works.

    Thread Starter muszkin

    (@muszkin)

    substr() need to stay ,because $cartnumber not only contains .00 but words too. If i dont use substr this is what i get in $cartnumber

    echo ($cartnumber) // “543.00 zł”

    What if you removed all characters besides digits and periods first.

    $cartnumber = preg_replace(“/[^0-9\.]/”, “”, $cartnumber);
    Then
    $cartnumber = floor($cartnumber);

    Thread Starter muszkin

    (@muszkin)

    it works ! ,thank you very much.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to make "if else" work’ is closed to new replies.