Vlad Preda
Vlad Preda

Reputation: 9910

Ajax in magento (load product view block)

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:

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

Upvotes: 2

Views: 13211

Answers (1)

mpaepper
mpaepper

Reputation: 4022

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

Upvotes: 8

Related Questions