2011-09-08 24 views
2

在Magento的數量,我需要加載從分頁欄(產品集合只有排名前20位的項目分頁欄應該只有4頁,因爲它只有20個項目 - 但在我如果它具有全部產品集合,因爲magento分頁欄是用於創建無限制屬性的工具欄的全集合對象)。Magento的分頁欄限制項目

我有以下代碼

$_productCollection = Mage::getResourceModel('reports/product_collection') 
      ->addAttributeToSelect('*') 
      ->addOrderedQty() 
      ->setPageSize($limit) 
      ->setPage($p, $limit)  
      ->setOrder('ordered_qty', 'desc'); 

這正確返回20個項目。當我通過以下方法將其設置爲分頁欄時。

$magento_block = Mage::getSingleton('core/layout'); 
    $productsHtml = $magento_block->createBlock('catalog/product_list'); 
    $productsHtml ->setTemplate('catalog/product/list.phtml')->setCollection($_productCollection); 
    $pager   = $magento_block->createBlock('page/html_pager', 'catalog/product_list')->setCollection($_productCollection); 
    $productsHtml->setChild('pager', $pager); 

它採取所有的產品集合,而不是隻有20個項目創建工具欄。所以我在productcollection中添加了where條件,如下所示。

$_productCollection->getSelect()->where("order_items.qty_ordered >= 50"); 

此集合返回出售超過50個數量的物品。但是,我想要的是正確的分頁欄20個項目。

如何在分頁欄中設置限制。在Magento模板 上午裝載該產品集合Magento企業1.9版本外,不與:

下也不會爲我工作

$magento_block->createBlock('page/html_pager', 'catalog/product_list')->setCollection($_productCollection)->setLimit(20); 

注意。有人可以在這裏找到錯誤嗎?

回答

0

我不確定(在我看來,它不會在第二個代碼中加載$ _productCollection),但我猜你已經忘記加載集合。

$model = Mage::getResourceModel('reports/product_collection'); 
$collection = $model->getCollection(); 
$collection->AddAttributeToSelect(*); 
$collection->AddOrderedQty(); 
$collection->SetPageSize($limit); 
$collection->load();