Essential Prestashop Functions – Day 4

Prestashop has lots of time-saving functions that we can use when developing modules or extensions. Let’s see how to get various products list and deal with cookies, in this fourth batch.

NOTICE: Values with “=” in the declaration are optional.

Getting products of a Category


Category::getProducts($id_lang, $p, $n, $order_by = null, $order_way = null, $get_total = false, $active = true, $random = false, $random_number_products = 1, $check_access = true, Context $context = null);

This is the preferred method to get products from any category in Prestashop. It’s used not only in categories pages, but by the homefeatured module, as well as others.

Example Usage


$category = new Category(5);

// Retrieves the first 15 products of a category
$products = $category->getProducts($this->context->language->id, 1, 15);

// Retrieves the first 15 products of a category, returning the number of total products for that category as well. Ordered by price, lowest to highest
$products = $category->getProducts($this->context->language->id, 1, 15, 'price', 'asc', true);


// Gets a random number of products, in random order (yes, even if we specified it)
$products = $category->getProducts($this->context->language->id, 1, 15, 'price', 'asc', false, true);


Getting new products


Product::getNewProducts($id_lang, $page_number = 0, $nb_products = 10, $count = false, $order_by = null, $order_way = null, Context $context = null);

This method can be used statically, without having to instantiate the product class.

Example Usage


$newProducts = Product::getNewProducts((int) $this->context->language->id, 0, 10);

Getting discounted products


Product::getPricesDrop($id_lang, $page_number = 0, $nb_products = 10, $count = false,
		$order_by = null, $order_way = null, $beginning = false, $ending = false, Context $context = null);

This method can also be used statically, without having to instantiate the product class.

Example Usage



// get the last 10 prices drop, ordered by position
$price_drops = Product::getPricesDrop((int) $this->context->language->id, 0, 10);

// get the last 10 prices drops, ordered by price, but only where the offer's start date is after july 23, 2015
$price_drops =  Product::getPricesDrop((int) $this->context->language->id, 0, 10, false,
		'price', 'asc', '2015-7-23 10:45:32');

Getting Best Sales


ProductSale::getBestSales($id_lang, $page_number = 0, $nb_products = 10, $order_by = null, $order_way = null)

This method can also be used statically, without having to instantiate the product class.

Example Usage


// get 10 best selling products, ordered by best selling
$products = ProductSale::getBestSales($this->context->language->id, 0, 10);

Getting Best Sales (Light)


ProductSale::getBestSalesLight($id_lang, $page_number = 0, $nb_products = 10, Context $context = null)

Lighter and quicker version of the above method, which only retrieves strictly necessary information (not recommended to display products in the product-list template)

Example Usage


// get 5 best selling products, ordered by best selling, called from a hook method
$products = ProductSale::getBestSalesLight((int)$params['cookie']->id_lang, 0, 5))

Adding variables to the cookie object


Cookie::__set($key, $value);

Useful to store dynamic information through pages (like user preferences for the current login session)

Example Usage


// you might want to trigger this when a user clicks on the cookie loaw banner (where any), so that you are not prompting him to accept it on every page
$this->context->cookie->__set('cookielawaccepted', 1);

// The value can hold strings as well (arrays can be saved by serializing them)
$this->context->cookie->__set('mycustomoptionmode', 'test');


Checking cookie variables


Cookie::__get($key);

Gets the given key’s value from the cookie object.

Example Usage


// Following up the previous example, you can use this to avoid prompting the user again
if(!$this->context->cookie->__get('cookielawaccepted'))
{
	// display the message 
}

Clearing a cookie variable


Cookie::__unset($key);

Removes the given key from the cookie object.

Example Usage


// removes the cookie law entry,
$this->context->cookie->__unset('cookielawaccepted')

Logging a user out


Customer::logout();

Example Usage


// Clears all of the current session data for this customer
$this->context->customer->logout();

Additional Resources

You like the tuts and want to say "thank you"? Well, you can always feel free to donate:

  • Kon Rad

    Hi. Im looking for a hint how to show only products with the same name in homefeatured module? Like only “Ipod” like Ipod nano, IPOD shuffle, IPod mini etc. Thanks!

  • moy2010

    Hi, Nemo. What would be the syntax to check only if a product is considered as new?

    I’m trying the following:

    (Product::getNewProducts($product) > 0) ? 1 : 0;

    But it’s not working >.<

    • NemoPS

      That method is only used to get a list of new products. There is another method I could have added: Product::isNew. Use it with a product instance like if($product->isNew()) do something

      • moy2010

        Awesome, @NemoPS :D. $product->isNew(); did the trick!

You like the tuts and want to say "thank you"? Well, you can always feel free to donate: