Magento分页栏限制项目数

Magento分页栏限制项目数,magento,Magento,在magento中,我只需要从带有分页栏的产品集合中加载前20项(分页栏应该只有4页,因为它只有20个项目——但在我的例子中,它有整个产品集合,因为magento分页栏在创建工具栏时使用了整个集合对象,而没有限制属性) 我有以下代码 $_productCollection = Mage::getResourceModel('reports/product_collection') ->addAttributeToSelect('*') -&g

在magento中,我只需要从带有分页栏的产品集合中加载前20项(分页栏应该只有4页,因为它只有20个项目——但在我的例子中,它有整个产品集合,因为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->getSelect()->where("order_items.qty_ordered  >= 50");
此集合返回的商品销售量超过50件。但我想要的是20件具有正确分页栏的商品

如何在分页栏中设置限制

以下内容也不适用于我

$magento_block->createBlock('page/html_pager', 'catalog/product_list')->setCollection($_productCollection)->setLimit(20);
注意: 我在Magento Enterprise 1.9版本之外加载此产品集合,而不是在Magento模板中加载。有人能找到我在这里犯的错误吗?

我不确定(在我看来,第二个代码中没有加载您的$\u productCollection),但我想您忘记加载集合了

$model = Mage::getResourceModel('reports/product_collection');
$collection = $model->getCollection();
$collection->AddAttributeToSelect(*);
$collection->AddOrderedQty();
$collection->SetPageSize($limit);
$collection->load();
我不确定(在我看来,第二个代码中没有加载$\u productCollection),但我猜您忘记加载集合了

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