tmonier / sylius-b2b-predictive-analytics-plugin
Predictive reorder analytics for Sylius 2 B2B: median interval calculation between successive orders per company and product, scheduled recalculation, reorder alerts
Package info
github.com/thibmonier/sylius-b2b-predictive-analytics-plugin
Type:sylius-plugin
pkg:composer/tmonier/sylius-b2b-predictive-analytics-plugin
Requires
- php: ^8.2
- sylius/sylius: ^2.2.9 || ^2.3
- tmonier/sylius-company-accounts-plugin: ^1.1
- tmonier/sylius-company-insights-plugin: ^0.2
- twig/intl-extra: ^3.21
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpstan/phpstan-doctrine: ^2.0
- phpstan/phpstan-webmozart-assert: ^2.0
- phpunit/phpunit: ^10.5
- rector/rector: ^2.0
- sylius-labs/coding-standard: ^4.4
- sylius/test-application: ^2.0.0@alpha
- symfony/browser-kit: ^7.4 || ^8.0
- symfony/css-selector: ^7.4 || ^8.0
- symfony/debug-bundle: ^7.4 || ^8.0
- symfony/dom-crawler: ^7.4 || ^8.0
- symfony/dotenv: ^7.4 || ^8.0
- symfony/http-client: ^7.4 || ^8.0
- symfony/intl: ^7.4 || ^8.0
- symfony/runtime: ^7.4 || ^8.0
- symfony/web-profiler-bundle: ^7.4 || ^8.0
- symfony/webpack-encore-bundle: ^2.2
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-10-30 08:26:51 UTC
README
Français : README.fr.md
Predictive reorder analytics for Sylius 2 B2B: calculates reorder patterns per company and product reference to anticipate purchase needs before stock runs out.
Built on tmonier/sylius-company-accounts-plugin (companies, members, roles) and tmonier/sylius-company-insights-plugin (purchasing dashboard).
What it does
- Median interval calculation: analyzes order history to determine the typical purchase rhythm for each product reference per company
- Explainable prediction: keeps the calculation explanation ("ordered 5 times, every 28 days on median")
- Expected reorder date: estimates the next order date based on usual rhythm
- Reorder alerts in customer account: displays a "To reorder soon" block for products with expected reorder date within the next 7 days (including overdue items up to one median interval)
- Pre-fill cart: "Prepare cart" button adds the typical median quantity to cart
- Dismiss alerts: "Not now" (temporary) or "Don't suggest again" (permanent) buttons
- Recalculation command: updates all predictions with a schedulable command
Prerequisites
- PHP 8.2 or higher
- Sylius 2.2.9 or 2.3+
tmonier/sylius-company-accounts-plugin^1.1 installedtmonier/sylius-company-insights-plugin^0.2 installed
Installation
composer require tmonier/sylius-b2b-predictive-analytics-plugin
-
Enable the plugin in
config/bundles.php:Tmonier\SyliusPredictiveAnalyticsPlugin\TmonierSyliusPredictiveAnalyticsPlugin::class => ['all' => true],
-
Run migrations to create the predictions table:
php bin/console doctrine:migrations:migrate
-
Import the routes in
config/routes.yaml:tmonier_predictive: resource: '@TmonierSyliusPredictiveAnalyticsPlugin/config/routes.yaml'
Usage
Reorder alerts in customer account
The plugin automatically displays a "To reorder soon" block in the customer area for product references with expected reorder date within the next 7 days (including overdue items up to one median interval).
Each alert offers:
- "Prepare cart" button that pre-fills the cart with the usual median quantity
- "Not now" button to temporarily hide the alert
- "Don't suggest again" button to permanently hide this product
Company isolation is strict: a buyer only sees alerts from their own company.
Calculate a prediction
use Tmonier\SyliusPredictiveAnalyticsPlugin\Calculator\PredictiveReorderCalculatorInterface; $prediction = $calculator->calculate($variant, $company); if ($prediction->isPredicted()) { echo $prediction->explanation; // "ordered 5 times, every 28 days on median" echo $prediction->expectedReorderDate->format('Y-m-d'); // Expected next reorder date } else { echo $prediction->explanation; // "insufficient history: ordered 1 time, 3 orders needed" }
Scheduled recalculation
The plugin includes a command to recalculate all predictions:
php bin/console tmonier:predictive:recalculate
This command can be scheduled daily via cron:
0 2 * * * cd /path/to/project && php bin/console tmonier:predictive:recalculate
Screenshots
The "Reorder soon" block of the customer account, in the Sylius B2B demo:
Calculation rules
- Minimum 3 orders: a prediction requires at least 3 completed orders
- Counted orders: only completed and non-cancelled orders are taken into account
- Robust median: the median is not skewed by extreme values, unlike the average
- Company isolation: each company has its own predictions, with no mixing
B2B demo
This plugin is used in the Sylius B2B demo: a Sylius 2.3 shop with fictitious sample data where the B2B plugins work together. The source code, setup and fixtures of the demo are in that repository.
- Online demo: sylius-b2b.thibaut-monier.fr
- Author: Thibaut Monier
The demo is an example, not a production setup.
License
MIT
