Product Comparison Component Development on Vue.js for 1C-Bitrix
The standard Bitrix comparison component (bitrix:catalog.compare.buy + bitrix:catalog.compare.result) works through page reloads: the user adds a product to the comparison, the page refreshes, the counter changes. The comparison page is a static table. All of this worked in 2010; today UX expectations are different. The Vue.js version: adding to comparison is instant, the header counter updates without a reload, and the comparison table can hide identical characteristics.
Storing the Comparison List
Standard Bitrix stores comparisons in a PHP session ($_SESSION). For the Vue version, localStorage with server synchronization for authenticated users is better — the list persists across sessions and devices.
// stores/compareStore.ts (Pinia)
export const useCompareStore = defineStore('compare', {
state: () => ({
items: [] as number[], // product IDs
}),
actions: {
async add(productId: number) {
if (this.items.includes(productId)) return
if (this.items.length >= 4) {
alert('You can compare up to 4 products at once')
return
}
this.items.push(productId)
localStorage.setItem('compare_items', JSON.stringify(this.items))
// Sync with server for authenticated users
if (isLoggedIn()) {
await fetch('/local/api/compare/add/', {
method: 'POST',
body: JSON.stringify({ product_id: productId }),
})
}
},
remove(productId: number) {
this.items = this.items.filter(id => id !== productId)
localStorage.setItem('compare_items', JSON.stringify(this.items))
},
isInCompare: (state) => (productId: number) => state.items.includes(productId),
},
getters: {
count: (state) => state.items.length,
},
})
On app initialization the list is loaded from localStorage (for guests) or from the API (for authenticated users, with cross-device merge).
"Compare" Button on the Product Card
<!-- CompareButton.vue -->
<template>
<button
:class="['compare-btn', { 'compare-btn--active': isAdded }]"
@click="toggle"
:title="isAdded ? 'Remove from comparison' : 'Add to comparison'"
>
<IconScale :filled="isAdded" />
<span>{{ isAdded ? 'In comparison' : 'Compare' }}</span>
</button>
</template>
<script setup lang="ts">
const props = defineProps<{ productId: number }>()
const store = useCompareStore()
const isAdded = computed(() => store.isInCompare(props.productId))
function toggle() {
isAdded.value ? store.remove(props.productId) : store.add(props.productId)
}
</script>
The button is mounted on each product card. In the Bitrix template:
// In the product card template.php
?>
<div data-compare-btn data-product-id="<?= $arItem['ID'] ?>"></div>
<?php
// After page load Vue mounts CompareButton into each [data-compare-btn]
Counter in the Site Header
<!-- CompareCounter.vue (mounted in header) -->
<template>
<a href="/compare/" class="header-compare">
<IconScale />
<span class="badge" v-if="count > 0">{{ count }}</span>
</a>
</template>
<script setup lang="ts">
const store = useCompareStore()
const count = computed(() => store.count)
</script>
The counter updates reactively via Pinia — no global EventBus or DOM manipulation.
Comparison Page
The most complex part — the comparison table. Product data is loaded from the server by ID list:
// In CompareTable.vue
async function loadCompareData(ids: number[]) {
const res = await fetch(`/local/api/compare/?ids=${ids.join(',')}`)
const data = await res.json() // [{id, name, price, specs: {...}}, ...]
products.value = data
buildDiffMap()
}
// Identify identical characteristics for the option to hide them
function buildDiffMap() {
const allSpecs = new Set(products.value.flatMap(p => Object.keys(p.specs)))
diffMap.value = {}
allSpecs.forEach(spec => {
const values = products.value.map(p => p.specs[spec])
diffMap.value[spec] = new Set(values).size > 1 // true = there are differences
})
}
"Show only differences" filter — a button that hides rows where all values are identical:
<template>
<table class="compare-table">
<thead>
<tr>
<th>Characteristic</th>
<th v-for="p in products" :key="p.id">
<ProductCard :product="p" @remove="store.remove(p.id)" />
</th>
</tr>
</thead>
<tbody>
<tr
v-for="spec in visibleSpecs"
:key="spec"
:class="{ 'row--different': diffMap[spec] }"
>
<td class="spec-name">{{ specLabels[spec] }}</td>
<td v-for="p in products" :key="p.id">{{ p.specs[spec] ?? '—' }}</td>
</tr>
</tbody>
</table>
</template>
<script setup lang="ts">
const showOnlyDiff = ref(false)
const visibleSpecs = computed(() =>
showOnlyDiff.value
? Object.keys(diffMap.value).filter(k => diffMap.value[k])
: Object.keys(diffMap.value)
)
</script>
PHP Data Controller for Comparison
// CompareController.php
public function getAction(string $ids): array {
$idArr = array_filter(array_map('intval', explode(',', $ids)));
if (!$idArr) return [];
$products = [];
$res = CIBlockElement::GetList(
[],
['ID' => $idArr, 'IBLOCK_ID' => CATALOG_IBLOCK_ID, 'ACTIVE' => 'Y'],
false, false,
['ID', 'NAME', 'PREVIEW_PICTURE', 'DETAIL_PAGE_URL', 'CATALOG_PRICE_1_']
);
while ($el = $res->GetNext()) {
// Get properties for comparison
$props = [];
$prRes = CIBlockElement::GetProperty(CATALOG_IBLOCK_ID, $el['ID'], [], ['COMPARE' => 'Y']);
while ($pr = $prRes->Fetch()) {
$props[$pr['CODE']] = $pr['VALUE'];
}
$products[] = [
'id' => $el['ID'],
'name' => $el['NAME'],
'price' => $el['CATALOG_PRICE_1_'],
'image' => $el['PREVIEW_PICTURE'] ? CFile::GetPath($el['PREVIEW_PICTURE']) : null,
'url' => $el['DETAIL_PAGE_URL'],
'specs' => $props,
];
}
return $products;
}
Properties participating in comparison are flagged with COMPARE = 'Y' in the infoblock settings — a standard Bitrix mechanism.
Fixed First Column on Mobile
The comparison table on mobile is horizontally scrollable. The first column (characteristic names) must be sticky:
.compare-table {
display: grid;
grid-template-columns: 180px repeat(4, 1fr);
overflow-x: auto;
}
.compare-table td:first-child,
.compare-table th:first-child {
position: sticky;
left: 0;
background: white;
z-index: 1;
}
CSS Grid with position: sticky is a more reliable approach than display: table with fixed columns.
Timeline
| Scope | What's included | Duration |
|---|---|---|
| Basic comparison | Buttons + counter + table | 1–2 weeks |
| Full-featured component | + diff filter, localStorage, sync | 2–3 weeks |
| + Mobile adaptive, persist for user account | + sticky column, user save | +1 week |
Product comparison is a tool for buyers choosing between similar items. A properly implemented comparison helps make a decision on the site rather than leaving for an aggregator.







