2

I need to change the order grid in the admin area, and add in a column for Margin (ie, total margin on each order). I have set up code\local\MageWorx\Adminhtml\Block\Orderspro\Sales\Order\Grid.php with

$this->addColumn('margin', array(
    'renderer'  => 'mageworx/orderspro_sales_order_grid_renderer_margin',
    'type'  => 'currency',
    'currency' => 'order_currency_code',
    'header' => $helper->__('Margin'),
    'index' => 'total_margin'
));

Then I have Sales\Order\Grid\Renderer\Margin.php which has the following:

class MageWorx_Adminhtml_Block_OrdersPro_Sales_Order_Grid_Renderer_Margin extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {
        $orderItems = Mage::getModel('sales/order')->getCollection();
        $totalMargin = 0;

        foreach ($orderItems as $item) {
            $totalMargin += $totalMargin->getMarginR();
        }
        return $totalMargin;
    }

    public function getMarginR()
    {
        //each individual margin
        $x = 0;
        $x = ($this->getPrice() * $this->getqty_ordered()) - ($this->getData(mage::helper('purchase/MagentoVersionCompatibility')->getSalesOrderItemCostColumnName()) * $this->getqty_ordered());

        return $x;
    }
}

This just gives me a blank page.

If I take out the foreach loop and just have something like this:

$orderItems = Mage::getModel('sales/order')->getCollection();
$totalMargin = "hi";

//foreach ($orderItems as $item) {
//    $totalMargin += $totalMargin->getMarginR();
//    $totalMargin = 1;
//}
return $totalMargin;

I get "hi" in each field, so I think the getCollection bit is working, I think the problem is in the foreach. Even if I take out the

$totalMargin += $totalMargin->getMarginR();

line and just try to loop and do nothing of consequence it just gives a blank page.

Fairly new at this but what am I missing? Thanks. Could it be that the collection is empty, am I calling it wrong?

var/log/system.log has nothing.

Siarhey Uchukhlebau
  • 15,957
  • 11
  • 54
  • 83
hawthorn
  • 25
  • 6
  • let me get this straight. You want a column in your orders grid that will show a value that is the sum of a field from all orders? Wouldn't you get the same value for all rows? – Marius Jun 10 '16 at 13:58
  • No - I want the margin total for each individual order. So in each order it should loop through the items in the order, add the individual margins together and show on the order grid. – hawthorn Jun 10 '16 at 14:06
  • 1
    http://magento.stackexchange.com/questions/428/fundamentals-for-debugging-a-magento-store – Fabian Blechschmidt Jun 10 '16 at 14:16
  • The foreach should be like this I think - I had the line wrong. Still doesn't work though. Blank screen with above code. Normal order grid but with blank fields with Marius's answer below foreach ($orderItems as $item) { $totalMargin += $item->getMarginR();
    }
    – hawthorn Jun 10 '16 at 14:53

2 Answers2

1

Try this:

public function render(Varien_Object $row)
{
    $orderItems = Mage::getModel('sales/order')->getCollection();
    $totalMargin = 0;

    foreach ($orderItems as $item) {
        $totalMargin += $this->getMarginR(); //since your getMarginR() function is in same class, use $this to call it          
    }
    return $totalMargin;
}

public function getMarginR()
{
    //each individual margin
    $x = 0;
    $x = ($this->getPrice() * $this->getqty_ordered()) - ($this->getData(mage::helper('purchase/MagentoVersionCompatibility')->getSalesOrderItemCostColumnName()) * $this->getqty_ordered());

    return $x;
}

Since your getMarginR() function is in same class, use $this to call it.

Moreover

I don't see any logic going to the loop there, I think you are trying to get price of each $item. In that case you would want to pass a parameter to getMarginR() function like this:

public function render(Varien_Object $row)
{
    $orderItems = Mage::getModel('sales/order')->getCollection();
    $totalMargin = 0;

    foreach ($orderItems as $item) {
        $totalMargin += $this->getMarginR($item); //since your getMarginR() function is in same class, use $this to call it          
    }
    return $totalMargin;
}

public function getMarginR($item)
{
    //each individual margin
    $x = 0;
    $x = ($item->getPrice() * $item->getQtyOrdered()) - ($this->getData(Mage::helper('purchase/MagentoVersionCompatibility')->getSalesOrderItemCostColumnName()) * $item->getQtyOrdered()); //here comes the loop logic in my view
    ////
    // are you sure Mage::helper('purchase/MagentoVersionCompatibility') is pointing to your right helper class? how does your helper class looks? 
    //If helper file is in this tree format `Magento/Version/Compatibility.php` then it should be called as follow: Mage::helper('purchase/magento_version_compatibility')

    return $x;
}
Siarhey Uchukhlebau
  • 15,957
  • 11
  • 54
  • 83
Adarsh Khatri
  • 8,360
  • 2
  • 25
  • 58
  • Thank you Adarsh - that has got things moving anyway. Especially the second part seems to have cracked it. I now have numbers appearing in the fields. They are not the correct numbers :) but am sure it's just the logic in the getMarginR so hopefully I can figure it out from here, thanks. Seems I need to do some research on the whole $this thing. – hawthorn Jun 10 '16 at 15:14
  • Not sure about the MagentoVersionCompatibility bit - this is a site I have inherited. Every few months I have to do a few bits on it but Magento is not really my thing. I nicked that code from a margin tab on the individual order screen so I will have to look at it again. Thanks for the pointer though it might come in useful. – hawthorn Jun 10 '16 at 15:17
  • Happy learning. Cheers – Adarsh Khatri Jun 10 '16 at 15:19
0

You can try this:

public function render(Varien_Object $row) {
    $orderItems = $row->getAllItems();
    $totalMargin = 0;

    foreach ($orderItems as $item) {
        $totalMargin += $totalMargin->getMarginR();
    }

    return $totalMargin;
}
Siarhey Uchukhlebau
  • 15,957
  • 11
  • 54
  • 83
Marius
  • 197,939
  • 53
  • 422
  • 830
  • Thanks - tried this and got Fatal error: Call to a member function getMarginR() on a non-object in /home/independ/public_html_test/app/code/local/MageWorx/Adminhtml/Block/Orderspro/Sales/Order/Grid/Renderer/Margin.php on line 48 – hawthorn Jun 10 '16 at 14:25
  • Changed foreach line to ' $totalMargin += $item->getMarginR();' No errors but blank fields – hawthorn Jun 10 '16 at 14:54