4

We are using Magento 1.8.1. When I navigate to the Catalog > Manage Products grid view of our products, I see the Attribute Set column, which displays as a selectable dropdown menu.

The challenge we are having is that our list of attribute sets is quite long, and they are ordered in order of when they were created (so I assume they are ordered by their attribute_set_id or something like that).

I would like to re-sort this list alphabetically, so that if I wish to find a specific attribute set, I simply look through an alphabetized list, rather than having to scroll up and down like crazy to find the attribute set I need.

I appreciate any guidance you can provide.

Thanks.

3

Step 1: Create app/etc/modules/MyProject_Catalog.xml

Contents:

<?xml version="1.0"?>
<config>
    <modules>
        <MyProject_Catalog>
            <active>true</active>
            <codePool>local</codePool>
        </MyProject_Catalog>
    </modules>
</config>

Step 2: Create app/code/local/MyProject/Catalog/etc/config.xml

Contents:

<?xml version="1.0"?>
<config>
    <modules>
        <MyProject_Catalog>
            <version>1.0.0</version>
        </MyProject_Catalog>
    </modules>
    <global>
        <blocks>
            <adminhtml>
                <rewrite>
                    <catalog_product_grid>MyProject_Catalog_Block_Adminhtml_Catalog_Product_Grid</catalog_product_grid>
                </rewrite>
            </adminhtml>
        </blocks>
    </global>
</config>

Step 3: Create app/code/local/MyProject/Catalog/Block/Adminhtml/Catalog/Product/Grid.php

Contents:

<?php
class MyProject_Catalog_Block_Adminhtml_Catalog_Product_Grid 
    extends Mage_Adminhtml_Block_Catalog_Product_Grid
{
    public function _prepareColumns()
    {
        parent::_prepareColumns();

        $this->removeColumn('set_name');

        $sets = Mage::getResourceModel('eav/entity_attribute_set_collection')
            ->setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId())
            ->setOrder('attribute_set_name', 'asc')
            ->load()
            ->toOptionHash();

        $this->addColumnAfter('set_name',
            array(
                'header'=> Mage::helper('catalog')->__('Attrib. Set Name'),
                'width' => '100px',
                'index' => 'attribute_set_id',
                'type'  => 'options',
                'options' => $sets,
        ), 'type');

        $this->sortColumnsByOrder();

        return $this;
    }
}

Step 4 Clear 'configuration' cache type if enabled.

  • This is working as expected and I give you +1. You just solved a logical issue in Magento. Could you please help me with other changes same like this? First: When I create a New Product, Attribute Set list is not sorted alphabetically. Second: When I create a New Attribute Set, the Attribute Set list is not sorted alphabetically. Third: In editing Attribute Set page, unassigned attributes are not sorted. Thank you! – ADDISON74 Mar 20 '17 at 20:53
  • Never mind, I figured out based on your solution. – ADDISON74 Mar 21 '17 at 17:30
  • @ADDISON74 Just saw this, glad you were able to resolve your issues :) – Darren Felton Mar 19 '18 at 16:55
1

In app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php there is this code that retrieves the attribute set.

$sets = Mage::getResourceModel('eav/entity_attribute_set_collection')
        ->setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId())
        ->load()
        ->toOptionHash();

In order to get is sorted by name the code needs to look like this

$sets = Mage::getResourceModel('eav/entity_attribute_set_collection')
        ->setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId())
        ->setOrder('attribute_set_name', 'ASC')
        ->load()
        ->toOptionHash();

But don't edit the core. Override the class.

  • Please take a look at my answer for how to do this by overriding the class. – Darren Felton Jan 15 '15 at 22:12
0

Really I wanted to do this all Via XML without any observers but using XML didn't quite work as removing the original column was not possible as the XML to remove was actioned before before the column itself had been added. But I will add my code anyway.

In you module layout file:

    <?xml version="1.0"?>
    <layout>

        <!-- catalog product index action -->
        <adminhtml_catalog_product_index>
            <reference name="product.grid">
                <action method="addColumnAfter">
                    <columnId>set_name_new</columnId>
                    <arguments helper="theextensionlab_attributes/data/getAttributeSortedByName"/>
                    <after>type</after>
                </action>
            </reference>
        </adminhtml_catalog_product_index>

<!-- catalog product grid action (ajax) -->
        <adminhtml_catalog_product_grid>
            <reference name="admin.product.grid">
                <action method="addColumnAfter">
                    <columnId>set_name_new</columnId>
                    <arguments helper="theextensionlab_attributes/data/getAttributeSortedByName"/>
                    <after>type</after>
                </action>
            </reference>
        </adminhtml_catalog_product_grid>

    </layout>

A helper that pulls in the attribute sets via helper="" with ->setOrder('attribute_set_name', 'ASC') to sort by name

<?php  class Theextensionlab_Attributes_Helper_Data extends Mage_Core_Helper_Abstract
{
    public function getAttributeSortedByName(){
        $sets = Mage::getResourceModel('eav/entity_attribute_set_collection')
            ->setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId())
            ->setOrder('attribute_set_name', 'ASC')
            ->load()
            ->toOptionHash();

        $column = array(
            'header' => "Attrib. Set Name - Asc",
            'index'  => "attribute_set_id",
            'type'  =>  "options",
            'options'   => $sets
        );

        return $column;
    }
}

If we were just adding a second Attribute set column sorted by Name this next part would be needed but since I didn't want to leave the original I added an observer to remove the original Column.

 <adminhtml>
        <events>
            <adminhtml_catalog_product_grid_prepare_massaction>
                <observers>
                    <theextensionlab_attributes>
                        <class>Theextensionlab_Attributes_Model_Observer</class>
                        <method>adminhtmlCatalogProductGridPrepareMassaction</method>
                    </theextensionlab_attributes>
                </observers>
            </adminhtml_catalog_product_grid_prepare_massaction>
        </events>
    </adminhtml>

Add then the observer itself to remove the original column. You could ofcourse also do the addColumn here and not do the XML stuff but I found if I did that it didn't listen to the sort and allways was added as the last column.

<?php class Theextensionlab_Attributes_Model_Observer
{
    public function adminhtmlCatalogProductGridPrepareMassaction(Varien_Event_Observer $observer)
    {
        $block = $observer->getBlock();
        $block->removeColumn('set_name');
    }
}

Partly based upon : http://www.ecomdev.org/2010/07/27/adding-order-attribute-to-orders-grid-in-magento-1-4-1.html

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.