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


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

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


// 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']);

Getting a Product’s Name


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

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

Example Usage


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

Getting a Product’s Quantity



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)

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

Example Usage


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

Getting and displaying Products Cover Image


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

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

Example Usage


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

Getting Product Features for the front office


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

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

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

Example Usage


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


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

Getting Product Categories




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

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

Example Usage


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


Additional Resources

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

  • NemoPS

    That is correct, I used the standard php way for method notations. The usable one is in the example usage

  • TheBlackSheep

    Hi Nemops

    I found a mistake on your tutorial:

    // Gets features so they can be properly displayed

    Product::getFrontFeatures($id_lang);

    Should be:

    $product->getFrontFeatures($id_lang);

Store Top Sales

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