Sort Magento Additional Information Attributes
I’m a man who likes organisation. You wouldn’t know it from looking at my desk (or my sock drawer), but sometimes it’s important that things are in order. Today’s post solves a minor problem I had with a recent Magento site. By default, Magento creates a list of attributes on your product page and it shows them in no particular order. That’s all fine and well, but if they need to be ordered in some way it’s not possible to do.
By using the ‘position’ attribute provided on the attribute editor page, you can give the attribute a numerical value. This is used to position the attributes in the layered navigation, but if this doesn’t matter to you then you can use it to order the attributes list instead, which normally appears under the heading ‘Additional Information’ on your product page.
My namespace is Creare and my module is Sortattributes.
etc/config.xml
I’m simply rewriting product/view/Attributes.php in the catalog module, and I’m declaring a layout update in adminhtml:
<?xml version="1.0"?>
<config>
<modules>
<Creare_Sortattributes>
<version>0.1.0</version>
</Creare_Sortattributes>
</modules>
<global>
<blocks>
<catalog>
<rewrite>
<product_view_attributes>Creare_Sortattributes_Block_Catalog_Product_View_Attributes</product_view_attributes>
</rewrite>
</catalog>
<sortattributes>
<class>Creare_Sortattributes_Block</class>
</sortattributes>
</blocks>
</global>
<adminhtml>
<layout>
<updates>
<sortattributes>
<file>sortattributes.xml</file>
</sortattributes>
</updates>
</layout>
</adminhtml>
</config>
Block/Catalog/Product/View/Attributes.php
Now, I’m rewriting the getAdditionalData module. All I’m doing is adding two bits of code. Firstly, I’ve added a new array key for each attribute called ‘position’ and I’ve assigned it the position value which belongs to each attribute. Then I’ve added a function that you may have seen before called ‘subval_sort()’ around the line which returns the array.
<?php
class Creare_Sortattributes_Block_Catalog_Product_View_Attributes extends Mage_Catalog_Block_Product_View_Attributes
{
public function getAdditionalData(array $excludeAttr = array())
{
$data = array();
$product = $this->getProduct();
$attributes = $product->getAttributes();
foreach ($attributes as $attribute) {
// if ($attribute->getIsVisibleOnFront() && $attribute->getIsUserDefined() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
if ($attribute->getIsVisibleOnFront() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
$value = $attribute->getFrontend()->getValue($product);
if (!$product->hasData($attribute->getAttributeCode())) {
$value = Mage::helper('catalog')->__('N/A');
} elseif ((string)$value == '') {
$value = Mage::helper('catalog')->__('No');
} elseif ($attribute->getFrontendInput() == 'price' && is_string($value)) {
$value = Mage::app()->getStore()->convertPrice($value, true);
}
if (is_string($value) && strlen($value) && $value != "N/A") {
$data[$attribute->getAttributeCode()] = array(
'label' => $attribute->getStoreLabel(),
'value' => $value,
'position' => $attribute->getPosition(),
'code' => $attribute->getAttributeCode()
);
}
}
}
return $this->subval_sort($data, 'position');
}
public function subval_sort($a,$subkey)
{
foreach($a as $k=>$v)
{
$b[$k] = strtolower($v[$subkey]);
}
asort($b);
foreach($b as $key=>$val)
{
$c[] = $a[$key];
}
return $c;
}
}
What is subval_sort()?
I’ve used this function before on other sites as it’s very simple and effective to use. It simply reorders a multidimensional array by a sub-value. The sub-value I want to reorder by is ‘position’, so I call it as follows:
return $this->subval_sort($data, 'position');
This will return the array, ordered by position – which means we can now control the order of the attributes.
app/design/adminhtml/default/default/layout/sortattributes.xml
There’s one final problem we have to address. The position field is disabled if the attribute is not filterable (which means anything other than dropdown or multi-select). This is caused by a JavaScript check when the page loads. It’s easy to rectify though, start by creating the file above – we’ve already made the module aware of it in config.xml. Then add the following:
<?xml version="1.0"?>
<layout>
<adminhtml_catalog_product_attribute_edit>
<reference name="js">
<block type="adminhtml/template" after="attribute_edit_js" template="catalog/product/attribute/sortattributes_js.phtml" />
</reference>
</adminhtml_catalog_product_attribute_edit>
</layout>
app/design/adminhtml/default/default/template/catalog/product/attribute/sortattributes_js.phtml
Finally, create the file declared in our block, and add the following JavaScript:
<script type="text/javascript">// <![CDATA[
$('position').disabled = false;
// ]]></script>
How to use…
Using this is now means that we can set a position by going into Catalog > Attributes > Manage Attributes and clicking on the attribute we want to order. Enter your position in the Position field, then when you see it on the frontend it should be properly order (assuming you’ve ordered the rest too!).

Yes, it’s a little bit long-winded for something so simple, but is a solid solution. Again it depends if you want to use the position for your filters. You can technically use for both if you only want your text attributes showing in the Additional Information list. You could of course create another ‘attribute’ attribute for positioning, but that’s for another day.
Adam is Ecommerce Manager and a PHP developer at Creare Group. Adam is responsible for training Magento development within the company. Follow Adam on Twitter: http://twitter.com/adampmoss. - Read my other posts.
