Jump to content

List products in order that was added


Recommended Posts

oh sorry, I see what you mean now. :)

 

if you want to sort them in the template, the simplest way would be to use a Smarty plugin that i've posted previously.

 

http://forum.whmcs.com/showthread.php?95519-temporarilly-change-sorting-of-items&p=400690#post400690

http://forum.whmcs.com/showthread.php?101339-Any-way-to-sort-Products-in-summary-page&p=420374#post420374

 

to add the Smarty plugin to v6.3.1+, create a new file called 'modifier.sortby.php' in the /vendor/smarty/smarty/libs/plugins/ folder and add the following code to it...

 

<?php

function array_sort_by_fields(&$data, $sortby){
     static $sort_funcs = array();

   if (empty($sort_funcs[$sortby]))
   {
       $code = "\$c=0;";
       foreach (split(',', $sortby) as $key)
       {
          $d = '1';
             if (substr($key, 0, 1) == '-')
             {
                $d = '-1';
                $key = substr($key, 1);
             }
             if (substr($key, 0, 1) == '#')
             {
                $key = substr($key, 1);
              $code .= "if ( \$a['$key'] > \$b['$key']) return $d * 1;\n";
$code .= "if ( \$a['$key'] < \$b['$key']) return $d * -1;\n";
             }
             else
             {
              $code .= "if ( (\$c = strcasecmp(\$a['$key'],\$b['$key'])) != 0 ) return $d * \$c;\n";
           }
       }
       $code .= 'return $c;';
       $sort_func = $sort_funcs[$sortby] = create_function('$a, $b', $code);
   }
   else
   {
       $sort_func = $sort_funcs[$sortby];
   }   
   uasort($data, $sort_func);   
}

#
# Modifier: sortby - allows arrays of named arrays to be sorted by a given field
#

function smarty_modifier_sortby($arrData, $sortfields) {
  array_sort_by_fields($arrData, $sortfields);
  return $arrData;
} 

then in your templates/orderforms/*template*/products.tpl template, you would change...

 

{foreach $products as $product}

to...

 

{foreach $products|@sortby:pid as $product}

the above code is from standard_cart/products.tpl, but I suspect the {foreach} code would be similar in the other templates too.

 

you should find that the products are now sorted in pid order - e.g the order in which you added them. :idea:

Link to comment
Share on other sites

  • 1 year later...

G'day Brian!

Thanks for the code to enable better sorting of the products in our order forms, but there's a tweak we're keen to make, but can't seem to work out how. So I'm hoping you can help.

Because not all of our products have been added in order, sorting by $pid doesn't work in every category.

What we're looking for is a way to sort the products according to the "sort" column from the products database.

This would ensure that regardless of $pid, the products would always be presented to our customers in exactly the way we intend from our backends.

I'm hoping you can help.

Cheers!

Link to comment
Share on other sites

I incorrectly said, "What we're looking for is a way to sort the products according to the "sort" column from the products database."

What I meant was, "What we're looking for is a way to sort the products according to the "order" column from the products database."

Link to comment
Share on other sites

Hi,

9 hours ago, ezyweb said:

I incorrectly said, "What we're looking for is a way to sort the products according to the "sort" column from the products database."

What I meant was, "What we're looking for is a way to sort the products according to the "order" column from the products database."

where are you looking to do this? if it's the products page, the products will already be shown sorted in the order they are in in the admin area (and hence the database).

if you want to change that sort order (of groups and/or products), you can do so in the admin area and that updated sort order will be reflected in the cart products page.

https://docs.whmcs.com/Products_and_Services#Sorting

Quote

Groups and Products are sortable using the crosshair icon on the appropriate row. This is on the left for Groups and the right for Products.

 

Link to comment
Share on other sites

Hi Brian,

Thanks for the reply. I'm aware of the use of the crosshairs to adjust the sorting in the admin area, and this had obviously been done.

But I've now discovered what the issue is. For some reason, even when using the crosshairs to sort the products into the relevant order in the admin area, the "order" table in the database remains filled with "0" for every product, which causes the problem of the incorrect order on the products.tpl.

I'm now going through the database to manually add the sort order for each product, and now their displaying correctly, but only once I returned the code back to its default of ...

{foreach $products as $product}

Thanks for your help. This tweak has been really helpful knowing regardless of whether I need to use it at this time or not.

Link to comment
Share on other sites

31 minutes ago, ezyweb said:

But I've now discovered what the issue is. For some reason, even when using the crosshairs to sort the products into the relevant order in the admin area, the "order" table in the database remains filled with "0" for every product, which causes the problem of the incorrect order on the products.tpl.

just tried this on a v7.4.2 dev and initially, even though there was an on-screen message saying that the change, it did nothing in the cart... it was only after refreshing the admin page in the browser, did it start to really save the order changes to the database.

and it's also worth noting that it only changes the order values of the products you move... any others unmoved will have a '0' value and will be shown in alphabetical order by product name after the "sorted" products.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated