Search code examples
phpmagentomagento-1.7adminhtml

Magento Adminhtml Grid using sales/order_item collection filtered by customer


My objective is simple. I have a module called Quotes, I have managed to get Magento to create a new quote record each time a cart is created by changing the is_active column when a quote is 'checked out'. So I have a bunch of quotes each related to a customer and i have sales/order_item rows each related to a quote. I have a page in the backend which displays a grid of all the quotes. When a quote is clicked, the edit page has two tabs, one with a Form.php showing the details of the quote. (customer name, date etc), then I have another tab which should contain a grid of all the items in that quote. It seems so simple:

$this->addTab("items_section", array(
  "label" => Mage::helper("quote")->__("Quote Items"),
  "title" => Mage::helper("quote")->__("Quote Items"),
  "content" => $this->getLayout()->createBlock("quote/adminhtml_quotes_edit_tab_cart")->toHtml(),
));

Then in my cart block I have this:

protected function _prepareCollection()
{
    $collection = Mage::getModel('sales/order_item')->getCollection();
    print_r($collection);
    $this->setCollection($collection);

    return parent::_prepareCollection();
}

I'm not even interested in loading the correct collection (by order_id) because there is a problem ro be solved here first: The print_r statement reveals the collection I specified but passing it to $this->setCollection($collection) gives me 'No records found' rendered in the grid. In typical Magento fashion there are no errors etc. I understand that the model is supposed to query the database as needed but that doesn't seem to be happening. I suppose it's time to read Mage::core files but you can imagine my frustration at such a simple tasking being so complicated so I would appreciate it if anyone who knows what's going on here can help me out. Thanks in advance.


Solution

  • Our head developer managed to help me get it working. We're still not sure why but this seemed to work

     protected function _prepareCollection()
    {
        $quoteId = $this->getRequest()->getParam('id');
    
        $quote = Mage::getModel('sales/quote')->getCollection()->addFieldToFilter('entity_id', $quoteId);
        if ($quote->getFirstItem()->getId()) {
            $collection = $quote->getFirstItem()->getItemsCollection(false);
        }
    $this->setCollection($collection);
    
        return parent::_prepareCollection();
    }