Question

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.

Was it helpful?

Solution 2

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();
}

OTHER TIPS

I may be wrong, but you can't setCollection() on a quote with sales order items. It has to be populated with sales/quote model items.

I don't know what the scope of $this is in _prepareCollection() but i'm assuming since its on the cart block, its dealing with a quote.

Just a hint, you might instead of print_r($collection), try doing this...

echo "<pre>";
foreach ($collection as $item) {
    var_dump($item->debug());
}

It supplies pretty much just the important info instead of the database structure. You can also check your item type and make sure you are using the correct model for your setCollection method. You can also throw a break; in there if you want to just get the first item etc. Debugging magento objects can be tedious, and i've found that this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top