2

Currently my Orders Grid is set up to add a Weight column with the total order weight, but it is displaying with 4 decimal places and I would like to only display 2 decimal places.

_prepareCollection

$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->joinInner(array('order_address' => Mage::getSingleton('core/resource')->getTableName('sales_flat_order_address')),'order_address.parent_id = main_table.entity_id',array('company','telephone'))->group('order_address.parent_id');
$collection->getSelect()->join(array('sfo'=>'sales_flat_order'),'sfo.entity_id=main_table.entity_id',array('sfo.customer_email','sfo.weight')); 
$this->setCollection($collection);

_prepareColumns

...
$this->addColumn('weight', array(
        'header' => Mage::helper('sales')->__('Weight'),
        'index' => 'weight',
        'filter_index' => 'sfo.weight',
        'width' => '50px',
));
...

What is the proper way to format the data in the grid?

4

There are a couple of ways to do it.

SQL-level

$collection->getSelect()
    ->columns(array('weight' => new Zend_Db_Expr("DECIMAL(weight, 2)")));

Here we re-define the weight column in the selection object to select weight as a float rounded to the nearest hundredth.

Application-level

# Your block, _prepareColumns method
$this->addColumn('weight', array(
        'header' => Mage::helper('sales')->__('Weight'),
        'index' => 'weight',
        'filter_index' => 'sfo.weight',
        'width' => '50px',
        'renderer' => 'yourmodule/adminhtml_grid_renderer_yourblock',
                         |
));                      |
                         |
# Your renderer block <--|
class Namespace_Yourmodule_Block_Adminhtml_Grid_Renderer_Yourblock
    extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{

    // Where $row is an order model
    public function render(Varien_Object $row)
    {
        return number_format($row->getWeight(), 2);
    }

}

Here we leverage Magento's column renderer feature to pass each row of the grid (order model in this case) through a custom block to manipulate and return the display value.

  • I tried setting this up at the SQL-level and it is an improvement, but still not working quite ideally. It still allows 0 or 1 decimal points as well (56 or 56.2, for example). My exact implementation is $collection->getSelect()->join(array('sfo'=>'sales_flat_order'),'sfo.entity_id=main_table.entity_id',array('sfo.customer_email','weight' => new Zend_Db_Expr("ROUND(sfo.weight,2)"))); – travisw Jul 6 '15 at 14:42
  • Sorry @travisw , try to use the DECIMAL function instead. I've updated my answer – Rick Buczynski Jul 6 '15 at 14:46
  • @travisw, I also updated my answer to use number_format in the application-level example. – Rick Buczynski Jul 6 '15 at 14:50
  • Using DECIMAL (still in the SQL level) instead of ROUND returns a syntax error from Magento. SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; – travisw Jul 6 '15 at 14:52
  • Application level worked perfectly after I set up a Renderer. Thanks! – travisw Jul 6 '15 at 15:01
2

try adding 'type' => 'number',

...
$this->addColumn('weight', array(
        'header'    => Mage::helper('sales')->__('Weight'),
        'index'     => 'weight',
        'type'      => 'number', //This Line
        'filter_index' => 'sfo.weight',
        'width' => '50px',
));
...
1

In database, weight attribute have backend_type set to decimal.

To change the decimal precision of attributes that have a backend_type set to decimal,

You have to change the type of value in this table :

catalog_product_entity_decimal

If you have not change yet, you should see :

decimal(12,4)

In your case, you have to replace with

decimal(12,2)

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.