Page 1 of 1

How to sort products by model number in shopping cart and on invoice

Posted: Sun Dec 31, 2023 12:58 pm
by 14Steve14
I asked this question many years ago when I was updating my current store but the code has changed since then and the old way of doing it no longer seems to work.

We sort similar products in the same area of the store room, so when we use the invoice or packing slip as a picking list it saves time to have the products listed on the invoice and packing slips in model number order. I think last time it ewas decided to save the items in the basket in the correct order, that then showed them in the right order on the invoice/packing slip.

Before I had to create a new copy of the shopping cart file which is now in includes/versioned/1.0.8.7/shopping_cart.php and add to that file. That now produces a blank page.

What would be the best way to sort the products in the shopping basket, packing slip and invoice by model number?

Re: How to sort products by model number in shopping cart and on invoice

Posted: Sun Dec 31, 2023 1:27 pm
by burt
I've had a quick glance at the code and can see where to make a change so the Admin side works as you want, but I am not able to find a hook call that would be suitable. In the meantime, until someone else can find a hook (and write the hook)...or someone finds a different solution;

Make a copy of;
/includes/system/versioned/1.0.7.10/database_order_builder.php

To (note that you might need to create the override folder and that it has nothing to do with the override tmplate);
/includes/system/override/database_order_builder.php

Change L119 in that NEW file to:

Code: Select all

$order_products_query = $GLOBALS['db']->query("SELECT * FROM orders_products WHERE orders_id = " . (int)$this->order->get_id() . " ORDER BY products_model");
Save. Invoice and Packing Slip should now be ordered by model.

-

Note that if someone comes up with a better solution that does not involve overriding the database_order_builder file, all you need to do is delete that NEW database_order_builder.php file and you are back to normal. Then follow their instructions to a better solution.

Re: How to sort products by model number in shopping cart and on invoice

Posted: Sun Dec 31, 2023 4:07 pm
by 14Steve14
Thanks @burt Gary. Works like a charm with the test products so hopefully should be alright when there are more items in an order.

Another change ticked off the very long list.

Re: How to sort products by model number in shopping cart and on invoice

Posted: Sun Dec 31, 2023 4:22 pm
by ecartz
burt wrote: Sun Dec 31, 2023 1:27 pm until someone else can find a hook

Code: Select all

      $GLOBALS['all_hooks']->cat('constructOrder', $this);
https://github.com/CE-PhoenixCart/Phoen ... 1C1-L31C59

Something like

Code: Select all

public function listen_constructOrder($order) {
  usort($order->products, function ($a, $b) { return $a['model'] <=> $b['model']; });
}
But if you want the products sorted in the cart, that should also sort in the order for future products. You can sort the cart with any hook that runs before the display.

Re: How to sort products by model number in shopping cart and on invoice

Posted: Sun Dec 31, 2023 6:18 pm
by burt
I missed that Hook. So to test it, try (and note that I have not tested this);

1. Delete that new override file you made.

2.
New File:
/includes/hooks/admin/siteWide/sortProducts.php

Content:

Code: Select all

class hook_admin_siteWide_sortProducts {
  
  public function listen_constructOrder($order) {
    usort($order->products, function ($a, $b) { return $a['model'] <=> $b['model']; });
  }

}
As it's sitewide that Hook should work for both invoice and packingslip.
Be sure to check for unwanted gremlins elsewhere - if any appear let us know.

Re: How to sort products by model number in shopping cart and on invoice

Posted: Sat Mar 23, 2024 11:57 am
by 14Steve14
This hook seems to work as expected. Not found anything wrong elsewhere yet.