Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2009
    Location
    UK
    Posts
    1,159
    Plugin Contributions
    1

    Default 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)
    Simon

  2. #2
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    11,924
    Plugin Contributions
    86

    Default 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>';
    }
    ?>

  3. #3
    Join Date
    Feb 2009
    Location
    UK
    Posts
    1,159
    Plugin Contributions
    1

    Default Re: Rounding individual price

    Quote Originally Posted by lat9 View Post
    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_pricezen_get_tax_rate($product_check->fields['products_tax_class_id']));
                        
    $show_normal_price .= '/hr</span>'
    Simon

  4. #4
    Join Date
    Feb 2009
    Location
    UK
    Posts
    1,159
    Plugin Contributions
    1

    Default 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.
    Simon

  5. #5
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    11,924
    Plugin Contributions
    86

    Default Re: Rounding individual price

    You could use zen_get_products_actual_price((int)$_GET['products_id']) instead of fussing with removing various character strings.

  6. #6
    Join Date
    Feb 2009
    Location
    UK
    Posts
    1,159
    Plugin Contributions
    1

    Default Re: Rounding individual price

    Quote Originally Posted by lat9 View Post
    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!
    Simon

 

 

Similar Threads

  1. Replies: 0
    Last Post: 12 Apr 2015, 10:31 PM
  2. v151 Price Rounding
    By rebel tech in forum General Questions
    Replies: 4
    Last Post: 22 Jan 2013, 06:23 PM
  3. v151 Price rounding
    By rebel tech in forum Currencies & Sales Taxes, VAT, GST, etc.
    Replies: 1
    Last Post: 2 Dec 2012, 05:27 PM
  4. Price rounding up
    By BillNally in forum General Questions
    Replies: 7
    Last Post: 30 Mar 2011, 08:04 PM
  5. Rounding price
    By tatiana77 in forum Setting Up Categories, Products, Attributes
    Replies: 1
    Last Post: 24 Nov 2007, 06:12 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR