Back to Top

PrestaShopCollection component in PrestaShop

Updated 23 July 2024

In this blog, we are going to learn how to use PrestaShopCollection component in PrestaShop while developing module.

PrestaShop have a PrestaShopCollection class which is a collection of ObjectModel objects, which implements common useful interfaces available in PHP such as Iterator, ArrayAccess and Countable.

use PrestaShopCollection;

$idLang = 1;
$productCollection = new PrestaShopCollection('Product', $idLang);

$rules = new PrestaShopCollection('SpecificPriceRule');

Some of the example uses of collection
Joining associated entities

  • Join manufacturer to your Product collection
  • Category joining etc
$productCollection = new PrestaShopCollection('Product'); 
$productCollection->join('manufacturer');

$productCollection = new PrestaShopCollection('Product'); 
$productCollection->join('categories', 'id_category');

$productCollection->join('categories', 'id_category', PrestaShopCollection::LEFT_JOIN); 
$productCollection->join('categories', 'id_category', PrestaShopCollection::INNER_JOIN); 
$productCollection->join('categories', 'id_category', PrestaShopCollection::LEFT_OUTER_JOIN);

$productCollection = (new PrestaShopCollection('Product')) 
            ->join('manufacturer') 
            ->where('manufacturer.name', '=', 'Manufacturer AAA');

Ordering a Collection 
Collection can be ordered in ascending or descending order, with asc or desc in the $order parameter. To order collection, use the orderBy() method as shown below

$productCollection = (new PrestaShopCollection('Product')) 
           ->orderBy('reference', 'desc');

Grouping
Collection can be grouped based on a specific field, by using groupBy() method as shown below

Searching for an experienced
Prestashop Company ?
Find out More
$productCollection = (new PrestaShopCollection('Product')) 
           ->groupBy('id_supplier');

Paginating a Collection
For retrieving a large number of items, it is necessary to use pagination techniques such as SQL’s OFFSET/LIMIT. This will help in managing collection more efficiently.

$productCollection = (new PrestaShopCollection('Product')) 
              ->setPageSize(100) // will get only 100 items 
              ->setPageNumber(2); // but from page 2

Filtering a Collection
When building the Collection, you may need to filter its content. To do so, use the where() method.

$productCollection = new PrestaShopCollection('Product');
$productCollection->where('on_sale', '=', true); 
$productCollection = new PrestaShopCollection('Product');
$productCollection->where('reference', 'LIKE', 'REF-%'); // find products with reference beginning by "REF-"

Having method
There is also having() method available, which calls where() with the parameter $method set to having.

public function having($field, $operator, $value)

You can use any PrestaShop object similarly as above example to fetch, display and manipulate.

That’s all about PrestaShopCollection component class in PrestaShop. Hope it will help you.

Concluding this blog, I hope it proves helpful to you. If you encounter any issues or have doubts about the aforementioned process, please don’t hesitate to contact us through the comment section.

Also, you can explore our PrestaShop Development Services and a large range of quality PrestaShop Modules.

For any doubt contact us at [email protected]

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home