We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21417
    • 486 Posts
    Hey again coroico,

    Thanks so much.

    The adjusted GetCategoryLink works well now for individual products - but not for their parents or other non product pages, searching for those throws a parse error:
    `` is not numeric and may not be passed to makeUrl() »


    Rather to add a TV in each product may be it could be better to retreive the category document by an another way. What are the difference between the category product document and a product document ? May be the category product document use a specific template which is not the same used by the children documents ?

    OK great - not using a TV sounds ace to me. All children products use template id 7 (product items) or id 9 (product sub category) - all the top level parent documents use template id 8 (this is the template where the info is displayed). Other pages of the site use different templates so I think you have worked out the best way to do it. If

    I hope this will work with the tree as its pretty deep and some products have sub sub sub categories and are displayed on their top parent pages - template id numbers are shown next to the link names to illustrate which templates are used where:

    Home  (1)
    Products (10)
    -Brand (10)
    --Category (8)
    ---Sub Category (9)
    ----Sub Sub Category (9)
    -----item (7)
    -----item (7)
    -----item (7)
    ---Sub Category (9)
    ----item (7)
    ----item (7)
    --Category (8)
    ---item (7)
    ---item (7)
    ---item (7)
    etc
    
    where template id #:
    7 = product items (no individual page content)
    9 = product groups sub category (no individual page content)
    8 = product parent (where the info is viewable and displayed on page)
    


    If it is the case, by modifying the GetCategoryLink snippet, starting from any document, we could search a document in the upper hierarchy with this template number. If this document is found, the snippet return the link to the category otherwise run as usual and return the link to the current document. Is it possible to do this distinction between documents ?

    Absolutely, I think this will be the ultimate solution!

    Thanks so much coroico.
      Web design Adelaide
      http://gocreate.com.au
      • 5811
      • 1,717 Posts
      Hereafter is the new version of the GetCategoryLink snippet:
      <?php
      /*
       * Get the link to a category product
       *
       * &tplid : the template id of the category product
       *
       * &id : any document id
       *
       * Explore the parents of a document to search the category product
       * if the category product is found, return the link to this document
       * otherwise return the link to the current document
       *
       */
      
       if(!function_exists('findIt')) {
           function findIt($cid, $tplid, $id) {
           global $modx;
              $parent = $modx->getParent($cid,1,'id, template'); // get parent infos
              if ($parent) {
                  if ($parent['template'] == $tplid) $ctgid = $parent['id'];  // eureka!
                  else $ctgid = findIt($parent['id'], $tplid, $id); // go up!
              }
              else $ctgid = $id; // parent not found
              return $ctgid;
          }
      }
      
      $tplid = (isset($tplid)) ? $tplid : 0;
      $id = (isset($id)) ? $id : $modx->documentIdentifier;
      $ctgid = findIt($id,$tplid,$id);
      $url = $modx->makeUrl($ctgid);
      echo $url;
      ?>
      For a document if a parent document has a template with a specific number, then the link to this parent document is returned otherwise the link to the current document is provided.

      In your result template you need to call it as follow:
      <a class="[+as.resultLinkClass+]" href="[[GetCategoryLink? &tplid=`8` &id=`[+as.id+]`]]" title="[+as.longtitle+]">[+as.pagetitle+]</a> 
      Where &tplid is the template number of your category document.

      What kind of beer do you drink?
      In front of the gulf St Vincent, any beer grin
        • 21417
        • 486 Posts
        yaaaaahhhhhhhooooooooooo!

        You beauty coroico, that works perfectly across the whole site.

        Thank you so very, very much for your persistence and your extraordinary programming.

        I am so happy.

        In front of the gulf St Vincent, any beer
        That’s a fair call!

        Check your PM - It’s beer o’clock!
          Web design Adelaide
          http://gocreate.com.au