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

product table in OpenCart: products, descriptions, categories and related tables

How a product is stored in OpenCart 3.0 and 4.1: the product table and its companions (descriptions, categories, stores, options, prices, images, codes and variants), with version differences.

8 min read OC 3.0 OC 4.1

The product table in OpenCart stores only the "technical" part of a product: model, price, quantity, weight, and status. The name and description are in product_description, categories are in product_to_category, and store visibility is in product_to_store. A dozen or so other tables hold images, options, customer-group prices, and relations. Below is where things live in 3.0 and 4.1, what conditions the storefrontWhat the customer sees: the home page, categories, product page, and cart. More in the glossary → applies, and why it is better to write products through the model.

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 stored in the product table itself?

The primary key is product_id. The main columns are the same in both branches:

ColumnsPurpose
model, locationmodel SKU and storage location
price, tax_class_id, pointsbase price excluding tax, tax class, bonus points for purchase
quantity, subtract, minimum, stock_status_idstock quantity, whether to subtract it on order, minimum quantity, stock status for out-of-stock items
weight, weight_class_id, length, width, height, length_class_id, shippingweight, dimensions and units, whether shipping is required
image, manufacturer_idmain image and manufacturer
status, date_available, sort_orderenabled or not, date from which the product is available, sort order
date_added, date_modifiedservice dates

The differences between versions in this table are significant:

  • OpenCart 3.0: has the code columns sku, upc, ean, jan, isbn, mpn, and the view counter viewed.
  • OpenCart 4.1: these columns are gone. Codes have been moved to the product_code table, and views to product_viewed and product_report. Instead, master_id, variant, and override have appeared for product variants, along with rating.

A import or feed module written for 3.0 that reads p.sku will get an SQL error about an unknown column in 4.1. Keep this in mind when porting any code that works with products directly.

How are descriptions, categories, and stores structured?

product_description

(product_id, language_id), with one row per language. Fields: name, description, tag, meta_title, meta_description, meta_keyword. There is an index on name. A product without a row for the current language will either not appear on the storefront or will appear without a name, so the import must create a description for every enabled language.

product_to_category

A many-to-many relation with the primary key (product_id, category_id) and a separate index on category_id. A product can belong to several categories. The core has no concept of a primary category: if one is needed for breadcrumbsA navigation chain such as "Home → Category → Product." More in the glossary → or feeds, that logic belongs to the module.

product_to_store and product_to_layout

product_to_store determines which stores the product is visible in; the default store has store_id = 0. product_to_layout with the key (product_id, store_id) sets the page layout for the product on a specific store. Without a row in product_to_store, the product will not be found on the storefront even with status 1.

Example

-- товари категорії 20, видимі в головному магазині, з назвою мовою 1
SELECT p.product_id, p.model, p.price, pd.name
FROM oc_product_to_category p2c
JOIN oc_product p ON (p.product_id = p2c.product_id)
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 p2c.category_id = 20
  AND p.status = 1
  AND p.date_available <= NOW();

These exact conditions (status, date_available, store, language) are applied by the storefront in both branches. If a product exists in the admin but is not visible on the site, check them one by one. The oc_ prefix and language ids here are examples: in code, use DB_PREFIX and config_language_id.

Where are prices, discounts, and specials stored?

The base price is product.price. Prices for customer groups and quantities are stored separately, and the branches diverge here:

WhatOpenCart 3.0OpenCart 4.1
Quantity discountproduct_discount: group, quantity, priority, price, datesproduct_discount with the same fields plus type and special = 0
Special priceseparate product_special tableproduct_discount row with special = 1
Value typealways a fixed priceF is a fixed price, S subtracts an amount, P is a percentage of the price
Bonus points for purchaseproduct_reward per customer groupproduct_reward

The storefront selects the row for the current customer groupA segment of buyers (retail, wholesale) with different prices and terms. More in the glossary →, quantity 1, and valid dates, with the lowest priority, and with equal priority, the lowest price. Empty dates are stored as 0000-00-00 and mean "no limit".

The cart price is calculated by the cart library with separate queries to these same tables, so changing the price only at the catalog model level is not enough. For model interception and its limits, see the article event model in OpenCart.

How are options, attributes, filters, and images stored?

Options

