Changing the order of products on invoice

Highlighted
New Member
7 0 0

Hi,

Currently the products on our invoices is the order in which they were placed in the cart.

However items in our pull room are placed by SKU.

Is there a way to reorganize items in the invoice so that they appear by SKU?

Thanks

 

0 Likes
Highlighted

Are you using  Order Printer app?

Available for hiring. Inbox me lixonic[at]gmail[dot]com
0 Likes
Highlighted
New Member
7 0 0

We are using Order Printer.

Is there a way to modify the templates to reorder products?

0 Likes
Highlighted

I'm not sure whether we can sort the line_item object within the for loop. Instead, I used javascript. Maybe it's not perfect, but it works.

<script type="text/javascript">
   function sortData(tableId) {
        // Read table body node.
        var tableData = document.getElementById(tableId).getElementsByTagName('tbody').item(0);
        // Read table row nodes.
        var rowData = tableData.getElementsByTagName('tr'); 
        for(var i = 0; i < rowData.length - 1; i++) {
            for(var j = 0; j < rowData.length - (i + 1); j++) {
            
                var locale = rowData.item(j).getElementsByTagName('td').item(0).innerHTML.localeCompare(rowData.item(j+1).getElementsByTagName('td').item(0).innerHTML);
                //Swap row nodes if short condition matches
                if(locale == 1) {
                    tableData.insertBefore(rowData.item(j+1),rowData.item(j));
                }
                if(locale == -1){
                  tableData.insertBefore(rowData.item(j),rowData.item(j));
                }
            }
        }
    }
 
    // self executing function here
    (function() {
        sortData('SortData');
     })();
   </script>

Screenshots from my Test Shop

https://monosnap.com/file/DUl68U5UznsghXioI1iCrKjnZBV8l7.png

https://monosnap.com/file/LJ7eSTReHIF3NZ4Tjd8H4EI6EBnAXW.png

Invoice Template code > https://gist.github.com/lixonic/fc77a3da9a1fdd43e8b1e66c9c4085a5

 

Available for hiring. Inbox me lixonic[at]gmail[dot]com
1 Like
Highlighted

And please remember to take a backup of current invoice template.

Available for hiring. Inbox me lixonic[at]gmail[dot]com
1 Like
Highlighted
New Member
7 0 0

Thanks!

And it is always a blessing to have a backup.

0 Likes
Highlighted
Shopify Expert
2686 67 715

Generally, one should be able to sort the array before the for loop, like this:

{% assign sorted_line_items = line_items | sort: 'sku' %}
{% for line_item in sorted_line_items %}
  ...
{% endfor %}

 

Want to hire me to tweak a theme? Mail me at tairli@yahoo.com!
My post solved your problem? Like it!
I am away till Feb-15.
1 Like
Tourist
6 0 0

Where would this be placed?

0 Likes