We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45118
    • 123 Posts
    How can I create a product detail page without creating a resource for every product?

    I have a product page with a list of many products. Every product has a Number, an Image, a Description, etc, all this data resides in a database. I can sort the products by various dimensions and choose a subset, this all works fine.

    Every product will get a link to a page with details on the specific product and I need to make these detail pages for every product. Can I use one resource to create all the pages by using placeholders?

    I'm not sure where to start... Can anyone point me in the right direction?

    Thank you very much!

    This question has been answered by multiple community members. See the first response.

    • discuss.answer
      • 3749
      • 24,544 Posts
      Yes, that's definitely the way to do it.

      Is the database with the data set up for access with xPDO?

      If so, it's as simple as passing the product ID in the URL, creating a Tpl chunk with placeholders for the data, and doing something like this:

      $tplName = "ProductDetailTpl";
      $productId = $_GET['productId'];
      
      $product = $modx->getObject('modProduct', $productId);
      if ($product) {
          $fields = $product->toArray();
          $output = $modx->getChunk($tplName, $fields);
      } else {
         $output ='Product not found';
      }
      return $output;
      

        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
        • 45118
        • 123 Posts
        Thanks very much Bob!
        I don't have it working yet, this is how far I got...

        In the Tpl chunk for the product page I gave the image a link to the product detail page...
        <a href="[[~177? &[[+itemno]]=`~177`]]"><img src="[[+imgLink]]" /></a>

        It generates a url looking like this: products-detail.html?123456=%7E177 where the itemno of the product is 123456. I think that's correct, but I'm not sure...

        The Chunk with the placeholders for the data is ShowProdDetail and this is the Snippet, that gets called in the resource (ID 177):
        <?php
        $path = MODX_CORE_PATH . 'components/aeiproducts/';
        $result = $modx->addPackage('aeiproducts',$path .
            'model/','aei_');
        
        if (! $result) {
            return 'failed to add package';
        }
        
        $productId = $_GET['number'];
        
        $product = $modx->getObject('Products', $productId);
        if ($product) {
            $fields = $product->toArray();
            $output = $modx->getChunk('ShowProdDetail', $fields);
        } else {
           $output ='Product not found';
        }
        return $output;


        It returns Product not found and I wonder if I have to add newQuery or something like that.

        Thanks in advance for all your help!
        • discuss.answer
          • 3749
          • 24,544 Posts
          I think you're close. Your snippet looks good, but I believe you want something like this in the Tpl chunk:

          <a href="[[~177? &number=`[[+itemno]]`]]"><img src="[[+imgLink]]" /></a>


          You want a URL that looks like this:

          products-detail.html? number=123456
            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
            • 45118
            • 123 Posts
            Quote from: BobRay at Dec 02, 2014, 04:47 PM
            I think you're close. Your snippet looks good, but I believe you want something like this in the Tpl chunk:

            <a href="[[~177? &number=`[[+itemno]]`]]"><img src="[[+imgLink]]"></a>


            You want a URL that looks like this:

            products-detail.html? number=123456

            This works beautifully!! Thank you VERY much!!
              • 3749
              • 24,544 Posts
              I'm glad I could help. smiley
                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