The options directory (option, option_value, and their descriptions) is shared across all products. Linking to a product happens in two tables:

  • product_option: which option the product has and whether it is required (required). For text options and dates, the default value is stored in value.
  • product_option_value: values for select, radio, or checkbox options, with their own quantity and subtract, as well as price, points, and weight adjustments: price/price_prefix, points/points_prefix, weight/weight_prefix. The prefix in the product form is + or -.

Option stock is independent of product stock: if subtract is enabled for an option valueA specific variant: "M," "L," "Red." More in the glossary →, both are subtracted on order.

Attributes and filters

product_attribute has the key (product_id, attribute_id, language_id) and the value text separately for each language. product_filter is simply (product_id, filter_id) pairs; filter values are described in the filter directory. Therefore, characteristics for comparison are better kept in attributes, and values for filtering in filters. For the store-side view of filters, see the article product filter in OpenCart.

The main image is product.image, additional ones are product_image with sort_order. Paths are relative to the image/ directory. product_related contains (product_id, related_id) pairs; the admin keeps the relation in both directions, and when a product is deleted, the core cleans up both directions. product_to_download links files for download after payment.

What's new with products in OpenCart 4.1?

Product codes: product_code and identifier

The identifier table is a directory of code types (SKU, UPC, EAN, JAN, etc., with the validation field), and product_code holds values for a product: product_id, code, value. Selecting a product by SKU in 4.1 is a JOIN with product_code on code = 'SKU', not a condition on the product.sku column.

Product variants

A variant is a separate product row with master_id pointing to the master product. variant stores JSON of the selected option values, and override stores JSON of the fields the variant does not inherit from the master. Fields not marked in override are inherited by the variant from the master product. Therefore, editing such a field directly in a variant may be overwritten by the admin the next time the master product is saved. Change shared fields in the master product, and in the variant only those marked in override.

Views and subscriptions

The view counter has moved from product.viewed to product_viewed, while detailed records (store, IP, country, date) are in product_report. The 3.0 recurring payments (product_recurring) have been replaced by product_subscription with the key (product_id, subscription_plan_id, customer_group_id).

How to safely modify products from a module?

There are no foreign keys in the database in either 3.0 or 4.1, so integrity is maintained only by code. Deleting a product in the admin cleans up, besides product, the descriptions, attributes, categories, discounts, filters, images, options, related productsProducts shown on a product page as recommendations. They increase the average order value. More in the glossary →, points, stores, layouts, reviews, SEO URLs, and coupon links; in 4.1 also codes, reports, and subscriptions. A manual DELETE FROM product will leave all these rows behind.

  • Write through the admin model catalog/product: addProduct(), editProduct(), deleteProduct().
  • Remember that editProduct() overwrites related tables with the data passed in. If you pass an array without images, the images will be deleted.
  • Clear the cache: after changes, the admin model calls $this->cache->delete('product'); your direct query should do this too.
  • Do not add columns to product. Keep your own fields in a separate module table with product_id; see custom module tables in OpenCart.
  • Don't forget the SEO URL: it is in seo_url, not in the product tables. The record format differs between 3.0 and 4.1, see seo_url in OpenCart.

Bulk updating prices and stock from price lists is often easier to delegate to a ready-made module, for example Automatic Price List Processing (AOP); how it works is described in the blog: importing price lists into OpenCart.

Summary

  • product holds the price, stock, weight, and status; names are in product_description by language, categories in product_to_category, and visibility in product_to_store.
  • The storefront shows a product only with status 1, a valid date_available, a row for the store, and a description in the current language.
  • In 4.1, product codes are in product_code, specials in product_discount with special = 1 and a value type, and variants with master_id and override have appeared.
  • Write products through the catalog/product model and clear the product cache.

Tables for orders, customers, languages, and stores are covered in other articles in the Database section. For how models work with the database, see the article OpenCart model.

FAQ

Why is the product in the admin but not showing on the site?
Check the status, the date available, the product_to_store row for the required store, and the description for the current language. If everything is in place, check whether the product is linked to the category you are opening.
Where is the product view count stored?
In 3.0 it is in the product.viewed column, in 4.1 it is in the product_viewed table, and detailed view records are in product_report.
Can several products have the same model?
Yes, the model column has no unique key. If the SKU must be unique for import or warehouse use, check this in the module code.
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.