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

OpenCart Database Tables: Schema and Key Tables in 3.0 and 4.1

OpenCart 3.0 and 4.1 Database Map: How Many Tables There Are, How They Are Grouped, Which Naming Patterns Repeat, and What Changed in the 4.1 Schema for Module Developers.

8 min read OC 3.0 OC 4.1

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 → database tables consist of several repeating patterns: the main entity table (product, category), its translations (_description with language_id), store binding (_to_store), and page layout binding (_to_layout). Once you understand these patterns, you can quickly read any part of the schema. In 3.0.5.1 there are 134 tables, in 4.1.0.4 there are 159, and some key tables in 4.1 have changed structure.

Examples are for OpenCart 4.1; in 4.0.x, model triggers and event handling differ. For where the code lives for the models that work with these tables, see the articles OpenCart model and $this->db and OpenCart file structure.

Where is the schema described, and how do the versions differ overall?

The source of truth for the schema is different in each branch:

  • OpenCart 3.0: the install/opencart.sql file with ready-made CREATE TABLE and demo data.
  • OpenCart 4.1: a PHP array in system/helper/db_schema.php (function oc_db_schema()). The installer and upgrader build tables from it, and the demo data is stored separately in install/opencart.sql.

All tables have a prefix from the DB_PREFIX constant in config.php; by default it is oc_, but on a specific server it may be different, so in module code always write DB_PREFIX . "product". For configs, see the article config.php in OpenCart.

ReleaseTablesEngineCore table encoding
3.0.4.1136134 MyISAM, 2 InnoDButf8 / utf8_general_ci
3.0.5.1134132 MyISAM, 2 InnoDButf8mb4 / utf8mb4_unicode_ci
4.1.0.4159InnoDButf8mb4 / utf8mb4_unicode_ci

Between 3.0.4.1 and 3.0.5.1, two tables disappeared: order_shipment and shipping_courier. An important consequence of the engine: in 3.0, MyISAM has neither foreign keys nor transactions. In 4.1, db_schema.php describes relationships in the foreign section, but the installer does not create foreign keys, so integrity between tables in both branches is maintained only by model code. If you delete a product with a direct SQL query, you will leave "orphans" in ten related tables.

How are the tables grouped?

OpenCart table names speak for themselves, and they are convenient to read in groups:

GroupKey tables
Catalogproduct, product_description, product_to_category, product_image, product_option, product_option_value, product_attribute, product_discount, category, category_description, category_path, manufacturer
Attributes, options, filtersattribute, attribute_group, option, option_value, filter, filter_group and their _description
Ordersorder, order_product, order_option, order_total, order_history, order_status
Customerscustomer, customer_group, address, customer_transaction, customer_reward
Localisationlanguage, currency, country, zone, geo_zone, tax_class, tax_rate
Stores and designstore, layout, layout_route, layout_module, module, theme, seo_url
Systemsetting, extension, extension_install, event, modification, session, cart, user, user_group

A separate note on several tables that modules work with most often:

  • setting — core and module settings: store_id, code, key, value, serialized. Arrays are stored as JSON with serialized = 1. For details, see the article setting model and the setting table.
  • category_path — a ready-made categoryA section of the catalog that contains products. It may have subcategories. More in the glossary → tree: for each category, rows with all its ancestors (path_id) and the level (level). Through it, the core finds all subcategories without recursion.
  • cart — the database cart, bound to session_id and customer_id.
  • event and modification — registered events and OCMOD modifications of modules.

Which patterns repeat across the entire schema?

Translations: _description and language_id

Everything shown to the customer as text is moved into tables with the suffix _description and the primary key (id сутності, language_id). For example, product_description has PRIMARY KEY (product_id, language_id) and the fields name, description, tag, meta_title, meta_description, meta_keyword. Therefore, every query for names has the filter language_id, and a module that adds a product must insert a row for each language. In 4.1 this pattern was extended to countries and regions: country_description and zone_description appeared.

Stores: _to_store and store_id 0

The tables product_to_store, category_to_store, information_to_store, manufacturer_to_store determine in which store an entity is visible. A product without a row in product_to_store for the current store will not be found on the storefrontWhat the customer sees: the home page, categories, product page, and cart. More in the glossary →. The main store has store_id = 0, and in the table store it is absent: there are only additional stores there, while the settings of the main store are in setting with store_id = 0.

Page layouts: _to_layout

product_to_layout, category_to_layout and others define which layout a specific page uses, and layout_module defines which modules are displayed in the positions of that layout.

Example

SELECT p.product_id, p.model, p.price, pd.name
FROM oc_product p
JOIN oc_product_description pd ON (pd.product_id = p.product_id AND pd.language_id = 1)
JOIN oc_product_to_store p2s ON (p2s.product_id = p.product_id AND p2s.store_id = 0)
WHERE p.status = 1
ORDER BY pd.name
LIMIT 20;

The value language_id = 1 here is for illustration only: language ids depend on the installation. In code, take it from $this->config->get('config_language_id'), and the store from config_store_id.

