問題描述
我想要達到的目標:點擊產(chǎn)品鏈接/圖片(至少在某些區(qū)域)以打開包含完整產(chǎn)品信息(基本上是產(chǎn)品查看頁面的所有內(nèi)容)的彈出窗口.
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:
- 創(chuàng)建了 ajax php 代碼之外的所有內(nèi)容(模塊、鏈接、模板、重寫)
- 創(chuàng)建了 ajax 控制器(可以通過類似于以下的鏈接訪問:
http://test.com/index.php/ajaxproductview/ajax/index/id/2
).莉> - 遵循各種教程(如this 或 this ) - 幫助我走到這一步.但我不想加載我的自定義塊,我想要默認的產(chǎn)品視圖塊.
嘗試在 indexAction() 中添加一些代碼.它到達那里,但代碼失敗.我沒有收到任何錯誤/通知/報告,只是看起來像是殺死我的處理器的無限循環(huán).
- 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 函數(shù)只是獲取這個 HTML 響應(yīng),將它放入一個 div,然后打開一個彈出窗口.
My AJAX function simply gets this HTML response, puts it into a div, and opens a pop-up.
我的問題是(是) - 如何設(shè)置產(chǎn)品 ID,以便塊知道要加載的產(chǎn)品,以及如何正確加載此塊.我也嘗試過類似的東西:
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 控制器使用幫助器來設(shè)置活動產(chǎn)品.你應(yīng)該可以在你的控制器中做同樣的事情!
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 塊這樣的塊.如果它在其模板文件中調(diào)用它們,則需要額外的子塊.
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 文件中的路由器節(jié)點組成,例如 <Yourmodule>
節(jié)點,確保將其小寫!然后用下劃線添加控制器名稱和操作名稱,在您的情況下為 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 方法呈現(xiàn)自身的根塊.因此,在您的控制器操作中,您只需要上面的兩行代碼,然后:
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();
這篇關(guān)于magento 中的 Ajax(加載產(chǎn)品視圖塊)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!