Question

Nous utilisons Magento 1.8.1.Lorsque j'accède à la vue en grille Catalogue > Gérer les produits de nos produits, je vois la colonne Ensemble d'attributs, qui s'affiche sous la forme d'un menu déroulant sélectionnable.

Le défi auquel nous sommes confrontés est que notre liste d'ensembles d'attributs est assez longue et qu'ils sont classés par ordre de création (je suppose donc qu'ils sont classés par leur attribut_set_id ou quelque chose comme ça).

Je voudrais trier à nouveau cette liste par ordre alphabétique, de sorte que si je souhaite trouver un ensemble d'attributs spécifique, je regarde simplement une liste alphabétique, plutôt que d'avoir à faire défiler de haut en bas comme un fou pour trouver l'ensemble d'attributs dont j'ai besoin.

J'apprécie tous les conseils que vous pouvez fournir.

Merci.

Était-ce utile?

La solution

Étape 1: Créer app/etc/modules/MyProject_Catalog.xml

Contenu:

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

Étape 2: Créer app/code/local/MyProject/Catalog/etc/config.xml

Contenu:

<?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>

Étape 3: Créer app/code/local/MyProject/Catalog/Block/Adminhtml/Catalog/Product/Grid.php

Contenu:

<?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;
    }
}

Étape 4 Effacez le type de cache « configuration » s’il est activé.

Autres conseils

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.

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top