Example Usage

// 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 discounted products

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

Example Usage

[php]

// 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 (Light)

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

[php]

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

[/php]

Adding variables to the cookie object

[php]

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

[/php]

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

Example Usage

[php]

// 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’);

[/php]

Checking cookie variables

[php]

Cookie::__get($key);

[/php]

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

Example Usage

[php]

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

Example Usage

Logging a user out

[php]

Customer::logout();

[/php]

Example Usage

[php]

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

[/php]

Wrapping it up

We saw quite a number of useful PrestaShop Functions today, which are enough to start coding your own, simple module. Apart from trying to remember them all, I suggest creating code snippets with some text expander, so you can quickly drop them in, without having to remember the exact syntax.
Need PrestaShop Modules? Have a look at my Prestashop Addons Store!

Author Spotlight

Fabio Porta

Fabio has been involved in web development and design since 2005, when launched his first website at the age of 16. He’s now highly skilled in both client and server side development, along with design, and since August 2012 runs a successful website about PrestaShop tutorials and Prestashop Modules called Nemo’s Post Scriptum, at http://nemops.com