2

Anyone know how to set offset in Magento products collection? I need to get 11 products on first collection page than on other pages I want to have 8 products, and I must know how many pages by 8 (with offset of 11) I have.

4
  • setPageSize() method for setting number of items you want to display in one page.Mage::getModel('catalog/product')->getCollection() ->setPageSize(10);
    – Vishal
    Sep 5, 2014 at 9:34
  • So you want the first page to display 11 products and all other pages to display 8 products? Sep 5, 2014 at 9:37
  • @JonathanHussey yes, first page 11 product all other 8 products.
    – kuba_ceg
    Sep 5, 2014 at 9:38
  • $collection = Mage::getModel('catalog/product')->getCollection(); $collection->setPage(0,11); // will bring back the first 11 products $collection->setPage(11,8); // will bring back 8 products after the 11th product
    – Vishal
    Sep 5, 2014 at 9:43

1 Answer 1

6

With a different number of products between the first page and other pages, you are probably going to find it's not entirely straightforward dealing the paging. When dealing with collections I like to fall back to the underlying Zend methods by pulling the select object from the collection:

$select = $collection->getSelect();

you can then run any Zend_Db_Select method to build the collection for each page including the limit() which allows you set a limit on returned results and an offset, so you would need to use:

$select->limit(11);

for the first page:

$select->limit(8, 11);

for the second page:

$select->limit(8, 19);

for the third page and so on. You will need to build some custom logic to calculate the offset for each page. Note that the page is not stored in the session so if there is no p $_GET argument you are on page 1, otherwise the argument indicates the current page.

Depending on how you are doing things you may need to use the reset() method on the $select object to reset the limit/offset before you set your own. It's all on the Zend page linked above anyway.

As a final note, I would suggest only going down this road if you really have to, having different numbers of products on different category pages doesn't strike me as a great idea. There's always a better way to deal with a situation than going down a rather hacky route like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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