0

Creating a custom module that has a grid in admin. I'm trying to assign the value of a function to be the column value. The hard part is the $columnid as what I want doesn't have an attribute in the db.

Here's my add column section:

   $this->addColumn(
        '',
        array(
            'header' => Mage::helper('catalog')->__('Amount Sold (Monthly)'),
            'index'  => $this->getSalesOverMonth()
        )
    );

and my getSalesOverMonth function:

public function getSalesOverMonth() {
    $from = date('Y-m-d', strtotime('first day of this month'));
    $to   = date('Y-m-d');

    $sku = $this->getSku();

    $qty = Mage::getResourceModel('reports/product_collection')
        ->addOrderedQty($from, $to)
        ->addAttributeToFilter('sku', $sku)
        ->getFirstItem();

    return (int) $qty;
}

that function does actually return the correct value but I can't plug it into the table

any idea where I'm going wrong?

Thanks

1 Answer 1

0

The Answer I found was to create a custom column renderer that takes the function data and returns it

example of my one for anyone looking for a solution to their problem:

// app/code/local/Company/Module/Block/Adminhtml/Render/Column.php
class Company_Module_Block_Adminhtml_Render_Column extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {
    public function render(Varien_Object $row) {
        $block   = Mage::getBlockSingleton('company_module/adminhtml_grid');
        $monthly = $block->getSalesOverMonth($row->getData('sku'));
        $weekly  = $monthly / 4;

        if ($monthly == 0) {
            return '0';
        } else {
            return $weekly;
        }
    }
}

Main Block File:

// app/code/local/Company/Module/Block/Grid.php
$this->addColumn(
    'columnid',
     array(
         'header'   => Mage::helper('catalog')->__('Table Header'),
         'index'    => 'field_id',
         'renderer' => 'Company_Module_Block_Adminhtml_Render_Column'
     )
);

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.