What changed in the OpenCart 4.1 schema?

Most 4.1 tables match 3.0 by name, but there are differences in key tables that break direct SQL queries from modules:

WhatOpenCart 3.0OpenCart 4.1
Product codescolumns sku, upc, ean, jan, isbn, mpn in producttable product_code (product_id, code, value) and the reference identifier (SKU, UPC, EAN, JAN, etc.)
Product variantsnonecolumns master_id, variant, override in product
Special pricesseparate product_specialproduct_discount rows with special = 1 and the column type
Product viewscolumn product.viewedtables product_viewed and product_report
Recurring paymentsrecurring, order_recurring, product_recurringsubscription, subscription_plan, product_subscription and others
Gift vouchersvoucher, voucher_theme, order_vouchernot in the core schema
SEO URLsseo_url.query = product_id=48seo_url.key = product_id, value = 48, plus sort_order
Payment method in orderorder.payment_method varchar(128) and payment_codeorder.payment_method text with JSON, without payment_code
Customerhas salt, cart, wishlist, address_id, faxthese columns are absent

This is what seo_url looks like in 3.0:

install/opencart.sql · OpenCart 3.0.5.1 · lines 3699–3708

CREATE TABLE `oc_seo_url` (
  `seo_url_id` int(11) NOT NULL AUTO_INCREMENT,
  `store_id` int(11) NOT NULL,
  `language_id` int(11) NOT NULL,
  `query` varchar(255) NOT NULL,
  `keyword` varchar(255) NOT NULL,
  PRIMARY KEY (`seo_url_id`),
  KEY `query` (`query`),
  KEY `keyword` (`keyword`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

In 4.1, instead of one query row there is a pair key (varchar(64)) and value (varchar(255)), and keyword has been expanded to varchar(768). A 3.0 module query of the form WHERE query = 'product_id=48' must be rewritten for 4.1 as WHERE `key` = 'product_id' AND `value` = '48'. For the table itself, see the article seo_url in OpenCart.

New table groups in 4.1: blog (article, topic with descriptions and bindings), tasks cron, startup, antispam, gdpr, tokens and authorization (customer_token, customer_authorize, user_token, user_authorize, user_login), reports (product_report, download_report, marketing_report).

Which indexes exist, and where are they missing?

Core indexes are minimal, and it is worth knowing this before writing your own queries. For comparison of several tables:

TableOpenCart 3.0OpenCart 4.1
orderprimary key onlyadditionally email and order_status_id
customerprimary key onlyadditionally email
product_descriptionnamename
product_to_categorycategory_idcategory_id
order_productorder_idorder_id

The practical conclusion: in 3.0, selecting orders by status or customers by email on a large database performs a full table scan. If a module regularly makes such queries, check the plan via EXPLAIN and, if necessary, suggest that the store owner add an index. Changing core tables from the install() module without warning is not advisable: it affects all other modules and future upgrades.

How to work with the schema safely from a module?

  • Read through core models where possible. Models already account for language, store, customer groupA segment of buyers (retail, wholesale) with different prices and terms. More in the glossary →, and status, while direct SQL easily skips one of these conditions.
  • Write through core models where possible. editProduct() or deleteProduct() update and clean up a dozen related tables; a manual DELETE FROM product won't do this, and there are no foreign keys to act as a safety net.
  • Keep your own data in your own tables with the module prefix, on InnoDB, and do not add columns to core tables. For how to design them, see the article custom module tables in OpenCart.
  • Check the schema against the version. Before a direct query, open install/opencart.sql (3.0) or system/helper/db_schema.php (4.1) and verify that the column exists there.
  • Always filter by language_id and store_id in tables where they exist, otherwise you will get duplicate rows.

Summary

  • 3.0.4.1 has 136 tables, 3.0.5.1 has 134, and 4.1.0.4 has 159; the prefix is taken from DB_PREFIX.
  • The 3.0 schema is described in install/opencart.sql, and the 4.1 schema is in system/helper/db_schema.php.
  • Patterns: _description with language_id, _to_store with store_id (the main store is 0, and it is absent from store), _to_layout for page layouts.
  • In 4.1, product codes, specials, SEO URLs, subscriptions, and the payment methodAn extension such as card or cash payment. More in the glossary → format have changed; there are no vouchers in the core schema.
  • There are no foreign keys in either branch, so write data through core models.

Tables for products, orders, and customers, working with language_id and store_id, and safe and slow queries are covered separately; see the articles in the Database section.

FAQ

How do I find the exact table prefix on my server?
Open config.php in the store root and find the DB_PREFIX constant. The same constant must be in admin/config.php.
Can I convert OpenCart 3.0 tables from MyISAM to InnoDB?
Technically yes, the core does not depend on the storage engine, but do it on a copy of the database and with a backup. Locking behavior and memory requirements differ between engines, so check your modules and the load after the change.
Where is the customer's cart content stored?
In the cart table, linked to session_id and, for a logged-in customer, also to customer_id. The core reads the products and prices for the cart from the catalog tables every time.
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.