croxton / Stash

Stash allows you to stash text and snippets of code for reuse throughout your templates.
GNU General Public License v3.0
198 stars 20 forks source link

orderby when using "get_list" doesn't order properly when ordering on a numeric value #134

Closed snetman closed 9 years ago

snetman commented 9 years ago

I'm using stash to store a list of product entries and I'm storing their prices in a stash "price_sort" variable.

When I use get_list to display the entries, I'm using my price_sort variable as the orderby parameter, but the entries are not ordered properly. For example, if I have these prices:

And I use this tag:

{exp:stash:get_list name="products" orderby="price_sort" sort="asc"}

The entries are displayed in this order on the page:

It looks like orderby is not treating these as numbers, but as strings instead. Is there a way to get orderby to work properly when ordering on a numeric value?

croxton commented 9 years ago

sort_type='numeric' https://github.com/croxton/Stash/wiki/%7Bexp:stash:get_list%7D

snetman commented 9 years ago

I guess reading would have helped ;-). Thanks very much for pointing that out.

On Jul 27, 2015, at 11:24 AM, Mark Croxton notifications@github.com wrote:

sort_type='numeric' https://github.com/croxton/Stash/wiki/%7Bexp:stash:get_list%7D https://github.com/croxton/Stash/wiki/%7Bexp:stash:get_list%7D — Reply to this email directly or view it on GitHub https://github.com/croxton/Stash/issues/134#issuecomment-125243683.

snetman commented 9 years ago

So I've been trying to use the "numeric" sort_type, but the order of my entries is still not correct.

I'm able to orderby "price" in string mode in both ascending and descending orders. That behaves as expected. Here are some screenshots (you can see the value I'm storing for "price" in the black circle:

orderby="price" sort_type="string" sort="asc" https://www.dropbox.com/s/plyxzey4x69f5ov/price-string-asc.png?dl=0

orderby="price" sort_type="strong" sort="desc" https://www.dropbox.com/s/xe3xs94viuf49t4/price-string-desc.png?dl=0

But when I switch the sort_type to "numeric" the order doesn't behave as I would expect. Here are some screens:

orderby="price" sort_type="numeric" sort="asc" https://www.dropbox.com/s/gojm81owkfbyvqf/price-numeric-asc.png?dl=0

orderby="price" sort_type="numeric" sort="desc" https://www.dropbox.com/s/43vyiinopccxwlp/price-numeric-desc.png?dl=0

Any reason that you can see that would explain it?

snetman commented 9 years ago

Edited comment above to include dropbox links to screenshots since github didn't want to accept my attachments.

croxton commented 9 years ago

Just make sure your not saving any whitespace around your price value.

snetman commented 9 years ago

Is there a way to use your trim parameter on a variable set inside an append_list tag?

My (simplified) code structure looks like this:

{exp:stash:append_list name="products" parse_tags="yes"}

          {stash:name}{title}{/stash:name}

          {!-- Price for sorting --}
          {exp:store:product entry_id="{entry_id}"}
            {stash:price}{price_val}{/stash:price}
          {/exp:store:product}

{/exp:stash:append_list} 

But when I try to add trim="yes" when I store the price, the value isn't stored. I also tried using the full {exp:stash:set name="price"} syntax instead of the short version, but I'm guessing that doesn't work inside append_list.

Any suggestions?

croxton commented 9 years ago

You've not left any whitespace around the price in the code above so you don't need trim. You can check what stash is capturing by turning on template debugger and looking for lines beginning 'Stash:'. Check that the price value is being output without spaces each side or unparsed tags.

snetman commented 9 years ago

I checked the template debugger and it looks like my price value is getting captured correctly. Here is some debugger output from a listing page with two entries. You can see that for each one, price is stored as a plain old number.

For some context, these are bottles of wine and I'm storing 4 variables for each: name, url, image, price:

(0.216860 / 16.00MB)      Stash: SET products to value name|=|Domaine Dupeuble Beaujolais|&|url|=|http://shoptipsy.dev/shop/domaine-dupeuble-beaujolais|&|image|=|http://shoptipsy.dev/images/uploads/shop/Domaine_Dupeuble_Beaujolais.JPG|&|id|=|781|&|price|=|17|+|name|=|Joubert Beaujolais Cuvee a l’Ancienne|&|url|=|http://shoptipsy.dev/shop/joubert-beaujolais-cuvee-a-lancienne|&|image|=|http://shoptipsy.dev/images/uploads/shop/brouilly.jpg|&|id|=|259|&|price|=|18

Any other ideas?

If it adds any more context, the site is a wine shop site. Each bottle is an entry in a "shop" channel. There is also a "varietals" channel where each entry is a different type of grape. On the bottle entries, I've got a playa field where the client chooses the varietals that correspond to that bottle.

The pages that I'm currently debugging are pages for the varietals which include some information about the varietal (from the varietal entry) and then show a list of all the bottles that are related.

So the full stash setup looks like this:

{exp:channel:entries channel="varietals" url_title="{segment_3}" disable="categories|category_fields|member_data" dynamic="no"}

      {exp:stash:set}
        {stash:page_title}Made with {title}{/stash:page_title}
        {stash:name}{title}{/stash:name}
        {stash:description}{varietal_description}{/stash:description}
      {/exp:stash:set}

      {!-- Get the bottles that are related to this varietal --}

      {exp:playa:parents channel="shop" disable="categories|category_fields|member_data"}

        {exp:stash:append_list name="products" parse_tags="yes"}

          {stash:name}{title}{/stash:name}
          {stash:url}{title_permalink='shop'}{/stash:url}
          {stash:image}{shop_photo}{/stash:image}
          {stash:id}{entry_id}{/stash:id}

          {!-- Price for sorting --}
          {exp:store:product entry_id="{entry_id}"}
            {stash:price}{price_val}{/stash:price}  {!-- price_val gets the price without any decimals or currency symbols --}
          {/exp:store:product}

...Then there are some nested lists that store some additional bottle info like suggested pairings...

        {/exp:stash:append_list}

      {/exp:playa:parents}

    {/exp:channel:entries}