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

[php]

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

[/php]

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

[php]

$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);

[/php]

Getting new products

[php]

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

[/php]

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

Example Usage

[php]

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

[/php]

Getting discounted products

[php]

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

[/php]

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

[/php]

Getting Best Sales

[php]

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

[/php]

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

Example Usage

[php]

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

[/php]

Getting Best Sales (Light)

[php]

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

[/php]

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
}

[/php]

Clearing a cookie variable

[php]

Cookie::__unset($key);

[/php]

Removes the given key from the cookie object.

Example Usage

[php]

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

[/php]

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!

Looking for quality PrestaShop hosting? Check out Arvixe Web Solutions.

Tags: , , , , | Posted under PrestaShop | RSS 2.0

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

Leave a Reply

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