2

We have a few categories which function like "bestsellers" categories; these categories can hold any number of products, but I'd like to show only the top four.

So I figured to create an observer with the catalog_block_product_list_collection event and try to limit it. The observer triggers fine on the category pages, but whatever I try the collection isn't limited;

    $limitedCollection = $observer->getEvent()->getCollection();
    $limitedCollection->getSelect()->limit(4);
    return $this;

Or

    $limitedCollection = $observer->getEvent()->getCollection();
    $limitedCollection->setPageSize(4)->setCurPage(1);
    return $this;

Neither have any effect. What am I missing? Edit: It's also important that the search/layered navigation etc functionality sees only 4 products.

1
public function _loadEntities($printQuery = false, $logQuery = false)
{
    if ($this->_pageSize) {
    $this->getSelect()->limitPage($this->getCurPage(), $this->_pageSize);
}

You can prevent this by setting _pageSize to 0:

$productCollection->setPageSize(0);
$productCollection->getSelect()->limit(20,22);

Besides that, resetting the already loaded collection to load it again is not a good idea.

You can write an observer for catalog_product_collection_load_before to manipulate the limit right before the collection is loaded:

public function setProductPaging(Varien_Event_Observer $observer) 
{
    $collection = $observer->getCollection();
    if ($collection->getCurPage() == 1) {
        $collection->setPageSize(22);
    } else {
        $productCollection->setPageSize(0);
        $productCollection->getSelect()->limit(20, 22 + 20 * ($collection->getCurPage() - 2));
    }
}

reference link: Show different number of products in category page

Thanks

  • Are you sure your answer is based on Magento 2.3? As far as I can tell there is no catalog_product_collection_load_before event anymore. And the reference you gave is for Magento 1. – Alex Timmer Oct 18 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.