This packages facilitates a way to push stock to Magento from a configurable source. Both simple stock and MSI are supported.
This package provides all the logic of pushing stock to Magento. It only requires an implementation for retrieving the stock.
Features:
- Retrieve stock from any source
- MSI support
- Only update stock when there are modifications
- Automatically stop syncing when there are too many errors (configurable)
- Compare stock between Magento and this package
- Supports Magento 2 async bulk requests for updating stock using Laravel Magento Async
- Logs activities using Spatie activitylog
- Checks if Magento products exist using JustBetter Magento Products
Also check out our other Laravel Magento packages! We also have a Magento Client to easily connect Laravel to Magento!
Require this package: composer require justbetter/laravel-magento-stock
Publish the config
php artisan vendor:publish --provider="JustBetter\MagentoStock\ServiceProvider" --tag="config"
Publish the activity log's migrations:
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="activitylog-migrations"
Run migrations.
php artisan migrate
TIP: All actions in this package are run via jobs, we recommend Laravel Horizon or another queueing system to run these
Add the following commands to your scheduler.
protected function schedule(Schedule $schedule): void
{
// Process stocks marked for retrieval / update
$schedule->command(\JustBetter\MagentoStock\Commands\ProcessStocksCommand::class)->everyMinute();
// Retrieve all stock daily
$schedule->command(\JustBetter\MagentoStock\Commands\Retrieval\RetrieveAllStockCommand::class)->dailyAt('05:00');
// Retrieve modified stock every fifteen minutes, with a small overlap
$schedule->command(\JustBetter\MagentoStock\Commands\Retrieval\RetrieveAllStockCommand::class, ['from' => 'now -1 hour'])->everyFifteenMinutes();
}
We have a Laravel Nova integration for this package.
This package requires you to implement a stock repository which is responsible for retrieving stock.
Implement the retrieve
method and return a StockData
object that contains the stock fields.
<?php
namespace App\Integrations\MagentoStock;
use JustBetter\MagentoStock\Repositories\Repository;
use JustBetter\MagentoStock\Data\StockData;
use JustBetter\MagentoStock\Enums\Backorders;
class MyStockRepository extends Repository
{
public function retrieve(string $sku): ?StockData
{
// Retrieve stock from your source
return StockData::of([
'sku' => $sku,
'quantity' => 10,
'in_stock' => true,
'backorders' => Backorders::BackordersNotify,
]);
}
}
By default the Repository
that you are extending will retrieve the SKU's from justbetter/laravel-magento-products.
If you wish to use this you have to add the commands to your scheduler to automatically import products.
If you have another source for your SKU's you may implement the skus
method yourself.
It accepts an optional carbon instance to only retrieve modified stock.
<?php
namespace App\Integrations\MagentoStock;
use JustBetter\MagentoStock\Repositories\Repository;
use Illuminate\Support\Carbon;
use Illuminate\Support\Enumerable;
class MyStockRepository extends Repository
{
public function skus(?Carbon $from = null): ?Enumerable
{
return collect(['sku_1', 'sku_2']);
}
}
The repository class has a couple of settings that you can adjust:
class BaseRepository
{
protected string $name = 'Repository';
// How many stocks may be retrieved at once when the process job runs
protected int $retrieveLimit = 250;
// How many stocks may be updated at once when the process job runs
protected int $updateLimit = 250;
// How many times an update to Magento may fail before it stops trying
protected int $failLimit = 3;
// If MSI is enabled
protected bool $msi = false;
// Enable if the package should update backorders
protected bool $backorders = false;
}
After you've created and configured the repository you have to set it in your configuration file:
<?php
return [
'repository' => \App\Integrations\MagentoStock\MyStockRepository::class,
];
To retrieve stock you can use the following commands.
php artisan magento-stock:process
Each stock record has two flags, retrieve
and update
.
This command will dispatch the jobs to run those actions.
It is primarily used for updates, but you can implement the retrieve flag in your application to slow down retrievals.
php artisan magento-stock:retrieve {sku}
This will directly pass the SKU to your repository and process the result. If the stock has been modified it will set the update flag.
php artisan magento-stock:retrieve-all {from?}
This will call the skus
method on your repository and dispatch retrieval jobs for all the SKU's that are returned from your repository.
php artisan magento-stock:update {sku}
php artisan magento-stock:update-all
php artisan magento-stock:compare
This will compare the stock in Magento and the database table. If it differs it will force an update to Magento.
If you have Magento MSI enabled you have to return the quantity and status of each source in the repository.
NOTE: Be sure to set the
msi
setting in the repository totrue
!
For example:
<?php
namespace App\Integrations\MagentoStock;
use JustBetter\MagentoStock\Repositories\Repository;
use JustBetter\MagentoStock\Data\StockData;
use JustBetter\MagentoStock\Enums\Backorders;
class MyStockRepository extends Repository
{
public function retrieve(string $sku): ?StockData
{
return StockData::of([
'sku' => $sku,
'backorders' => Backorders::BackordersNotify,
'msi_status' => [
'A' => true, // Source A is in stock
'B' => false, // Source B is out of stock
],
'msi_quantity' => [
'A' => 10, // Source A has qty of 10
'B' => 0, // Source B has qty of 0
],
]);
}
}
This feature requires job batching
This package provides a way to compare stock quantities in Magento with those in the Laravel database. If a difference is detected it will start an update for that product.
An event is dispatched when a difference is detected \JustBetter\MagentoStock\Events\DifferenceDetectedEvent
.
When an update fails it will try again. A fail counter is stored with the model which is increased at each failure.
In the repository you can specify how many times the update may be attempted.
You can restart the updates for a product by setting the sync
field in the DB to true.
To ensure the quality of this package, run the following command:
composer quality
This will execute three tasks:
- Makes sure all tests are passed
- Checks for any issues using static code analysis
- Checks if the code is correctly formatted
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.