We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36582
    • 463 Posts
    I'm trying to list a range of products by ascending value. The following call...

    [[getResources? 
    &parents=`146,11` 
    &resources=`-[[*id]]`
    &tpl=`productTpl`
    &tplLast=`productTplLast`
    &showHidden=`0` 
    &limit=`0`
    &depth=`3`
    &hideContainers=`1`
    &sortbyTV=`product_rrp`
    &sortdirTV=`ASC`
    &sortbyTVType=`decimal`
    &includeContent=`1` 
    &includeTVs=`1` 
    &processTVs=`1` 
    &tvPrefix=``
    &tvFilters=`make==%[[*pagetitle]]%||maker_text==%[[*pagetitle]]%`
    ]]


    ...messes up the result and displays prices in the following order:

    795
    595
    650
    895
    1,650
    1,395
    etc

    ...so in some sort of order but not quite. I'm aware that the comma might be a problem. Sorting by `integer` gives the same result.

    I've tried entering the `product_rrp` in formats such as 00,795 | 00,595 | 01,650 to no avail.

    I've considered stripping the comma out of the product_rrp field but I'm still not convinced I would get the right result when some prices are 3 figures, some are 4 figures and some are 5 figures.
    Revo 2.4.3
    GetResources 1.6.1

    Any help, much appreciated.

    This question has been answered by BobRay. See the first response.

      Web site design in Nottingham UK by Chris Fickling http://www.chrisficklingdesign.co.uk
      • 3749
      • 24,544 Posts
      I don't think it's a problem with the commas alone. I don't think it's sorting by that TV at all. If it were, the ones starting with one would be at the top and the list would be in ascending order based on the first digit.

      TVs are always returned as strings, so you might try removing the &sortbyTVType property and making them all 5 digits:

      00795
      00595
      00650
      00895
      1,650
      1,395


      There's seems to be something else wrong though, because getResources is supposed to sort the strings as decimals:

      case 'decimal':
           $criteria->select("CAST(IFNULL(tvSort.value, tvDefault.default_text) AS DECIMAL) AS sortTV");


      I imagine you've double-checked the spelling of the TV. Are any of them @INHERIT TVs?
        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
        • 36582
        • 463 Posts
        Thanks for looking at this Bob. I'm not using any @INHERIT TVs.

        I agree that &sortbyTVType appears to be doing nothing but removing the &sortbyTVType doesn't help. After some hunting around (inspired by PdoTools) I came up with the following - it's odd as it shouldn't work and still appears to do nothing when I debug. Prices are formatted exactly as my original post...

        [[getResources?
        &parents=`146,11`
        &resources=`-[[*id]]`
        &tpl=`productTpl`
        &tplLast=`productTplLast`
        &showHidden=`0`
        &limit=`0`
        &depth=`3`
        &hideContainers=`1`
        &sortbyTV=`product_rrp`
        &sortdirTV=`ASC`
        &sortbyTVType=`CAST(product_rrp AS DECIMAL(10,2));`
        &includeContent=`1`
        &includeTVs=`1`
        &processTVs=`1`
        &tvPrefix=``
        &tvFilters=`make==%[[*pagetitle]]%||maker_text==%[[*pagetitle]]%`
        ]]

        I'm wondering if PdoTools might be a better fit but too late for this project really. Nobody else using pricing to order output in Modx?
          Web site design in Nottingham UK by Chris Fickling http://www.chrisficklingdesign.co.uk
        • discuss.answer
          • 3749
          • 24,544 Posts
          Just a thought ... If you're going to sort by price, you might consider storing the price in one of the existing resource fields. If you can spare longtitle, introtext, or description you can use one of them for the price. That makes sorting easier and *way* faster. I'm not sure it it will solve your problem.

          pdoTools is much faster than getResources and the conversion is usually really easy. I'm not sure if will do a better job of sorting by price or not.

          If the prices don't contain commas, they should sort properly with sortbyTVType=`decimal` in getResources version 1.4.2. You can add the commas to the display with a custom snippet using the money_format() function.
            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
            • 3749
            • 24,544 Posts
            On second thought. Moving the price to a resource field is probably not a good idea, since the field would be sorted as a string and there's no way to tell getResources to cast it to a decimal.
              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
              • 36582
              • 463 Posts
              I did consider using *description for the price field but there are quite a few products and it's one more thing for the client to remember, if all product details aren't grouped together.

              A custom snippet using the money_format() function seems to be the way to go. I can strip the commas out of the database.

              Thank you for your help. We have a working solution to get the site live and I can tweak it from there.
                Web site design in Nottingham UK by Chris Fickling http://www.chrisficklingdesign.co.uk
                • 3749
                • 24,544 Posts
                Can you report on what worked, for others with the same problem?
                  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
                  • 36582
                  • 463 Posts
                  The following call worked but I didn't want to stress that too much as I've got a feeling it's not going to be the right solution for anybody else. I also formatted the product_rrp TV prices as...

                  00,795
                  00,595
                  01,650

                  [[getResources?
                  &parents=`146,11`
                  &resources=`-[[*id]]`
                  &tpl=`productTpl`
                  &tplLast=`productTplLast`
                  &showHidden=`0`
                  &limit=`0`
                  &depth=`3`
                  &hideContainers=`1`
                  &sortbyTV=`product_rrp`
                  &sortdirTV=`ASC`
                  &sortbyTVType=`CAST(product_rrp AS DECIMAL(10,2));`
                  &includeContent=`1`
                  &includeTVs=`1`
                  &processTVs=`1`
                  &tvPrefix=``
                  &tvFilters=`make==%[[*pagetitle]]%||maker_text==%[[*pagetitle]]%`
                  ]]

                    Web site design in Nottingham UK by Chris Fickling http://www.chrisficklingdesign.co.uk
                    • 3749
                    • 24,544 Posts
                    Thanks for posting that. Can you tell if the CAST is doing anything? It should be the equivalent of &sortbyTVType=`decimal`.
                      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
                      • 36582
                      • 463 Posts
                      When I originally tried it I 'switched on' debugging and the report told me nothing about whether it was working. All I know is that without CAST, it doesn't output correctly. The domain is propagating at the moment - when it's done I'll run debug again and report back.
                        Web site design in Nottingham UK by Chris Fickling http://www.chrisficklingdesign.co.uk