Admin Grid - Assign CSS class to new orders
-
16-10-2019 - |
Question
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.
Solution
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;
}
OTHER TIPS
Rather than editing the template, I added a basic module to rewrite /Adminhtml/Widget/Grid/Column/Renderer/Longtext.php
.
public function render(Varien_Object $row)
{
//...native code
// Custom stuff here. Render the order # in red if the order is less than a day old.
if ($this->getColumn()->getId() == 'real_order_id') {
$yesterday = strtotime("-24 hours", Mage::getModel('core/date')->gmtTimestamp());
$yesterday = Mage::getModel('core/date')->date(null, $yesterday);
if ($row->getCreatedAt() > $yesterday) {
$text = '<span style="color: red; font-weight: bold;">' . $text . '</span>';
};
}
return $text;
}
@osondoar got me going in the right direction. I was hoping to use the built-in methods to make this modification, but this will suffice for now. Accepting the previous answer since it got me headed in the right direction, but this method works as well.