We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 15599
    • 27 Posts
    Hello,
    I’m trying to have a template variable with the retail price and a template variable with the slashed price; is it possible to do some math on these numbers to find the percent discounted?
      • 3749
      • 24,544 Posts
      PHX has a math: modifier that I think would do it, but it would be much faster to just send the two TVs as parameters to a snippet and have the snippet do the math.

      Something like this (untested):

      Your discount is [[Discount? &retailPrice=`[*retailTV*]` &salePrice=`[*saleTV*]`]]%<br />


      In the Discount snippet:

      <?php
      $diff = $retailPrice - $salePrice;
      $discount = $diff/$retailPrice;
      return($discount * 100);
      ?>
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 53098
        • 2 Posts
        This works perfectly for what I'm looking for. But how can I force it to round to nearest percent?
        i'm getting 24.057738572574% when i need 24%

        Thanks!
          • 54715
          • 1 Posts
          You can modify Bob's example to include the PHP round() function:

          <?php
          $diff = $retailPrice - $salePrice;
          $discount = $diff/$retailPrice;
          return(round($discount * 100));
          ?>
          


          PHP documentation here: https://www.w3schools.com/php/func_math_round.asp