2

Here is my remote files and their paths:

enter image description here

app/code/local/Sean/CustomerGrid/Block/Adminhtml/Customer/Grid.php

    <?php
class Sean_CustomerGrid_Block_Adminhtml_Customer_Grid extends Mage_Adminhtml_Block_Customer_Grid{
    /**
     * override the _prepareCollection to add an other attribute to the grid
     * @return $this
     */
    protected function _prepareCollection(){
        $collection = Mage::getResourceModel('customer/customer_collection')
            ->addNameToSelect()
            ->addAttributeToSelect('email')
            ->addAttributeToSelect('modify_catalog_labor')
            ->addAttributeToSelect('created_at')
            ->addAttributeToSelect('group_id')
            //if the attribute belongs to the customer, use the line below
            ->addAttributeToSelect('mobile')
            //if the attribute belongs to the customer address, comment the line above and use the one below
            //->joinAttribute('mobile', 'customer_address/mobile', 'default_billing', null, 'left')
            ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
            ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
            ->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
            ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
            ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');

        $this->setCollection($collection);
        //code from Mage_Adminhtml_Block_Widget_Grid::_prepareCollection()
        //since calling parent::_prepareCollection will render the code above useless
        //and you cannot call in php parent::parent::_prepareCollection()
        if ($this->getCollection()) {

            $this->_preparePage();

            $columnId = $this->getParam($this->getVarNameSort(), $this->_defaultSort);
            $dir      = $this->getParam($this->getVarNameDir(), $this->_defaultDir);
            $filter   = $this->getParam($this->getVarNameFilter(), null);

            if (is_null($filter)) {
                $filter = $this->_defaultFilter;
            }

            if (is_string($filter)) {
                $data = $this->helper('adminhtml')->prepareFilterString($filter);
                $this->_setFilterValues($data);
            }
            else if ($filter && is_array($filter)) {
                $this->_setFilterValues($filter);
            }
            else if(0 !== sizeof($this->_defaultFilter)) {
                $this->_setFilterValues($this->_defaultFilter);
            }

            if (isset($this->_columns[$columnId]) && $this->_columns[$columnId]->getIndex()) {
                $dir = (strtolower($dir)=='desc') ? 'desc' : 'asc';
                $this->_columns[$columnId]->setDir($dir);
                $this->_setCollectionOrder($this->_columns[$columnId]);
            }

            if (!$this->_isExport) {
                $this->getCollection()->load();
                $this->_afterLoadCollection();
            }
        }

        return $this;
    }

    /**
     * override the _prepareColumns method to add a new column after the 'email' column
     * if you want the new column on a different position just change the 3rd parameter
     * of the addColumnAfter method to the id of your desired column
     */
    protected function _prepareColumns(){
        $this->addColumnAfter('modify_catalog_labor', array(
            'header'    => Mage::helper('customer')->__('Labor'),
            'index'     => 'modify_catalog_labor',
            'type'      => 'decimal',
            'width'     => '240px',
            'renderer'  => 'sean_customergrid/adminhtml_grid_renderer_labor'
        ),'email');
        return parent::_prepareColumns();
    }
}

app/code/local/Sean/CustomerGrid/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Sean_CustomerGrid>
            <version>0.0.1</version>
        </Sean_CustomerGrid>
    </modules>
    <global>
        <blocks>
            <adminhtml>
                <rewrite>
                    <customer_grid>Sean_CustomerGrid_Block_Adminhtml_Customer_Grid</customer_grid>
                </rewrite>
            </adminhtml>
        </blocks>
    </global>
</config>

app/code/local/Sean/CustomerGrid/Block/Adminhtml/Grid/Renderer/Labor.php

<?php

class Sean_CustomerGrid_Block_Adminhtml_Grid_Renderer_Labor extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {

    public function render(Varien_Object $row) {

        return number_format($row->getModifyCatalogLabor(), 2);
    }

} 

enter image description here

I have an attribute I display in the Admin Panel called "Labor" but is there any way I can easily round that number to the nearest 2 decimals? 5.0000 --> 5.00, 4.5000 --> 4.50

I'm receiving this error:

Fatal error: Call to a member function setColumn() on a non-object in /home/makeyou/public_html/app/code/core/Mage/Adminhtml/Block/Widget/Grid/Column.php on line 291

