0

I added a custom column to display weight of the product in product grid.

The grid displays the weights correctly, when filtered, the column disappears, but if refresh the page it returns and filters the records, when ordering the same thing happens.

I do not know why that happens, do not want to override the default magento code, just add a column without involving alerar codes pattern.

My codes:

config.xml

<adminhtml> 
    events>
        <eav_collection_abstract_load_before>
            <observers>
                <dexxtz_customgrid>
                    <class>dexxtz_customgrid/observer</class>
                    <method>onEavLoadBefore</method>
                </dexxtz_customgrid>
            </observers>
        </eav_collection_abstract_load_before>
    </events>
</adminhtml>

layout.xml

<layout>
    <catalog_product_grid_update_handle translate="label" module="dexxtz_customgrid">
        <reference name="product.grid">
            <action method="addColumnAfter" ifconfig="dexxtz_customgrid/weight/active">
                <columnId>weigth</columnId>
                <arguments helper="customgrid/getWeightColumnParams" />
            </action>
        </reference>
    </catalog_product_grid_update_handle>
    <adminhtml_catalog_product_grid>
        <update handle="catalog_product_grid_update_handle" />
    </adminhtml_catalog_product_grid>
    <adminhtml_catalog_product_index>
        <update handle="catalog_product_grid_update_handle" />
    </adminhtml_catalog_product_index>
</layout>

Observer.php

<?php

class Dexxtz_Customgrid_Model_Observer
{
    public function onEavLoadBefore(Varien_Event_Observer $observer) {
        $collection = $observer->getCollection();
        if (!isset($collection)) return;


        if (is_a($collection, 'Mage_Catalog_Model_Resource_Product_Collection')) {
            $collection->addAttributeToSelect('weight');
        }
    }
}

function getWeightColumnParams()

public function getWeightColumnParams()
{
    $array = array('header' => 'Weight',
                   'index' => 'weight',
                   'filter_index' => 'weight',
                   'header_css_class' => 'a-center',
                   'align' => 'center',
                   'type' => 'text',
                   'width' => 100);

    return $array;
}

The problem seems to be in:

<adminhtml_catalog_product_grid>
    <update handle="catalog_product_grid_update_handle" />
</adminhtml_catalog_product_grid>

http://www.magentocommerce.com/boards/viewthr./viewthread/268277/

updated the catalog.xml and did not work

1 Answer 1

0

Solution, had to make a superscript block

config.xml

<global>        
    <blocks>
        <adminhtml>
            <rewrite>
                <catalog_product_grid>Dexxtz_Customgrid_Block_Adminhtml_Catalog_Product_Grid</catalog_product_grid>
            </rewrite>
        </adminhtml>
    </blocks>
</global>

added Dexxtz_Customgrid_Block_Adminhtml_Catalog_Product_Grid.php

<?php

class Dexxtz_Customgrid_Block_Adminhtml_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid
{    
    protected function _prepareCollection()
    {
        parent::_prepareCollection();
        $this->getCollection()->addFieldToFilter('weight', 1);

        return $this;
    }

    protected function _prepareColumns()
    {
        $this->addColumnAfter('weight', Mage::helper('customgrid')->getWeightColumnParams());

        parent::_prepareColumns();
        return $this;
    }
}

removed layout.xml

Summarizing what was necessary to run:

Add a rewrite in config.xml and having two functions _prepareCollection, _prepareColumns in Grid.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.