PHP code example of justbetter / laravel-magento-async

1. Go to this page and download the library: Download justbetter/laravel-magento-async 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-async example snippets




protected function schedule(Schedule $schedule): void
{
    $schedule->command(\JustBetter\MagentoAsync\Commands\UpdateBulkStatusesCommand::class)->everyFiveMinutes();
    $schedule->command(\JustBetter\MagentoAsync\Commands\CleanBulkRequestsCommand::class)->everyFiveMinutes();
}



public function __construct(
    protected \JustBetter\MagentoAsync\Client\MagentoAsync $magentoAsync
) {}
...
public function handle(string $store): void {
    $products = Product::query()->take(1000)->get();

    $payload = $products
        ->map(fn(Product $product) => ['product' => ['sku' => $product->sku, 'status' => $product->status]])
        ->toArray();

    $this->magentoAsync
        ->configure(fn (Magento $magento): Magento => $magento->store($store)) // Optionally configure the Magento client
        ->subjects($products->all()) // Pass the models in the same order as the payload
        ->postBulk('products', $payload); // Bulk post this payload to the `products` endpoint
}



public function __construct(
    protected \JustBetter\MagentoAsync\Client\MagentoAsync $magentoAsync
) {}
...
public function handle(): void {
    $product = Product::query()->first();

    $payload =  ['product' => ['sku' => $product->sku, 'status' => $product->status]];

    $this->magentoAsync
        ->subject($product) // Pass the subject
        ->post('products', $payload); // Post this payload to the `products` endpoint
}



use \JustBetter\MagentoAsync\Listeners\BulkOperationStatusListener as BaseBulkOperationStatusListener;
use \JustBetter\MagentoAsync\Models\BulkOperation;
use \JustBetter\MagentoAsync\Enums\OperationStatus;

class BulkOperationStatusListener extends BaseBulkOperationStatusListener
{
    protected string $model = Product::class;

    public function execute(BulkOperation $operation): void
    {
        /** @var Model $status */
        $status = $operation->subject;

        if ($operation->status === OperationStatus::Complete) {
            // Handle complete status
            return;
        }

        // Handle failed status
    }
}
bash
php artisan vendor:publish --provider="JustBetter\MagentoAsync\ServiceProvider" --tag=config