Better Product Comparison for OpenCart 4: A Floating Compare Tray and Highlight Differences (Free Extension)
A free extension that makes OpenCart product comparison easier: a live compare tray on every page and switches that highlight or show only the rows that differ.

OpenCart 4’s product comparison works, but shoppers barely notice it. Clicking the compare icon shows a success alert that fades after a few seconds. After that there is no sign of what they are comparing until they find the comparison page. That page then lists every row, including the ones where all the products are the same, and on a phone the table runs off the side of the screen.
This free extension makes OpenCart product comparison easier to use. A floating compare tray shows thumbnails of the products being compared on every page, with Compare now, remove and clear buttons. On the comparison page, two switches highlight the rows that differ or hide the rows that are the same. It uses the core compare list and the core compare page, so nothing else changes. Tested on OpenCart 4.1.0.4.
Download: smartcompare.ocmod.zip (OpenCart 4.0.2.x to 4.1.x, free, no core file changes)

What the extension does
- Shows a compare tray in the bottom-left or bottom-right corner with a thumbnail of each product being compared and “Compare (3/4)”.
- Updates the tray the moment a shopper clicks any core Add to compare button, on category, search and product pages.
- Removes one product (the × on its thumbnail) or clears all of them without leaving the page.
- Keeps Compare now disabled until there are at least two products.
- Lets shoppers collapse the tray to a single line. The browser remembers the choice.
- Adds Highlight differences and Show only differences switches above the comparison table, with a count such as “3 rows are the same, 12 are different”.
- Makes the comparison table scroll sideways on phones, with the row labels kept in view.
Install the extension
- Download
smartcompare.ocmod.zip. - Go to Extensions > Installer, click Upload and choose the zip.
- Go to Extensions > Extensions, choose Modules, find Smart Compare and click Install.
- Click Edit, switch Status on and click Save.
Settings

- Show the tray and Position: pick the corner that doesn’t clash with other floating buttons. Our demo store has a sticky add to cart bar and a back-to-top button, so the tray sits bottom left.
- Differences toolbar: the two switches on the comparison page.
- Highlight differences by default and Default view: start with differences highlighted, and with all rows or only the rows that differ.
How the tray stays in sync with OpenCart
The core compare buttons post to product/compare.add through jQuery in common.js. The tray doesn’t replace them. It listens for those requests to succeed and then asks the server for the current list:
// Core "Add to compare" buttons post through common.js with jQuery. Refresh the tray when one succeeds.
if (window.jQuery) {
jQuery(document).ajaxSuccess(function (event, xhr, settings) {
if (settings && settings.url && settings.url.indexOf('compare.add') !== -1) {
setCollapsed(false);
send(cfg.tray);
}
});
}
The list itself is OpenCart’s own $this->session->data['compare'], so the tray, the core compare page and the compare buttons always agree. Removing a product filters that same array:
public function remove(): void {
if ($this->enabled() && $this->request->server['REQUEST_METHOD'] == 'POST') {
if (!empty($this->request->post['clear'])) {
$this->session->data['compare'] = [];
} else {
$product_id = (int)($this->request->post['product_id'] ?? 0);
$this->session->data['compare'] = array_values(array_filter((array)($this->session->data['compare'] ?? []), fn($id) => (int)$id !== $product_id));
}
}
$this->json();
The tray is added before </body> by a catalog/view/common/footer/after event, with the first list rendered on the server, so it appears without waiting for a request. It is left off the comparison page itself, where it would only repeat the table. Here it is on a product page after adding a fourth product:

Highlight the differences
The comparison table is long: price, model, brand, availability, rating, summary, weight, dimensions and every attribute group. Most shoppers only care about the rows that differ. The extension compares the text of each row across the products and marks it as the same or different:
// A row is "the same" when every product cell has the same text (ignoring spacing and case).
// Rows with no first-column label (the add to cart / remove buttons) are left alone.
table.querySelectorAll('tr').forEach(function (row) {
var cells = Array.prototype.slice.call(row.children);
if (cells.length !== products + 1 || cells[0].textContent.trim() === '') {
return;
}
var values = cells.slice(1).map(function (td) {
var img = td.querySelector('img');
var text = td.textContent.replace(/\s+/g, ' ').trim().toLowerCase();
// Images and star ratings differ by markup, not text: compare those by what they show.
if (img && !text) {
text = 'img:' + img.getAttribute('src');
}
if (td.classList.contains('rating')) {
text = td.querySelectorAll('.fa-solid.fa-star').length + '|' + text;
}
return text;
});
var isSame = values.every(function (v) { return v === values[0]; });
row.classList.add(isSame ? 'sc-same' : 'sc-diff');
isSame ? same++ : diff++;
Comparing a MacBook, a MacBook Air and a MacBook Pro from the demo data, it found 3 rows that are the same (brand, weight and dimensions) and 12 that differ. With Show only differences on, those three rows disappear, and so does any attribute group heading whose rows are all the same:

The toolbar is printed above the table, so its script waits for DOMContentLoaded before reading the rows. The table is also wrapped in a .table-responsive div by the catalog/view/product/compare/after event, so on a phone it scrolls sideways while the row labels stay put:

What we found when testing
- Core OpenCart keeps at most four products in the comparison. Adding a fifth drops the oldest, so the tray always has four slots.
- Our first build of the toolbar did nothing: its script ran before the browser had parsed the table below it. Waiting for
DOMContentLoadedfixed it. - Counting columns with
tbody tr:first-childmatched the first row of every attribute group too, so every row looked “wrong”. The script now counts the cells of the first row of the firsttbody. - Rows where one product has no value, such as an attribute only two laptops have, count as different. That is usually what a shopper wants to see.
- On phones the tray becomes a full-width panel at the bottom. Shoppers can collapse it to one line.
- Uninstalling removes both events and the settings. The core compare page goes back to normal.
Limitations
- Four products at most, because that limit is in core
product/compare.add. - Rows are compared as text after trimming spaces and case. “1kg” and “1.00kg” count as different.
- Images are compared by file, so two products that share an image file count as the same in the image row.
- Themes that replace the core comparison table with different markup need the table class
table table-borderedfor the toolbar to attach.
Related posts
- Product Quick View for OpenCart 4: let shoppers check a product without leaving the category page.
- Faceted Filters for OpenCart 4: narrow the list by price, brand and attributes before comparing.
- Live Search for OpenCart 4: product suggestions as shoppers type.
- Product Availability on Product Cards for OpenCart 4: show stock on every listing.







