We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 50574
    • 70 Posts
    To set the scene...
    I have some product resources and these products have checkbox options that when selected will populate a <ul> on the product page. The checkbox options are populated by pulling in the names of the child resources of a particular category in the resource list. I also need the child resources to then show which products have their option selected.

    For example, I have three resources called Option1, Option2 & Option3. Product1 resource shows a checkbox list labelled Option1, Option2 & Option3. If I select Option1 & Option2 it will show Option1 & Option2 in a <ul> on Product1's page . Then on the Option1 & Option2 pages they will have Product1 listed.

    I've populated the checkbox TV using the (old faithful) Multi-select box for related pages tutorial http://rtfm.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/template-variables/creating-a-multi-select-box-for-related-pages-in-your-template and can then pull the data from this to populate my <ul> but I'm a little unsure on the final step of saying 'only show Product1 on the Option1 resource if Option1 is selected' and so on.

    Code being used to populate the <ul> on the product page:

    ProductCategories TV
    @EVAL return $modx->runSnippet('listProductCategories',array('parent' => 70));

    listProductCategories Snippet
    $parent = $modx->getOption('parent',$scriptProperties,70);
    $parentObj = $modx->getObject('modResource',$parent);
    if (!($parentObj instanceof modResource)) { return ''; }
    $resArray = $parentObj->getMany('Children');
    $resources = array();
    foreach($resArray as $res) {
      if ($res instanceof modResource) {
        $resources[] = $res->get('pagetitle') . '==' . $res->get('id');
      }
    }
    $out = implode("||",$resources);
    return $out;

    productCategories Snippet
    if (empty($input)) { return 'Can\'t find any categories related to this product'; }
    $tpl = $modx->getOption('tpl',$scriptProperties,'productCategoryListTpl');
    if ($modx->getChunk($tpl) == '') { return 'We found some product categories, but don\'t know how to present it.'; }
    $ids = explode('||', $input);
    $output = array();
    foreach ($ids as $key => $value) {
      $resource = $modx->getObject('modResource',array(
        'published' => 1,
        'id' => $value));
      if ($resource instanceof modResource) {
        $ta = $resource->toArray();
        $tvs = $resource->getTemplateVars();
        foreach ($tvs as $tv) {
           $ta[$tv->get('name')] = $tv->get('value');
        }
        $output[] = $modx->getChunk($tpl,$ta);
      }
    }
    return implode('',$output);

    Call to populate the <ul>
    [[productCategories? &input=`[[*productCategories]]` &tpl=`productCategoryListTpl`]]


    Can the above also be tweaked to pull the info into the Option resources as needed?

    Thanks in advance