We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30876 ☆ A M B ☆
    • 39 Posts
    I have inherited a MODx Evo 1.0.4 site which is producing an error in the following snippet.
    I am trying to get the MODx resource tag [*pageTitle*] values into the pagetitleTV in lines 11 and 19 .
    (I'm still a bit of a novice and would appreciate any assistance to either adjust the snippet code, create the Template Variable or do it in a more elegant way.

    <?php
    // [[orderDetailsInToSESSION]]
     
    if ( empty($_GET['customer']) ) return "no customer account specified";
    else {
        // get the details from the orderId
        $db_query = $modx->db->select("productId, quantity", $modx->getFullTableName('customer_order_details'), "orderId='" . $_GET['orderId'] . "'");
        if ($modx->db->getRecordCount($db_query) > 0) {
            while ($row = $modx->db->getRow($db_query)) {
                // get the current prices and names of the products previously ordered
                $document_tvs = $modx->getTemplateVars(array("pagetitle", "price", "specialPrice"), "name", $row['productId']);
                // to put the returned array into a simpler/easier to use array use the following
                foreach ($document_tvs as $document_TV) {
                        $docTVArray[$document_TV['name']] = $document_TV['value'];
                }
                // put these details into the session
               $_SESSION['shoppingCartContents'][] = Array (
                   'productId' => $row['productId'],
                   'productTitle' => $docTVArray['pagetitle'],
                   'price' => ( !empty($docTVArray['specialPrice']) ) ? $docTVArray['specialPrice'] : $docTVArray['price'],
                   'quantity' => $row['quantity']
               );
            }
        }
     
    }
    ?>

    This question has been answered by sottwell. See the first response.

      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You can't treat resource fields in the same way you do TVs. While they are in purpose extended resource fields, they are in actuality quite different. I've never been happy that TVs were given the same tags as resource fields, it leads to a lot of confusion. You'll have to get the pagetitle of the page in question (I presume the resource ID is the productId?) and add it to your array.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 30876 ☆ A M B ☆
        • 39 Posts
        Hi Susan
        Thank you for your feedback. I can attest to experiencing some of that confusion as I am a PHP/MODx novice and am at a complete loss as to what to do to fix the foreach error on line 13.
        I have floundered around trying all manner of permutations but the foreach code block (see the updated version below) still keeps throwing an error.
        I have created a new TV called productName and manually copied the product names into each product record. This appears to work and produce the values that are expected once I move past the foreach block.
        Can you suggest (or better, provide an example) of how to get this script working?
        Be a wonderful Christmas present smiley if you can.

        Updated Code -
        <?php
        // [[orderDetailsInToSESSION]]
          
        if ( empty($_GET['customer']) ) return "no customer account specified";
        else {
            // get the details from the orderId
            $db_query = $modx->db->select("productId, quantity", $modx->getFullTableName('customer_order_details'), "orderId='" . $_GET['orderId'] . "'");
            if ($modx->db->getRecordCount($db_query) > 0) {
                while ($row = $modx->db->getRow($db_query)) {
                    // get the current prices and names of the products previously ordered
                    $document_tvs = $modx->getTemplateVars(array("productName", "price", "specialPrice"), "name", $row['productId']);
                    // to put the returned array into a simpler/easier to use array use the following
                   foreach ($document_tvs as $document_TV) {
                           $docTVArray[$document_TV['name']] = $document_TV['value'];
                    }
                    // put these details into the session
                   $_SESSION['shoppingCartContents'][] = Array (
                       'productId' => $row['productId'],
                       'productTitle' => $docTVArray['productName'],
                       'price' => ( !empty($docTVArray['specialPrice']) ) ? $docTVArray['specialPrice'] : $docTVArray['price'],
                       'quantity' => $row['quantity']
                   );
                }
            }
        }
        ?>
        • discuss.answer
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Ok, it's been a while since I've done any serious Evo coding; getTemplateVars does fetch resource fields as well as TVs. This should work, if your pagetitle is the product name...and once you see a nice list of what you are expecting to see, then you can go ahead and load the values into the SESSION.
          <?php
          // [[orderDetailsInToSESSION]]
          if ( empty($_GET['customer']) ) return "no customer account specified";
          else {
              // please sanitize and preferably validate the input type to make sure it's what you are expecting; never, ever trust "foreign" input around your database - you could get an SQL injection exploit
              $orderId = $modx->db->escape($_GET['customer']);
              // get the details from the orderId
              $db_query = $modx->db->select("productId, quantity", $modx->getFullTableName('customer_order_details'), "orderId='" . $orderId . "'");
              if ($modx->db->getRecordCount($db_query) > 0) {
                  while ($row = $modx->db->getRow($db_query)) {
                      // get the current prices and names of the products previously ordered
                      $document_tvs = $modx->getTemplateVars(array("pagetitle", "price", "specialPrice"), "name", $row['productId']);
                      echo "<pre>";
                      print_r($document_tvs);
                      echo "</pre>";
                  }
              }
          }
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 30876 ☆ A M B ☆
            • 39 Posts
            Hi Susan
            Thank you, this almost worked. Once I changed line 6 to the following I got the array as you predicted -
            $customer = $modx->db->escape($_GET['customer']);
            $orderId = $modx->db->escape($_GET['orderId']);
            

            I still am getting errors with the foreach statement but will open a new post for that.
            Many thanks Stephen