No results found

Try a different search query

Popular searches:

Add to Cart

Cart

You have no purchases yet

Browse Marketplace
Original Subtotal:
Product discounts:
Quantity Discount:
Total:
View Cart
Database

customer Table in OpenCart: Customers, Groups and Addresses in 3.0 and 4.1

How OpenCart 3.0 and 4.1 store customers: the customer table, groups, addresses, balance and points, registration confirmation, passwords, and the service tables for logins and activity.

8 min read OC 3.0 OC 4.1

The customer table in OpenCartA free open-source CMS for online stores, written in PHP with a MySQL/MariaDB database. Everything that is not part of the core is added through extensions. More in the glossary → stores the customer account: name, email, phone, password, group, status, and flags such as newsletter subscription. Addresses are stored separately in address, group names in customer_group_description, and balance and reward points in the customer_transaction and customer_reward journals. About a dozen other service tables record logins, IP addresses, and activity. Between 3.0 and 4.1, password storage, default addresses, and tokens changed.

Examples are for OpenCart 4.1. In 4.0.x, model triggers and event handling differ. For the overall database map, see the article OpenCart database tables.

What is in the customer table?

ColumnsPurpose
customer_idprimary key; 0 in an order means a guest
customer_group_id, store_id, language_idgroup, store, and language the customer registered with
firstname, lastname, email, telephonecontact details
passwordpassword hash (format depends on version)
newsletternewsletter consent
custom_fieldvalues of custom account fields in JSON
status, safewhether the customer can log in; a "safe" customer is not checked by anti-fraud extensions
ip, token, code, date_addedIP at registration, service tokens, date

Differences between the branches in the table itself:

  • OpenCart 3.0: has salt for the password, address_id (default address), fax, and the legacy columns cart and wishlist. The wishlist is actually stored in a separate customer_wishlist table, and the cart in cart.
  • OpenCart 4.1: the salt, address_id, fax, cart, and wishlist columns are absent; commenter was added (in the admin hint, a flag related to spam checking of comments by the anti-spam system) along with an index on email.

How are passwords and emails stored?

Password

OpenCart 3.0 (both 3.0.4.1 and 3.0.5.1) writes a random string to salt during registration, and a triple SHA1 hash with this salt to password. At login, the core still accepts the old MD5 format.

OpenCart 4.1 uses password_hash() with PHP's default algorithm. During login, the core verifies the password via password_verify(). If the hash is in an old format (SHA1 with a salt, when the salt column remains after database migration, or MD5), it rehashes it after a successful login. This means customers migrated from 3.0 gradually receive new hashes as they log into their accounts.

Email

There is no unique key on email in any branch: duplicates are prevented by the registration and admin code, which counts customers with the same email before saving. Search is case-insensitive: WHERE LOWER(email) = … in 3.0 and LCASE(`email`) in 4.1. This leads to two consequences:

  • in 4.1, the email is stored in lowercase during registration; in 3.0, it is stored as the customer entered it;
  • a function applied to the column prevents MySQL from using the index, so on a large database, login and email checks perform a full table scan even in 4.1, where the index exists.

Importing customers from another system with direct SQL easily creates duplicate emails with different letter case. Check them with a GROUP BY LOWER(email) HAVING COUNT(*) > 1 query before running the import.

How do customer groups work?

customer_group has only customer_group_id, approval, and sort_order; the name and description are in customer_group_description with the key (customer_group_id, language_id). Groups are managed in "Customers → Customer Groups" (Customers → Customer Groups).

Customers → Customer Groups: group list and registration approval flag
Customers → Customer Groups: group list and registration approval flag · OpenCart 4.1.0.4

A group affects many things at once:

  • Prices. Discounts and specials in product_discount (and product_special in 3.0) are set for a specific group; points for purchases in product_reward are as well.
  • Custom fieldsYour own fields in registration or address, for example a tax ID number or a post office branch. More in the glossary →. custom_field_customer_group determines which fields the group sees and fills in.
  • Registration approval. If approval is enabled for a group, a new customer is created with status = 0, and a request appears in customer_approval; the administrator approves it in "Customers → Customer Approvals" (Customers → Customer Approvals).
  • Taxes and payment or shipping methods, if the corresponding modules take the group into account.

The default group for new customers and the list of groups that can be selected during registration are set in "System → Settings" on the "Option" tab (Customer GroupA segment of buyers (retail, wholesale) with different prices and terms. More in the glossary → and Customer Groups fields). For guests, the core uses the default group. If you delete a group that customers are still assigned to, their prices and fields become unpredictable, so first move the customers to another group.

How are addresses stored?

address has one row per address: customer_id, firstname, lastname, company, address_1, address_2, city, postcode, country_id, zone_id, and custom_field. An index on customer_id exists in both branches.

