0

This question concerns the grid in: sales->orders->[select an order]->Information.

At the bottom you can see a grid with the orders. I would like to sort them by SKU.

In: /app/code/core/Mage/Sales/Model/Order.php I found:

public function getItemsCollection($filterByTypes = array(), $nonChildrenOnly = false)
{   
    if (is_null($this->_items)) {
        $this->_items = Mage::getResourceModel('sales/order_item_collection')
            ->setOrderFilter($this);

        if ($filterByTypes) {
            $this->_items->filterByTypes($filterByTypes);
        }
        if ($nonChildrenOnly) {
            $this->_items->filterByParent();
        }

        if ($this->getId()) {
            foreach ($this->_items as $item) {

                $item->setOrder($this);
            }
        }
    }
    return $this->_items;
}

This creates the order grid, but how do I sort it by SKU?

1

Actually the piece of code does not create the order grid. This retrieves the ordered items for an order. It's used both in frontend and backend.

If you modify it to sort by sku then the items will be ordered by sku in the backend and frontend.
In order to do this you need to add this piece of code:

$this->_items->getSelect()->order('sku');

Right after

if ($nonChildrenOnly) {
    $this->_items->filterByParent();
}

Of course, don't edit the code. Override the model properly.
But I wouldn't recommend this approach. If you want the order of items by sku only in the backend you could sort them in the template.
The template is app\design\adminhtml\default\default\template\sales\order\view\items.phtml. You can replace the code that renders the items (I mean this):

<?php $_items = $this->getItemsCollection() ?>
    <?php $i=0;foreach ($_items 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) ?>
        </tbody>
    <?php endforeach; ?> 

With this:

<?php $_items = $this->getItemsCollection() ?>
    <?php $_sortedItems = array(); ?>
    <?php foreach ($_items as $_item) : ?>
        <?php $_sortedItems[$_item->getSku()] = $_item;?>
    <?php endforeach;?>
    <?php ksort($_sortedItems);//sort by sku?>
    <?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) ?>
        </tbody>
    <?php endforeach; ?> 

Again...don't modify the template. Put it in your own theme and modify it there.
Here is how you can change and admin theme.

  • Hi Marius, Thanks, this one is great! I've updated your solution code (was a little error in it) – Bob van Luijt May 20 '13 at 18:35
  • Yeah...sorry, I forgot one underscore. – Marius May 20 '13 at 18:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.