magento limiting number of returned items in product collection call

前端 未结 6 810
北荒
北荒 2021-02-04 11:10

Im trying to limit the number of returned results manually in a copy of the list.phtml template, but its turning out to be alot harder than I anticipated.

Ive tried man

相关标签:
6条回答
  • 2021-02-04 11:19

    I got the Code of User:clockworkgeek , but here is some issue and correct code is as follow, it work and thanks clockworkgeek.

    $_productCollection = $this->getLoadedProductCollection();
    $_productCollection->clear();
    $_productCollection->setPageSize(3)
    $_productCollection->load();
    

    You Also writeenter code here or solve this issue by modify as

    $this->getLoadedProductCollection()->clear();
    $_productCollection = $this->getLoadedProductCollection();
    

    Thanks, If it help you then comment.

    0 讨论(0)
  • 2021-02-04 11:25

    unfortunately it doesn't work because in the _getProductCollection() method the Collection has been already initialized with a page size.

    A more flexible solution could be that of observing the catalog_product_collection_load_before event which, as the name suggests, is dispatched before the collection is loaded.

    Here follows an example (assuming to write a yourmodule extension under yourpackage):

    STEP 1: Define your observer in config.xml

    in the global section of your config.xml extension file insert something like:

    <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>    
    

    STEP 2: Define your Observer class under the Model\Catalog folder:

    <?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;
      }
    }
    

    Hope it helps. Sincerely, Alessandro Ronchi

    0 讨论(0)
  • 2021-02-04 11:28

    A similar approach to @joseph is to override Mage_Catalog_Block_Product_List but insert the following code in your new class:

    const PAGE_SIZE = 3;
    
    protected function _getProductCollection(){
        $collection = parent::_getProductCollection();
        $yourCustomBoolean = someFunctionThatDetectsYourCustomPage();
        if($yourCustomBoolean) {
            $collection->setPageSize(self::PAGE_SIZE);
        }
        return $collection;
    }
    

    that way you will inherit any future changes in the Mage_Catalog code from the parent block but still set your page limits.

    Ideally you would use a system.xml node to create a field that can be edited by an administrator without hardcoding the page_size. The xml would look something like:

    <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>
    

    Then retrieve that value in your code with:

    $page_size = Mage::getStoreConfig('catalog/frontend/custom_page_size');
    

    HTH,
    JD

    0 讨论(0)
  • 2021-02-04 11:37

    It looks like the collection returned in list.phtml has already had load() called, which means that by the time we get to the template we've lost the opportunity to set the page size. So, this is going to get a bit messy!

    The block that generates that collection is Mage_Catalog_Block_Product_List, which we can extend with our own class and override at the same time. Create a new block that extends Mage_Catalog_Block_Product_List and override the method _getProductCollection as follows:

    /**
     * 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;
    }
    

    The important part is to find some way to detect whether you're using the custom list.phtml page or not. Then you'll need to override references to <block type='catalog/product_list' /> in the layouts with your class, and you should be set to go.

    Hope that helps!

    Thanks, Joe

    0 讨论(0)
  • 2021-02-04 11:43

    A quick way is with this method I recently discovered. You can even use it directly in the template.

    $_productCollection = clone $this->getLoadedProductCollection();
    $_productCollection->clear()
                       ->setPageSize(3)
                       ->load();
    
    0 讨论(0)
  • 2021-02-04 11:43

    As was mentioned, the productCollection already has page size set. There is another way to get the collection though; via the catalog/product Model:

    $productCollection = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToSelect(
            array('name', 'image', 'price')
        )
        ->addIdFilter( 
            array('1', '2')
        )
        ->setPageSize( 2 )
        ->load();
    ;
    
    return $productCollection->getSelect()->__toString();
    

    The resulting query (and ultimately object) contains the LIMIT syntax:

    SELECT `e`.* ... WHERE (`e`.`entity_id` IN('1', '2')) LIMIT 2;
    

    Is that what you were asking?

    0 讨论(0)
提交回复
热议问题