3

Есть магазин с товарами на wordpress+woocommerce. Пробую посчитать скидку на товар при распродаже, но что-то не получается - на выходе получаю формат 1.35а не 1.350 руб..Что делаю не так?

<p class="economy">
  <?php 
   function extract_unit($string, $start, $end)
   {
      $pos = stripos($string, $start);
      $str = substr($string, $pos);
      $str_two = substr($str, strlen($start));
      $second_pos = stripos($str_two, $end);
      $str_three = substr($str_two, 0, $second_pos);
      $unit = trim($str_three); // remove whitespaces
      return $unit;
    }
   global $product; 
   $price=$product->get_price_html(); 
   $saleprice = extract_unit($price,'<ins><span class="amount">','<ruble title="руб.">a</ruble></span></ins>');
   $regprice = extract_unit($price,'<del><span class="amount">','<ruble title="руб.">a</ruble></span></del>');     
   $economy= $regprice - $saleprice;
   echo $economy." <ruble title='руб.'>a</ruble>"; ?>
</p>

echo $product->get_price_html(); 

выводится в таком виде:

<p class="price">
  <del><span class="amount">21.150&nbsp;<ruble title="руб.">a</ruble>
  </span></del>
  <ins><span class="amount">19.800&nbsp;<ruble title="руб.">a</ruble>
  </span></ins>
</p>
0

1 ответ 1

2

Используйте sprintf:

Вместо echo $economy." <ruble title='руб.'>a</ruble>";

Попробуйте echo sprintf("%.3f"." <ruble title='руб.'>a</ruble>", $economy);

$number = 1234.56; 

// английский формат (по умолчанию)
$english_format_number = number_format($number);
// 1,234

// французский формат
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56

$number = 1234.5678;
// английский формат без разделителей групп
$english_format_number = number_format($number, 2, '.', '');
// 1234.57

Источник: php.net

5
  • получается так: 1350.000&nbsp; это совсем не то, что мне нужно - точка должна разделять тысячи, а не дробить на копейки, т.е. нужно получит так 1.350&nbsp;
    – Вася
    24 дек 2015 в 7:14
  • 1
    ну, тогда можно попробовать number_format
    – LamerXaKer
    24 дек 2015 в 7:33
  • это как записать - поясните плиз
    – Вася
    24 дек 2015 в 7:36
  • пишу так -number_format($economy,2,'','.'); ничего не выходит
    – Вася
    24 дек 2015 в 7:42
  • все, разобрался - надо так $number = number_format($economy,0,'','.'); echo $number." <ruble title='руб.'>a</ruble>";
    – Вася
    24 дек 2015 в 7:44

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.