Essential Prestashop Functions – Day 3

Prestashop has lots of time-saving functions that we can use when developing modules or extensions. The third batch will be focused on product-related methods.

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

Getting a Product’s Price

[php]

// It needs an instance
Product::getPrice($tax = true, $id_product_attribute = null, $decimals = 6,
$divisor = null, $only_reduc = false, $usereduc = true, $quantity = 1)

// Static way
Product::getPriceStatic($id_product, $usetax = true, $id_product_attribute = null, $decimals = 6, $divisor = null,
$only_reduc = false, $usereduc = true, $quantity = 1, $force_associated_tax = false, $id_customer = null, $id_cart = null,
$id_address = null, &$specific_price_output = null, $with_ecotax = true, $use_group_reduction = true, Context $context = null,
$use_customer_price = true);

[/php]

Both methods can be equally used to get a product’s price. While the first needs to be ran by a product instance, the second is static and can be ran from every context, as long as you provide the product id.

Example Usage

[php]

// get the product price, after instanciating a new object

$product = new Produc(4); // instanciate a product with id = 4
// get the price, but dynamically check if it needs to apply taxes or not
$product_price = $product->getPrice(Product::$_taxCalculationMethod == PS_TAX_INC);
// get the price of a specific combination, always with taxes
$product_attribute_price = $product->getPrice(true, 77);

// get price without instanciating an object
$products = array(
0 => array(‘id_product’ => 2),
1 => array(‘id_product’ => 86),
2 => array(‘id_product’ => 12),
);

foreach($products as $key => $product)
$products[$key][‘price’] = Product::getPriceStatic($product[‘id_product’]);

[/php]

Getting a Product’s Name

[php]

Product::getProductName($id_product, $id_product_attribute = null, $id_lang = null);

[/php]

Retrieves the given product name in a single language (if not specified, the current one)

Example Usage

[php]

// Gets the name in the current language
$name = Product::getProductName(34);

// Gets the combination name in a chosen language
$name = Product::getProductName(34, 6, 2);

[/php]

Getting a Product’s Quantity

[php]

Product::getQuantity($id_product, $id_product_attribute = null, $cache_is_pack = null);

// This will consider a specific warehouse
Product::getRealQuantity($id_product, $id_product_attribute = 0, $id_warehouse = 0, $id_shop = null)

[/php]

They both return the product’s quantity, but the latter is to be preferred with advanced stock management in mind.

Example Usage

[php]

// gets the quantity of all products in the array
$products = array(
0 => array(‘id_product’ => 2),
1 => array(‘id_product’ => 86),
2 => array(‘id_product’ => 12),
);

foreach($products as $key => $product)
$products[$key][‘qty’] = Product::getQuantity($product[‘id_product’]);

// Gets quantity for a specific combination of a product (product id = 6, combination id = 99)
$quantity = Product::getQuantity(6, 99);

// Gets the quantity in stock for the specific warehouse ID
$quantity = Product::getRealQuantity(6, 0, 1);

[/php]

Getting and displaying Products Cover Image

[php]

// Returns an image ID
Product::getCover($id_product, Context $context = null);

// Uses the product rewrite and image id to get the actual image link
Link::getImageLink($name, $ids, $type = null);

[/php]

These can be used in conjunction to display the product’s cover.

Example Usage

[php]

// returns an array like array(‘id_image’ => 66)
$cover = Product::getCover(5);

if($cover) // if there is an image
{
// notice ‘ipod-nano’ is the product link_rewrite field here;
$img_link = $this->context->link->getImageLink(‘ipod-nano’, $cover[‘id_image’]); // remember the previous is an array
}

[/php]

Getting Product Features for the front office

[php]

// Gets features so they can be properly displayed
Product::getFrontFeatures($id_lang);

// The same, but static
Product::getFrontFeaturesStatic($id_lang, $id_product);

[/php]

These methods come in handy when you want to display product features with their names and values.

Example Usage

[php]

$product = new Product(10);
$features = $product->getFrontFeatures($this->context->language->id);

// static way
$features = Product::getFrontFeaturesStatic($this->context->language->id, 10);

[/php]

Getting Product Categories

[php]

// Get ids of the categories this product belongs to
Product::getCategories();

// The same, Static
Product::getProductCategories($id_product);

// Get more data about categories, including name and link_rewrite
Product::getProductCategoriesFull($id_product, $id_lang = null);

// Get all parent categories, up to the root, in a single language. It will only consider the default one as starting point
Product::getParentCategories($id_lang = null);

[/php]

They are all used to retrieve data about the product’s category association.

Example Usage

[php]

$product = new Product(10);
// $categories will be ids only
$categories = $product->getCategories();

// Using the same Object, get all parents
$parent_categories = $product->getParentCategories();

// Static way, getting more data in the current language
$categories = Product::getProductCategoriesFull(10, $this->context->language->id);

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

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 *