magento 中的 Ajax(加载产品视图块)

2021-12-19 00:00:00 php magento ajax

我想要达到的目标:点击产品链接/图片(至少在某些区域)以打开包含完整产品信息(基本上是产品查看页面的所有内容)的弹出窗口.

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:

  • 创建了 ajax php 代码之外的所有内容(模块、链接、模板、重写)
  • 创建了 ajax 控制器(可以通过类似于以下的链接访问:http://test.com/index.php/ajaxproductview/ajax/index/id/2).
  • 遵循各种教程(如this 或 this ) - 帮助我走到这一步.但我不想加载我的自定义块,我想要默认的产品视图块.
  • 尝试在 indexAction() 中添加一些代码.它到达那里,但代码失败.我没有收到任何错误/通知/报告,只是看起来像是杀死我的处理器的无限循环.

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

所有其他页面都运行良好,它是一个全新的 magento,仅安装并激活了magneto 和我的模块.

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

我的 AJAX 函数只是获取这个 HTML 响应,将它放入一个 div,然后打开一个弹出窗口.

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

我的问题是(是) - 如何设置产品 ID,以便块知道要加载的产品,以及如何正确加载此块.我也尝试过类似的东西:

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:

谢谢.

PS:我也试过这个:

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

推荐答案

Product 控制器使用帮助器来设置活动产品.你应该可以在你的控制器中做同样的事情!

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

还有一点需要注意:如果你添加一个像 product.info 块这样的块.如果它在其模板文件中调用它们,则需要额外的子块.

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.

使用自定义布局 xml 文件最简单.然后,您可以为您的操作句柄添加特定布局(您的操作句柄由 <frontend><routers> 下的模块 etc/config.xml 文件中的路由器节点组成,例如 <Yourmodule> 节点,确保将其小写!然后用下划线添加控制器名称和操作名称,在您的情况下为 index_index),如下所示:

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>

这使 view.phtml 成为使用其 toHtml 方法呈现自身的根块.因此,在您的控制器操作中,您只需要上面的两行代码,然后:

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

相关文章