Php – Ajax in magento (load product view block)

ajaxmagentoPHP

What I want to achieve:
Clicking on a product link/image (at least in certain areas) to open a pop-up with the full product information (basically all the contents of the product view page).

What I did/tried so far:

  • created all the stuff outside the ajax php code (the module, links, templates, rewrites)
  • created the ajax controller (which can be accessed with a link similar to: http://test.com/index.php/ajaxproductview/ajax/index/id/2 ).
  • to follow various tutorials ( like this or this ) – that helped me get this far. But I don't want to load my custom block, I want the default product view block(s).
  • tried to add some code in the indexAction(). It gets there, but the code fails. I don't get any errors/notices/reports, just what it seems like an infinite loop that kills my processor.

    $body = $this
        ->getLayout()
        ->createBlock('product.info') // taken from catalog.xml
        ->toHtml();
    $this->getResponse()->setBody($body);
    

All the other pages work fine, and it's a fresh magento with only magneto and my module installed and activated.

My AJAX function simply gets this HTML response, puts it into a div, and opens a pop-up.

My question(s) is(are) – how can I set the product id, so the block knows what product to load, and how can I load this block correctly. I also tried something similar to this:

Thank you.

PS: I also tried this:

    $layout = $this->getLayout();
    $update = $layout->getUpdate();
    $update->load('catalog_product_view');
    $layout->generateXml();
    $layout->generateBlocks();
    $output = $layout->getOutput(); // $output is an empty string

Best Answer

The Product controller uses a helper to set the active product. You should be able to do the same in your controller!

Try this before you do your layouting:

$productId  = (int) $this->getRequest()->getParam('id');
Mage::helper('catalog/product')->initProduct($productId, $this);

Another thing to be aware of: If you add a block like the product.info block. It needs additional child blocks if it calls them in its template file.

It would be easiest to use a custom layout xml file. You can then add a specific layout for your action handle (your action handle consists of your routers node in your module's etc/config.xml file under <frontend><routers>, e.g. <Yourmodule> node, make sure to lowercase it! And then with underscores add the controller name and action name, in your case index_index) like this:

<yourmodule_index_index>
    <remove name="right"/>
    <remove name="left"/>
    <block type="catalog/product_view" name="root" output="toHtml" template="catalog/product/view.phtml">
    <!-- Add all the child blocks you need -->
    </block>
</yourmodule_index_index>

This makes the view.phtml the root block which renders itself using its toHtml method. Therefore, in your controller action, all you need is my two lines above and then:

$this->loadLayout();
$this->renderLayout();