Domanda

Stiamo usando Magento 1.8.1.Quando navigo verso il catalogo> Gestisci i prodotti Grid View dei nostri prodotti, vedo la colonna Imposta attributo, che viene visualizzato come menu a discesa selezionabile.

La sfida che stiamo avendo è che il nostro elenco di set di attributi è piuttosto lungo, e vengono ordinati in ordine di quando sono stati creati (quindi presumo che siano ordinati dal loro attributo_set_id o qualcosa del genere).

Vorrei ri-ordinare questo elenco in ordine alfabetico, in modo che se desidero trovare un set di attributi specifico, guardo semplicemente attraverso un elenco alfabetizzato, piuttosto che dover scorrere su e giù come un matto per trovare il set di attributo Ibisogno.

Apprezzo qualsiasi orientamento che puoi fornire.

Grazie.

È stato utile?

Soluzione

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

Contenuti:

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

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

Contenuti:

<?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: Crea app/code/local/MyProject/Catalog/Block/Adminhtml/Catalog/Product/Grid.php

Contenuto:

<?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 Cancella "Configurazione" Tipo di cache se abilitato.

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top