We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 12379
    • 460 Posts
    No programming experience here but I need to do a snippet that adds a value in a chunk to a TV and displays the output as a value.

    Was thinking along the lines of:

    <?php
    $item=[*itemPrice*]
    $plan={{planPrice}}
    $total=$item + $plan
    print($total)
    ?>

    If I can’t use a chunk in a snippet, then perhaps i could create a TV and bind it to the chunk and add that to the other TV?
      Mostly harmless.
      • 26903
      • 1,336 Posts
      You could try :-

      <?php
      global $modx;
      $item=[*itemPrice*]
      $plan=$modx->getChunk(’planprice’);
      $total=$item + $plan
      print($total)
      ?>
        Use MODx, or the cat gets it!
        • 12379
        • 460 Posts
        Thanks. So far i’ve been able to get this to parse:

        <?php
        global $modx;
        $item=’[*itemPrice*]’;
        $plan=$modx->getChunk(’planPrice’);
        $total=$item + $plan;
        return $total;
        ?>

        It’s getting the value form the chunk ok, but not able to get the TV value.
          Mostly harmless.
          • 26903
          • 1,336 Posts
          OK sorry, didn’t see the template variable there, you cant do this here, as far as I know there is no getTV member which will work in the same way as the getChunk.

          You could get the template variable value from the ’site_tmplvars_contentvalues’ table but you’d need to write a db call along the lines of
          ’SELECT value FROM site_tmplvar_contentvalues WHERE tmplvarid = <id>’

          substituting <id> for the db id of the itemPrice template variable.

          I suspect there are other easier ways of doing what your trying to do, MODx is very flexible in this respect.
            Use MODx, or the cat gets it!
            • 12379
            • 460 Posts
            thanks, but I’m thinking I’m going to need to add parameters to it now so I can reuse the snippet for adding other chunks and TVs.
            Something like this which doesn’t work. Thought the parameter might return the TV value for the snippet to use. Guess this thread needs to be in the snippet development forum now!

            <?php
            // AddThis
            // Adds a chunk value to a TV value
            // Usage: [[AddThis? &TV=`[*itemPrice*]` &chunkname=`planPrice`]]

            global $modx;
            $chunkvalue=$modx->getChunk(’$chunkname’);
            $total=$TV + $chunkvalue;
            return $total;
            ?>
              Mostly harmless.
              • 4310
              • 2,310 Posts
              If the value of the chunk is a number why not retrieve it from the database and use it in your claculation?
                • 12379
                • 460 Posts
                getting the value of the chunk isn’t the problem. it’s getting the TV value in there (although neither appears to work with latest snippet).

                i think this way will be easier for the client. i need to add several ’planprices’ to a single ’itemprice’ throughout the template, so this snippet will need to be used several times on one product page, for 30 or more products...
                  Mostly harmless.
                  • 26903
                  • 1,336 Posts
                  OK, I think the best thing to do is get the basic function working first, then worry about the parameters, try this snippet :-

                  <?php
                  global $modx;
                  //itemPrice TV id is 5
                  $tablename = $modx->getFullTableName(’site_tmplvar_contentvalues’);
                  $sql = "SELECT value FROM $tablename WHERE tmplvarid = 5";
                  $result = $modx->db->query($sql);
                  $row = @mysql_fetch_row($result);
                  $item = $row[0];
                  $plan=$modx->getChunk(’planPrice’);
                  $total=$item + $plan;
                  return $total;
                  ?>

                  Substitute the id of your template variable for the number 5 here, I;ve just done a test page on my dev system and this seems to work Ok.

                  You can parametrise this easily enough by passing the name of the chunk and the id of the template variable.

                  There are a number of getTemplateVar.... members on the document API, I’ve never used them so I dont know if they will help, but it might be better to look at these rather than go straight to the database.

                    Use MODx, or the cat gets it!