-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(frequently-bought-together): compatibility patch
- Loading branch information
1 parent
70c2e80
commit 5012fdb
Showing
4 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
packages/vendure-plugin-frequently-bought-together/CHANGELOG.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
# 0.0.2 (2024-12-03) | ||
|
||
- Patched compatibility range to >= 2.2.0 | ||
|
||
# 0.0.1 (2024-12-02) | ||
|
||
- Initial plugin setup |
2 changes: 1 addition & 1 deletion
2
packages/vendure-plugin-frequently-bought-together/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
packages/vendure-plugin-frequently-bought-together/src/fpgrowth.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { DataSource } from 'typeorm'; | ||
import { ID, OrderLine } from '@vendure/core'; | ||
import { FPGrowth, Itemset } from 'node-fpgrowth'; | ||
|
||
export async function getFrequentlyBoughtTogetherItemSets( | ||
app: INestApplication | ||
): Promise<void> { | ||
const dataSource = app.get(DataSource); | ||
const repository = await dataSource.getRepository(OrderLine); | ||
|
||
const ordersSince = new Date(); | ||
ordersSince.setFullYear(ordersSince.getFullYear() - 5); | ||
const channelId = 1; | ||
const languageCode = 'nl'; | ||
|
||
const result = await repository | ||
.createQueryBuilder('orderLine') | ||
.innerJoinAndSelect('orderLine.productVariant', 'productVariant') | ||
.innerJoinAndSelect('productVariant.translations', 'pvTranslations') | ||
.innerJoin('orderLine.order', 'order') | ||
.innerJoin('order.channels', 'channel') | ||
.where('order.orderPlacedAt >= :ordersSince', { ordersSince }) | ||
.andWhere('channel.id = :channelId', { channelId }) | ||
.andWhere('productVariant.deletedAt IS NULL') | ||
.andWhere('pvTranslations.languageCode = :languageCode', { languageCode }) | ||
.select(['orderLine.*', 'productVariant.id', 'pvTranslations.name']) | ||
.getRawMany(); | ||
|
||
const identifyingField = 'pvTranslations_name'; // Use productVariant_id for production | ||
|
||
const transactions = new Map<ID, string[]>(); | ||
result.forEach((orderLine) => { | ||
const transactionsForOrder = transactions.get(orderLine.orderId) || []; | ||
transactionsForOrder.push(orderLine[identifyingField]); | ||
transactions.set(orderLine.orderId, transactionsForOrder); | ||
}); | ||
const matrix = Array.from(transactions.values()); | ||
console.log('calculating frequent itemsets'); | ||
const fpgrowth = new FPGrowth<string>(0.001); | ||
const itemSets = (await fpgrowth.exec(matrix)) | ||
// Only combinations allowed | ||
.filter((is) => is.items.length > 1) | ||
// Lowest support first, because if they make sense, the higher ones will too | ||
.sort((a, b) => a.support - b.support) | ||
.reverse(); | ||
|
||
console.log('frequent itemsets:', itemSets); | ||
console.log('Total item sets', itemSets.length); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters