magento限制产品集合调用中返回的项目数

thr*_*801 11 php collections pagination magento

我试图在list.phtml模板的副本中手动限制返回结果的数量,但它变得比我预期的更难.

我已经尝试手动设置集合大小,但你再一次没有任何工作.有人能告诉我怎么做吗?非常感谢!

clo*_*eek 21

一种快速的方法是我最近发现的这种方法.您甚至可以直接在模板中使用它.

$_productCollection = clone $this->getLoadedProductCollection();
$_productCollection->clear()
                   ->setPageSize(3)
                   ->load();
Run Code Online (Sandbox Code Playgroud)


Jon*_*Day 7

与@joseph类似的方法是覆盖,Mage_Catalog_Block_Product_List但在新类中插入以下代码:

const PAGE_SIZE = 3;

protected function _getProductCollection(){
    $collection = parent::_getProductCollection();
    $yourCustomBoolean = someFunctionThatDetectsYourCustomPage();
    if($yourCustomBoolean) {
        $collection->setPageSize(self::PAGE_SIZE);
    }
    return $collection;
}
Run Code Online (Sandbox Code Playgroud)

这样,您将从父块继承Mage_Catalog代码中的任何未来更改,但仍设置页面限制.

理想情况下,您将使用system.xml节点创建一个可由管理员编辑的字段,而无需对page_size进行硬编码.xml看起来像这样:

<config>
<sections>
    <catalog>
        <groups>
            <frontend>
                <fields>
                    <custom_page_size translate="label">
                        <label>Page Size for Custom Page Type</label>
                        <frontend_type>text</frontend_type>
                        <sort_order>9999</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>1</show_in_store>
                    </custom_page_size>
                </fields>
            </frontend>
        </groups>
    </catalog>
</sections>
</config>
Run Code Online (Sandbox Code Playgroud)

然后使用以下代码检索代码中的值:

$page_size = Mage::getStoreConfig('catalog/frontend/custom_page_size');
Run Code Online (Sandbox Code Playgroud)

HTH,
JD


Ale*_*chi 5

不幸的是它不起作用,因为在_getProductCollection()方法中,已经使用页面大小初始化了Collection.

更灵活的解决方案可以是观察catalog_product_collection_load_before事件,顾名思义,在加载集合之前调度该事件.

下面是一个例子(假设在下面写一个yourmodule扩展名yourpackage):

第1步:在config.xml中定义观察者

global您的config.xml扩展文件的部分插入如下内容:

<events>
  <catalog_product_collection_load_before>
    <observers>
      <yourpackage_yourmodule_catalog_observer>
        <type>singleton</type>
        <class>yourpackage_yourmodule/catalog_observer</class>
        <method>limitPageSize</method>
      </yourpackage_yourmodule_catalog_observer>
    </observers>
  </catalog_product_collection_load_before>
</events>    
Run Code Online (Sandbox Code Playgroud)

第2步Model\Catalog:在文件夹下定义Observer类:

<?php
class Yourpackage_Yourmodule_Model_Catalog_Observer
{
  public function limitPageSize($observer)
  {
    #TODO: Insert the logic you need to differentiate when to apply the following
    $event = $observer->getEvent();
    $collection = $event->getCollection();
    $collection->setPageSize(3);
    return $this;
  }
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.真诚的,Alessandro Ronchi


Jos*_*tey 2

看起来 list.phtml 中返回的集合已经调用了 load(),这意味着当我们到达模板时,我们已经失去了设置页面大小的机会。所以,这会变得有点混乱!

生成该集合的块是Mage_Catalog_Block_Product_List,我们可以用我们自己的类扩展它并同时重写。创建一个扩展Mage_Catalog_Block_Product_List并重写该方法的新块_getProductCollection,如下所示:

/**
 * Retrieve loaded category collection
 *
 * @return Mage_Eav_Model_Entity_Collection_Abstract
 */
protected function _getProductCollection()
{
    if (is_null($this->_productCollection)) {
        $layer = Mage::getSingleton('catalog/layer');
        /* @var $layer Mage_Catalog_Model_Layer */
        if ($this->getShowRootCategory()) {
            $this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
        }

        // if this is a product view page
        if (Mage::registry('product')) {
            // get collection of categories this product is associated with
            $categories = Mage::registry('product')->getCategoryCollection()
                ->setPage(1, 1)
                ->load();
            // if the product is associated with any category
            if ($categories->count()) {
                // show products from this category
                $this->setCategoryId(current($categories->getIterator()));
            }
        }

        $origCategory = null;
        if ($this->getCategoryId()) {
            $category = Mage::getModel('catalog/category')->load($this->getCategoryId());
            if ($category->getId()) {
                $origCategory = $layer->getCurrentCategory();
                $layer->setCurrentCategory($category);
            }
        }
        $this->_productCollection = $layer->getProductCollection();

        $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());

        // OUR CODE MODIFICATION ////////////////////
        $yourCustomPage = someFunctionThatDetectsYourCustomPage();
        if($yourCustomPage) {
            $this->_productCollection->setPageSize(1);
            $this->_productCollection->setCurPage(3);
            $this->_productCollection->load();
        }
        /////////////////////////////////////////////

        if ($origCategory) {
            $layer->setCurrentCategory($origCategory);
        }
    }
    return $this->_productCollection;
}
Run Code Online (Sandbox Code Playgroud)

重要的是找到某种方法来检测您是否正在使用自定义 list.phtml 页面。然后,您需要用<block type='catalog/product_list' />您的类覆盖布局中的引用,并且您应该可以开始了。

希望有帮助!

谢谢,乔