Written by on

Have you ever wondered what a customer ID is? How does it work, and why do we need it? This article will explain all this in detail, and it will also show you how to get a customer ID in PrestaShop.

What is a customer ID in PrestaShop?

It is a unique identifier of your clients that will help you identify them easily and quickly when they make orders or place payments on your website. It's important to know how to get this value because it allows us to give our users more personalized services.

Let's say you want to reward one of your clients with a promotion, coupon or voucher, you will need to find the ID first. You can get his/her email address when having this ID number.

This user ID lets you access more information about the current user. For instance, you will be able to see all the orders of active customers. If there is no order yet, it means they have made no purchases beforehand. If the ID is 0, it means this is a guest customer.

After doing further analysis, you can see if this client is new or not, then you can send a discount voucher for future purchases if the client buys a product.

How do I get a customer ID in PrestaShop?

A simple way for merchants.

You will first have to visit the Back Office on your PrestaShop store and then login with that username and password.

Then click on the Customers menu on the left of the Admin Panel Dashboard.

This page will show a list of customer IDs. Feel free to get it.

Developers will get the ID from Context.

I know everyone needs another way to get the ID to program functions for their modules from the source code.

We will get the ID from Context:

  $id = (int)$this->context->cookie->id_customer;

or

  $id = (int)$Context::getContext()->cookie->id_customer;

We use (int) before the value to explicitly convert it to an integer. If the code returns false (null, error, etc...), it will be converted to 0.

Where would I put this code?

The above source code should be put into your PHP file in a function.

Get this value from the TPL file.

On PrestaShop 1.7, you also get and display this value from the TPL file by using the global variable user:

  {$customer.id}

With this user ID, you can get more information from your PrestaShop database for improving your PrestaShop module's function, customize your PrestaShop store and grow your eCommerce business.

Learn how we use the result from this post to generate PrestaShop Invoice by customer's name. For example, you can change the generated invoice name from the default format to IN000001_customername.pdf.