PHP code example of justbetter / laravel-magento-stock

1. Go to this page and download the library: Download justbetter/laravel-magento-stock library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

justbetter / laravel-magento-stock example snippets


    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();
    }



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,
        ]);
    }
}



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']);
    }
}

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;
}



return [
    'repository' => \App\Integrations\MagentoStock\MyStockRepository::class,
];



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
            ],
        ]);
    }
}

php artisan vendor:publish --provider="JustBetter\MagentoStock\ServiceProvider" --tag="config"

php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="activitylog-migrations"

php artisan migrate