1

I want to sort items ordered by product name in magento backend. I followed this link, https://stackoverflow.com/questions/17572936/magento-change-sort-order-of-ordered-items, but what this is doing is removing products with the same name(like in configurable different simple products). Please help/

2

The order items are displayed in the backend using the tempalte app/design/adminhtml/default/default/template/sales/order/view/items.phtml.

You should not edit the core file, so you should create your own admin theme and copy the file in that theme, keeping the relative path. Let's call that theme custom. So create the folder app/design/adminhtml/default/custom/template/sales/order/view.
Copy the file items.phtml mentioned above to this folder.
Now add this in the config.xml from one of your extensions inside the <config> tag.

<stores>
    <admin>
        <design>
            <theme>
                <default>custom</default>
            </theme>
        </design>
    </admin>
</stores>

More details about setting a custom admin theme can be found here.

Now in the template you need to change the sort of the items.

So after this line <?php $_items = $this->getItemsCollection() ?> add the following code.

<?php 
$sorted = array();
foreach ($_items as $_item) {
    if ($_item->getParentItem()) { //skip dummy products
      continue; 
    }
    $itemName = $item->getName();
    if (!isset($sorted[$itemName])) {
        $sorted[$itemName] = array();
    }
    $sorted[$itemName][] = $item;  //use an array for each name, in case there are items with the same name.
}
//sort the items by name
ksort($sorted);
//now you have in $sorted a 2-dimensional array. Let's make it 1 dimension.

$allItems = array();
foreach ($sorted as $name => $items) {
    $allItems = array_merge($allItems, $items);
}
?>

Now you have in $allItems the items sorted by name.
Replace the line <?php $i=0;foreach ($_items as $_item):?> with

<?php $i=0;foreach ($allItems as $_item):?>

and you are done.

|improve this answer|||||

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.