0

I'm trying to group the results returned from Low Search using Stash. I started by creating a stash list of all the groups, I then tried to create a nested list containing the products for that group.

When I call the get_list, I can return the groups just fine, but trying to fetch the products returns nothing.

Here is my code so far (Bear in mind I know that this will add ALL products into each group, but I'm taking this one step at a time)

{layout="includes/_layout"}

    {exp:stash:set parse_conditionals="yes" parse_tags="yes" trim="yes"}

        {!-- Set Other Page Data --}


        {!-- Results --}
        {exp:stash:set_list:searchRanges name="searchRanges" parse_tags="yes" trim="yes"}
            {exp:low_search:results query="{segment_3}" collection="products" keywords:lang="en" keywords:inflect="yes" keywords:loose="right" keywords:mode="all" limit="999"}
                {stash:itemRange}{products__range:url_title}{/stash:itemRange}

                {exp:stash:append_list:nested name="searchResults--{products__range:url_title}" parse_tags="yes" trim="yes"}
                    {stash:productTitle}sdfds{/stash:productTitle}
                {/exp:stash:append_list:nested}
            {/exp:low_search:results}
        {/exp:stash:set_list:searchRanges}

    {/exp:stash:set}



    {!-- Fetch the data --}
    {exp:stash:get_list name="searchRanges" unique="itemRange"}
        <p>{itemRange} - This is being returned.</p>

        {!-- Adding process="end" to this makes it return the correct amount, but adding process="end" to the nested get_list still returns nothing --}
        {exp:stash:list_count name="searchResults--{itemRange}" process="end"}

        {exp:stash:get_list:nested name="searchResults--{itemRange}"}
            <p>This content isn't being returned</p>
        {/exp:stash:get_list:nested}
    {/exp:stash:get_list}

Edit

Looking at the template debugger, I can see the get_list is parsing the name="searchResults--{itemRange}" correctly, but I can't see the list being set anywhere, so looks like an it's an issue with the nested list not being set. I've tried adding process="end" to the nested append_list but nothing still

3
  • 1
    The closing tag for the nested stash append tag has a typo in it. It should be: {/exp:stash:append_list:nested}. Commented Mar 31, 2015 at 11:43
  • I did notice that, sorry forget to update the code, didn't make a difference though.
    – KeironLowe
    Commented Mar 31, 2015 at 16:39
  • This doesn't appear to be a Low Search issue/question, as the same can be replicated with the native channel:entries tag. And I'm not a Stash expert, so I'm afraid I can't chip in on that part.
    – Low
    Commented Apr 1, 2015 at 9:18

2 Answers 2

2

You only need one list. You can use the unique="" parameter to output unique values of a given column, and the match="" & against="" parameters in a nested call to the same list to select products that match each unique value.

{!-- Capture search results --}
{exp:stash:set_list name="products" parse_tags="yes" parse_depth="2"}
    {exp:low_search:results query="{segment_3}" collection="products" keywords:lang="en" keywords:inflect="yes" keywords:loose="right" keywords:mode="all" limit="999"}
        {stash:item_range}{products__range:url_title}{/stash:item_range}
        {stash:product_title}{title}{/stash:product_title}
    {/exp:low_search:results}
{/exp:stash:set_list}

{!-- Output products grouped by range --}
{exp:stash:get_list name="products" unique="item_range" orderby="item_range" sort="asc"}

    <h1>Range: {item_range}</h1>

    <ul>
    {exp:stash:get_list:range name="products" match="#^{item_range}$#" against="item_range" prefix="range"}
        <li>{range:product_title}</li>
    {/exp:stash:get_list:range}
    </ul>

{/exp:stash:get_list}
3
  • Really close, product_title is repeating the same value
    – KeironLowe
    Commented Apr 1, 2015 at 12:55
  • Using a prefix on the inner get_list fixed the issue, cheers!
    – KeironLowe
    Commented Apr 1, 2015 at 12:57
  • Ah yes sorry forgot that - edited my answer. Commented Apr 1, 2015 at 13:44
0

Edit

See Mark's answer, it's much better.


I managed to sort the issue myself, I'm sure there is a better way of doing this, but it works for now.

Setting

I first started by getting all the product ranges into a list. Notice the parse_depth="2" on the outer set tag. This does not work without it.

{exp:stash:set parse_conditionals="yes" parse_tags="yes" trim="yes" parse_depth="2"}

    {!-- Get all product ranges --}
    {exp:stash:set_list:productRanges name="productRanges" parse_tags="yes" trim="yes"}
        {exp:channel:entries channel="ranges" dynamic="no" limit="999" disable="categories|category_fields|member_data|pagination"}
            {stash:rangeName}{title}{/stash:rangeName}
            {stash:rangeShortname}{url_title}{/stash:rangeShortname}
        {/exp:channel:entries}
    {/exp:stash:set_list:productRanges}

{/exp:stash:set}

Now that we've got the ranges stored, we can then create a new list per range, and then using the match and against parameters, we can add products from the search results to each list if they are assigned to that product range.

{exp:stash:set parse_conditionals="yes" parse_tags="yes" trim="yes" parse_depth="2"}

    {!-- Get all product ranges --}
    {exp:stash:set_list:productRanges name="productRanges" parse_tags="yes" trim="yes"}
        {exp:channel:entries channel="ranges" dynamic="no" limit="999" disable="categories|category_fields|member_data|pagination"}
            {stash:rangeName}{title}{/stash:rangeName}
            {stash:rangeShortname}{url_title}{/stash:rangeShortname}
        {/exp:channel:entries}
    {/exp:stash:set_list:productRanges}

    {!-- Create a new list for each range, and add the products from the search results if they are assigned to that range. --}
    {exp:stash:get_list name="productRanges"}
        {exp:stash:set_list:rangeProducts name="results--{rangeShortname}" parse_tags="yes" trim="yes" match="#{rangeShortname}#" against="productRange"}
            {exp:low_search:results query="{segment_3}" collection="products" keywords:lang="en" keywords:inflect="yes" keywords:loose="right" keywords:mode="all" limit="999"}
                {stash:productName}{title}{/stash:productName}
                {stash:productRange}{products__range:url_title}{/stash:productRange}
            {/exp:low_search:results}
        {/exp:stash:set_list:rangeProducts}
    {/exp:stash:get_list}

{/exp:stash:set}

Getting

Now that the lists are set it's easy to get them.

{exp:stash:get_list name="productRanges"}
    {if {exp:stash:not_empty name="results--{rangeShortname}"}}
        <p>{rangeShortname}</p>

        <ul>
            {exp:stash:get_list:nested name="results--{rangeShortname}"}
               <li>{productName}</li>
            {/exp:stash:get_list:nested}
        </ul>
    {/if}
{/exp:stash:get_list}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.