2014-05-14 93 views
0

我有一個響應Ajax請求的模塊。我試圖讓它呈現多個產品並獲得最終的HTML。我的控制器代碼如下。爲了測試目的,我硬編碼了這些ID。在Magento的同一頁面上顯示多個產品

$ id = 52986;

foreach ($ids as $id) { 
    Mage::helper('catalog/product')->initProduct($id, $this); 
    $this->loadLayout(); 
    $output[] = $this->getLayout()->getOutput(); 
    Mage::unregister('current_product'); 
    Mage::unregister('product'); 
} 
print_r($output); 

我假設在foreach中的每個產品渲染都會獨立渲染每個渲染(即爲每個渲染創建一個'new'佈局)。顯然,我不完全瞭解佈局系統的工作原理。因此我有兩個問題。

1)我怎樣才能得到每個產品的呈現HTML?

2)爲什麼不按照我期望的方式渲染布局?

有關相關的信息,這是我使用

<?xml version="1.0"?> 
<layout version="0.1.0"> 
    <shopthelook_ajax_index> 
     <update handle="catalog_product_view" /> 
      <remove name="html_calendar" /> 
      <reference name="root" output="toHtml"> 
        <action method="setTemplate"><template>shopthelook/wrapper.phtml</template></action> 
      </reference> 
      <reference name="product.info"> 
        <action method="setTemplate"><template>catalog/product/view.phtml</template></action> 
      </reference> 
      <reference name="product.info.media"> 
        <action method="setTemplate"><template>catalog/product/view/media.phtml</template></action> 
      </reference> 
      <reference name="product.info.options.configurable"> 
        <action method="setTemplate"><template>catalog/product/view/type/options/configurable.phtml</template></action> 
      </reference> 
    </shopthelook_ajax_index> 
</layout> 

回答

0

首先,更新您的佈局XML文件的佈局XML:

<?xml version="1.0"?> 
<layout> 
    <shopthelook_ajax_index> 
     <remove name="right"/> 
     <remove name="left"/> 
     <reference name="content"> 
      <block type="shopthelook_ajax/response" template="shopthelook_ajax/response.phtml" /> 
     </reference> 
    </shopthelook_ajax_index> 
</layout> 

它主要是告訴Magento的添加新的輸出(塊)到您的控制器。現在,你需要在你的模塊(應用程序/代碼/本地/ Shopthelook /阿賈克斯/座/ Response.php)來創建此塊類:

class Shopthelook_Ajax_Block_Response 
extends Mage_Core_Block_Template 
{ 
    public function getCollection() 
    { 
     $productIds = array(52986, 52987, 52988); 
     $collection = Mage::getModel('catalog/product')->getCollection(); 
     $collection->addAttributeToFilter('entity_id', array('in' => $productIds)); 
     $collection->addAttributeToSelect('*'); 
     return $collection; 
    } 
} 

,並創建一個模板文件(應用程序/設計/前端/基/缺省的/模板/ shopthelook_ajax/response.phtml:

<?php $_collection = $this->getCollection(); ?> 
<ul class="products"> 
<?php foreach ($_collection as $_product) { ?> 
    <li> 
     <span class="product-name"><?php echo $_product->getName() ?></span> 
     ... 
    </li> 
<?php } ?> 
</ul> 

在此之後只需要更新您的控制器來呈現其佈局:

public function ajaxAction() 
{ 
    $this->loadLayout()->renderLayout(); 
} 

行動將加載合併佈局的XML文件,並找到這取決於你的手柄米模塊/控制器/操作名稱(在您的案例shopthelook_ajax_index)。然後它會使用選定的模板創建和渲染您的區塊。在模板中調用getCollection方法,該方法創建所選產品的集合。

不確定代碼是否100%正確,因爲此刻我無法測試它,但架構&原則應該沒問題。

+0

這讓我更加接近!我真的需要使用product.info塊,但整個目錄模塊的構建是一次處理一個產品。基本上我想要做的是加載佈局,呈現HTML並重復。 – callmetwan

+0

catalog.product(catalog/product_view)塊從全局註冊表(Mage :: registry('product'))加載產品模型。要在一個頁面上多次渲染塊,您需要將產品模型註冊到全局註冊表中,並在渲染該塊之後取消註冊並再次註冊其他產品模型。但我絕對不能向你推薦這種方法,因爲product_view塊做了超出僅僅呈現產品數據範圍的其他事情(例如它設置頁面標題和元信息)。更好的方法是創建自定義塊來呈現產品數據。 –

相關問題