阿贾克斯在Magento(负载的产品视图块)视图、负载、产品、阿贾克斯

2023-09-10 13:23:17 作者:吾性傲以野

我想实现什么: 点击一个产品链接/图像(至少在某些领域),打开一个弹出的完整的产品信息(产品视图页基本上所有的内容)。

我所做/试过到目前为止:

创建所有的东西之外的AJAX PHP code(模块,链接,模板,重写) 创建Ajax控制器(可以用类似链接访问: http://test.com/index.php/ajaxproductview/ajax/index/id/2 )。 要遵循各种教程(如这或这) - 即帮助我走到这一步。但我不希望加载我的自定义模块,我想默认产品视图块(S)。

尝试添加一些code在的indexAction()。它到达那里,但code失败。我没有得到任何错误/通知/报告,正是这似乎是一个无限循环,杀死我的处理器。

  $身体= $此
     - > getLayout()
     - > createBlock('product.info')//取的catalog.xml
     - > toHtml();
$这个 - > GETRESPONSE() - > setBody($机构);
 

所有其他网页正常工作,这是一个新鲜的Magento只有磁和我的模块安装和激活。

我的AJAX功能只是得到这个HTML响应,将其放入一个div,并打开一个弹出窗口。

我的问题(s)是(是) - 我怎么可以设置产品ID,所以该块知道什么产品来加载,以及如何正确加载该块。我也尝试过类似这样:

感谢你。

PS:我也尝试过这样的:

  $布局= $这个 - > getLayout();
    $更新= $布图设计> getUpdate();
    $最新情况:>负载('catalog_product_view');
    $布局 - > generateXml();
    $布局 - > generateBlocks();
    $输出= $布图设计> getOutput(); // $输出是一个空字符串
 
T20天正建筑V6.0功能概述及常见问题

解决方案

该产品控制器使用一个辅助设置活动的产品。你应该能够在你的控制器做同样的!

试试这个,你做你的布点之前:

  $ productId参数=(INT)$这个 - >调用getRequest() - > getParam('身份证');
法师::帮手(目录/产品) - > initProduct($ productId参数,$本);
 

另一件事要注意的: 如果添加像product.info块的块。它需要如果它调用它们的模板文件中的其他子块。

这将是最容易使用的自定义布局的XML文件。然后,您可以添加特定的布局你的动作手柄(您的操作手柄包括在你的模块等/ config.xml文件中的路由器节点下的<前端><路由器> ,例如< Yourmodule> 节点,确保为小写字母,然后用下划线添加控制器名称和动作名称,你的情况index_index)这样的:

 < yourmodule_index_index>
    <删除名称=右/>
    <删除名称=左/>
    <块类型=目录/ product_viewNAME =根输出=toHtml模板=目录/产品/ view.phtml>
    <! - 添加你需要的所有子模块 - >
    < /块GT;
< / yourmodule_index_index>
 

这使得view.phtml使用其toHtml方法,使自己的根块。 因此,在你的控制器动作,你需要的是我的上面两行,然后:

  $这个 - > loadLayout();
$这个 - > renderLayout();
 

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

解决方案

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