Magento – Admin Grid – Assign CSS class to new orders

adminhtmlgridgrid-serlizationorders

I'd like to simply add a new-order class to an order row (or field, which seems simpler) if an order was placed in the last day.

For testing, I haven't moved anything to an overridden class, I'm simply working directly in the app/code/core/Mage/Adminhtml/Widget/Block/Grid/Column/Renderer/Longtext.php class just to get it working.

I have it basically set up how I like, but my logic is applying the class to all rows instead of just those I specify. I feel like I'm missing something simple. Here's some code:

class Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Longtext extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {
        // ...native code
        if ($this->getColumn()->getId() == 'real_order_id') {
            // realized this is available already with $row->getData()
            // $order = Mage::getModel('sales/order')->loadByIncrementId($text);

            $yesterday = strtotime("-1 day", Mage::getModel('core/date')->gmtTimestamp());
            $yesterday = Mage::getModel('core/date')->date(null, $yesterday);

            if ($row->getCreatedAt() > $yesterday) {
                $this->getColumn()->setColumnCssClass('new-order');
            };
        }

        return $text;
    }
}

When I make the call to setColumnCssClass() from here, it sets the data properly in the getData() object, but it is adding it for all rows; however, it is not actually applying that CSS class to the column.

Best Answer

You should do it in the grid template instead. You can add a new id to the order's row 'tr' . As an example, we'll highlight the orders placed in the last 24 hours.

Go to app/design/adminhtml/default/default/template/widget/grid.phtml. Starting in line 154:

...
<?php foreach ($this->getCollection() as $_index=>$_item): 
        $orderCreateAt = strtotime($_item->getCreatedAt());
        $last24hours = strtotime('24 hours ago'); ?>
        <tr id ="
        <?php if($orderCreateAt > $last24hours)
            { echo 'new-order'; }
        ?>"
        title="<?php echo $this->getRowUrl($_item) ?>"<?php if ($_class = $this->getRowClass($_item)):?> class="<?php echo $_class; ?>"<?php endif;?> >
...

Now defying the style in css should be straightforward. Go to skin/adminhtml/default/default/boxes.css and add the style you'd like to have

.grid table tr#new-order{
    background-color: #FF0000;
}