WhatOpenCart 3.0OpenCart 4.1
Default addresscustomer.address_idaddress.default column
Country address formatcountry.address_format columnseparate address_format table; in the country, address_format_id
Country and region namesin the country and zone tablesin country_description and zone_description by language

The address in an order is a copy, not a reference: at checkoutThe order placement page. The fewer fields, the higher the conversion. More in the glossary →, the address fields are written to the order table. Changing or deleting an address in the account does not affect old orders. For Ukraine, the city and address_1 fields are often filled with the city and the carrier's branch; shipping modules usually store the branch ID in custom fields or in their own tables. For the order table, see the article order table in OpenCart.

Where are the balance, reward points, and customer history?

In OpenCart, the balance and points are not stored as a single number. They are operation journals, and the current value is the sum of the rows:

  • customer_transaction is the monetary balance (store credit): customer_id, order_id, description, amount. Negative amounts are expenses.
  • customer_reward is reward points: customer_id, order_id, description, points.

Example

SELECT c.customer_id, c.email,
       (SELECT COALESCE(SUM(ct.amount), 0) FROM oc_customer_transaction ct WHERE ct.customer_id = c.customer_id) AS balance,
       (SELECT COALESCE(SUM(cr.points), 0) FROM oc_customer_reward cr WHERE cr.customer_id = c.customer_id) AS points
FROM oc_customer c
WHERE c.customer_id = 42;

This is exactly how the core models calculate them. A bonus or cashback module that wants to change the balance should add a row to the journal, not edit the amount. A ready-made version of such a mechanism is Advanced Bonuses and Cashback.

Other service tables for the customer:

  • customer_history: manager comments in the customer record;
  • customer_ip: IP addresses of logins (in 4.1 also store_id and country);
  • customer_login: failed login attempts by email and IP to protect against brute force;
  • customer_online, customer_activity, customer_search: who is on the site, actions in the account, search queries;
  • customer_affiliate: affiliate program data (in 4.1, balance and payment_method were added);
  • customer_wishlist: wishlist items (in 4.1 the key includes store_id).

In 4.1, customer_token (codes for password reset and confirmations) and customer_authorize (login authorization records with token, IP, browser, and expiry date) were added. These tables contain personal data, so access to them from modules should be limited to the minimum required. For admin protection, see the blog: OpenCart admin security.

How to work with customers safely from a module?

Customer data is personal, and mistakes here are more costly than in the catalog. A few rules that follow from the schema:

  • Create and modify customers through the admin model customer/customer: it correctly hashes the password for its version, in 4.1 it stores the email in lowercase, and it fills in all required columns.
  • Delete through the model as well. The deleteCustomer() method of the admin model removes the account along with the related service data of the customer, while a direct DELETE from customer will leave them in the database. Check the list of tables that the method cleans up in admin/model/customer/customer.php of your release.
  • Do not copy personal data into your own tables unnecessarily. Store customer_id and take the name or email from customer at the moment of use.
  • Account for data deletion requests. In 4.1, there is the gdpr table and the "Customers → GDPR" page (Customers → GDPR, route customer/gdpr) for requests to export and delete data. If a module stores customer data separately, plan for its deletion along with the customer, for example with an event using the admin/model/customer/customer/deleteCustomer/after trigger in 3.0 or admin/model/customer/customer.deleteCustomer/after in 4.1. A trigger with an incorrect separator will silently never fire.

How to intercept customer deletion or changes with an event is described in the article event model in OpenCart.

Summary

  • customer is the account; group names are in customer_group_description, addresses are in address.
  • 3.0 stores the password as SHA1 with a salt; 4.1 uses password_hash() and rehashes old formats at login.
  • Email has no unique key and is searched case-insensitively; check for duplicates after imports yourself.
  • Default address: customer.address_id in 3.0, address.default in 4.1.
  • Balance and points are sums of the customer_transaction and customer_reward journals; change them by adding new rows.

Languages and stores in the database, and safe and slow queries, are covered in other articles in the Database section. For how to build your own module tables alongside the customer tables, see the article custom module tables in OpenCart.

FAQ

How do I find all of a customer's orders, including guest orders?
Registered orders carry the customer's customer_id, while guest orders have customer_id 0, so search for them by email. Compare emails without regard to case.
Can I delete a customer without losing their orders?
Yes, orders keep a copy of the name, email and address, so they remain after the customer account is deleted. Deleting through the admin cleans up the customer's related tables but not the orders.
Why can't a customer log in after registering?
Most often their group requires approval: the customer is created with status 0 and waits under Customers → Customer Approvals. Also check the status field in the customer's record.
Was this article helpful?
Thanks for your feedback!

Related articles

Didn't find the answer?

Ask the community: OpenCart developers answer on the forum.

Ask on the forum
We use cookies

We use cookies and similar technologies to improve your experience, analyse traffic, and show personalised ads. Read our Cookie Policy for details.