Question

Could someone please point me in the right direction for displaying the items in an order in alphabetical order instead of chronological order?

I mainly need this for the pdf invoice (which I have already modified a fair amount to suit my needs but I'm stuck on this) I do realise that I may need to alter the way the invoice is sorted / ordered completely, and not just on the pdf production.

Normally the "ordered items" both in the customers cart, and in the invoice are in a chronological sort order of when they were added to the basket, however I would like them sorting by either SKU or Description / Title to aid with picking. Or even sorting by category would be an improvement.

If anyone can help with this I will be over the moon as I have spent the last 2 days trying to figure this out and have gotten nowhere.

Thanks for reading.

Oh, I'm using 1.5 CE. Thanks again.

Follow up:

I've found the code that sorts the order upon invoice creation:

    <?php $_items = $this->getInvoice()->getAllItems()>
    <?php $_i=0;foreach ($_items as $_item): ?>
    <?php if ($_item->getOrderItem()->getParentItem()) continue; else $_i++; ?>
    <tbody class="<?php echo $_i%2?'even':'odd' ?>">
        <?php echo $this->getItemHtml($_item) ?>
        <?php echo $this->getItemExtraInfoHtml($_item->getOrderItem()) ?>
    </tbody>
    <?php endforeach; ?>

And replaced it with this:

<?php $_items = $this->getInvoice()->getAllItems() ?>
<?php $_sortedItems = array(); ?>
<?php foreach ($_items as $_item) : ?>
    <?php $_sortedItems[$_item->getSku()] = $_item;?>
<?php endforeach;?>
<?php ksort($_sortedItems);>
<?php $i=0;foreach ($_sortedItems as $_item):?>
    <?php if ($_item->getParentItem()) continue; else $i++;?>
    <tbody class="<?php echo $i%2?'even':'odd' ?>">
        <?php echo $this->getItemHtml($_item) ?>
        <?php echo $this->getItemExtraInfoHtml($_item->getOrderItem()) ?>
                </tbody>
<?php endforeach; ?>

However now the invoice produces the A's for example first like it should, however it only actually shows the first item. The rest of the invoice is item less! :(

I made a very similar code change to the file that produces the table for orders in adminhtml and this is working perfectly!?? ??confused?? Maybe someone can help me further please??

On further examination, it is apparent that the single remaining visible item is in fact a simple product, and that the configurable products are vanishing from the invoice!???

_ edit __

I've solved it!

The reason I was only getting simple products listed was the clue! Instead of sorting by SKU I've simply sorted the list by Name using getName() in place of getSKU in the above code!!! :D

Was it helpful?

Solution

The answer is this:

<?php $_items = $this->getInvoice()->getAllItems() ?>
<?php $_sortedItems = array(); ?>
<?php foreach ($_items as $_item) : ?>
    <?php $_sortedItems[$_item->getName()] = $_item;   //getName() was the key! ?>
<?php endforeach;?>
<?php ksort($_sortedItems);?>
<?php $i=0;foreach ($_sortedItems as $_item):?>
<?php if ($_item->getOrderItem()->getParentItem()) continue; else $_i++; ?>
    <?php //if ($_item->getParentItem()) continue; else $i++;?>
    <tbody class="<?php echo $i%2?'even':'odd' ?>">
        <?php echo $this->getItemHtml($_item) ?>
        <?php echo $this->getItemExtraInfoHtml($_item->getOrderItem()) ?>
                </tbody>
<?php endforeach; ?> 

Sorting by SKU was a problem as configurable products have a different SKU type. So I simplified it and sorted it by Name. Now all items are listed, and listed alphabetically. This sorts the invoice in adminhtml

Also for the actual print side of things in invoice.php use:

$items = $invoice->getAllItems()  ; // Gets items --- need to sort them first! 
            $_sortedItems = array();                        // build array, inserts order items into array and sort
            foreach ($items as $item) :
                $_sortedItems[$item->getName()] = $item;
            endforeach;

            ksort($_sortedItems);
            foreach ($_sortedItems as $item) {   //pass sorted items back one at a time in alpha' order

            if ($item->getOrderItem()->getParentItem()) {
                continue;

in place of

foreach ($invoice->getAllItems() as $item){  
            if ($item->getOrderItem()->getParentItem()) {
                continue;

OTHER TIPS

  1. You need to find the collection which is in use for what you want to change.

  2. Do something like this:

    $collection->getSelect()->order('main_table.attribute_id ASC');

Of course you need to change attribute_id to the field/attribute you want to sort, and ASC and DESC for the order...

Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top