Rounding individual price
PHP v8.2.5
I have a couple of products within a category that are priced per hour, adding a '/hr' suffix to the price when using DPU is beyond me so I am adding the price/hour as a text field near the $products_name using this code
PHP Code:
<?php
if (($_GET['products_id'] == '631') || ($_GET['products_id'] == '633')) {
echo '<span class="priceHour">' . VOUCHER_PER_HOUR_PRE . zen_get_products_display_price((int)$_GET['products_id']) . VOUCHER_PER_HOUR_POST . '</span>';
}
?>
this displays as
(£45.00/hr)
I would like it do display to zero decimal places, but using this code
PHP Code:
<?php
if (($_GET['products_id'] == '631') || ($_GET['products_id'] == '633')) {
echo '<span class="priceHour">' . VOUCHER_PER_HOUR_PRE . round(zen_get_products_display_price((int)$_GET['products_id']), 0) . VOUCHER_PER_HOUR_POST . '</span>';
}
?>
gives a fatal error
Code:
[17-Sep-2023 14:12:46 Europe/London] PHP Fatal error: Uncaught TypeError: round(): Argument #1 ($num) must be of type int|float, string given in D:\wamp64\www\mydomain.co.uk\includes\templates\my_template\templates\tpl_product_info_display.php:86
how should I correctly structure the 'round' code to display as
(£45/hr)
Re: Rounding individual price
Try the following, since zen_get_products_display_price is returning that string.
Code:
<?php
if (($_GET['products_id'] == '631') || ($_GET['products_id'] == '633')) {
echo '<span class="priceHour">' . VOUCHER_PER_HOUR_PRE . round((float)zen_get_products_display_price((int)$_GET['products_id']), 0) . VOUCHER_PER_HOUR_POST . '</span>';
}
?>
Re: Rounding individual price
Quote:
Originally Posted by
lat9
Try the following, since zen_get_products_display_price is returning that string.
Code:
<?php
if (($_GET['products_id'] == '631') || ($_GET['products_id'] == '633')) {
echo '<span class="priceHour">' . VOUCHER_PER_HOUR_PRE . round((float)zen_get_products_display_price((int)$_GET['products_id']), 0) . VOUCHER_PER_HOUR_POST . '</span>';
}
?>
Alas, that gives me
(0)
Not relevant to your code change but I meant to say that, in order to have the '/hr' display on that categories' Product Listing page - for those two products only, I had modified the zen_get_products_display_price function to include
PHP Code:
//simon1066: added /hr to individual gift vouchers
} else if (($product_check->fields['products_id'] == 631 || $product_check->fields['products_id'] == 633) && ($_GET['cPath'] == '66')) {
$show_normal_price = '<span class="productBasePrice">';
$show_normal_price .= $currencies->display_price($display_normal_price, zen_get_tax_rate($product_check->fields['products_tax_class_id']));
$show_normal_price .= '/hr</span>';
Re: Rounding individual price
The function 'zen_get_products_display_price' returns a product's price that includes the currency symbol (plus the custom /hr suffix I added into the function) so the price is a string and not a numeric value. I used 'str_replace' to remove the unwanted portion of the price.
Re: Rounding individual price
You could use zen_get_products_actual_price((int)$_GET['products_id']) instead of fussing with removing various character strings.
Re: Rounding individual price
Quote:
Originally Posted by
lat9
You could use zen_get_products_actual_price((int)$_GET['products_id']) instead of fussing with removing various character strings.
Yes, that's much better, thank you!