1 Answer 1

2

Sorry this is all done using a mobile phone so haven't tested

app/code/local/Sean/CustomerGrid/Block/Adminhtml/Customer/Grid.php

class Sean_CustomerGrid_Block_Adminhtml_Customer_Grid extends Mage_Adminhtml_Block_Customer_Grid
{ 
    /** 
     * override the _prepareCollection to add an other attribute to the grid 
     * @return $this 
     */ 
    protected function _prepareCollection()
    { 
        $collection = Mage::getResourceModel('customer/customer_collection') 
            ->addNameToSelect() 
            ->addAttributeToSelect('email') 
            ->addAttributeToSelect('modify_catalog_labor') 
            ->addAttributeToSelect('created_at') 
            ->addAttributeToSelect('group_id') 
            //if the attribute belongs to the customer, use the line below 
            ->addAttributeToSelect('mobile') 
            //if the attribute belongs to the customer address, comment the line above and use the one below 
            //->joinAttribute('mobile', 'customer_address/mobile', 'default_billing', null, 'left') 
            ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left') 
            ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left') 
            ->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left') 
            ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left') 
            ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left'); 

        $this->setCollection($collection); 

        //code from Mage_Adminhtml_Block_Widget_Grid::_prepareCollection() 
        //since calling parent::_prepareCollection will render the code above useless 
        //and you cannot call in php parent::parent::_prepareCollection() 
        if ($this->getCollection()) { 
            $this->_preparePage(); 
            $columnId = $this->getParam($this->getVarNameSort(), $this->_defaultSort); 
            $dir      = $this->getParam($this->getVarNameDir(), $this->_defaultDir); 
            $filter   = $this->getParam($this->getVarNameFilter(), null); 

            if (is_null($filter)) { 
                $filter = $this->_defaultFilter; 
            } 

            if (is_string($filter)) { 
                $data = $this->helper('adminhtml')->prepareFilterString($filter); 
                $this->_setFilterValues($data); 
            } else if ($filter && is_array($filter)) { 
                $this->_setFilterValues($filter); 
            } else if(0 !== sizeof($this->_defaultFilter)) { 
                $this->_setFilterValues($this->_defaultFilter); 
            } 

            if (isset($this->_columns[$columnId]) && $this->_columns[$columnId]->getIndex()) { 
                $dir = (strtolower($dir)=='desc') ? 'desc' : 'asc'; 
                $this->_columns[$columnId]->setDir($dir); 
                $this->_setCollectionOrder($this->_columns[$columnId]); 
            } 

            if (!$this->_isExport) { 
                $this->getCollection()->load(); 
                $this->_afterLoadCollection(); 
            } 
        } 
        return $this; 
    } 

    /** 
     * override the _prepareColumns method to add a new column after the 'email' column 
     * if you want the new column on a different position just change the 3rd parameter 
     * of the addColumnAfter method to the id of your desired column 
     */ 
    protected function _prepareColumns()
    { 
        $this->addColumnAfter('modify_catalog_labor', array( 
            'header'    => Mage::helper('customer')->__('Labor'), 
            'index'     => 'modify_catalog_labor', 
            'type'      => 'decimal', 
            'width'     => '240px', 
            'renderer' => 'customergrid/adminhtml_grid_renderer_labor', 
        ),'email'); 
        return parent::_prepareColumns(); 
    } 
} 

app/code/local/Sean/CustomerGrid/Block/Adminhtml/Grid/Renderer/Labor.php

class Sean_CustomerGrid_Block_Adminhtml_Grid_Renderer_Labor extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{ 
    public function render(Varien_Object $row)
    { 
        return number_format($row->getModifyCatalogLabor(), 2); 
    }
} 
16
  • is Rendor suppose to be Render? Not sure if typo or not Jun 22, 2017 at 23:42
  • Yeah sorry as said using phone so auto correct Jun 22, 2017 at 23:45
  • np also what is suppose to go here? just empty string? '' => 'customergrid/adminhtml_grid_renderer_labour', and is it render or renderer here? Jun 22, 2017 at 23:56
  • Sorry should be renderer in those quotes I'll update it Jun 23, 2017 at 0:01
  • Please recheck code have fixed spelling mks Jun 23, 2017 at 0:02

Your Answer

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

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