Skip to content

Instantly share code, notes, and snippets.

@dmaksimov
Last active February 8, 2022 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmaksimov/0bf63f818f6b17926dc652c9724b0929 to your computer and use it in GitHub Desktop.
Save dmaksimov/0bf63f818f6b17926dc652c9724b0929 to your computer and use it in GitHub Desktop.
<?php
namespace App\Console\Commands;
use App\Models\LocallyProduct;
use Illuminate\Console\Command;
class TestLocallySyncVariants extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'test-locally-sync-variants';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$results = (object) [
'matches' => 0,
'no_matches' => 0,
];
$allLocallyProducts = LocallyProduct::where('product_id', '!=', '')->get();
$locallyProductGroups = $allLocallyProducts->groupBy('handle');
foreach ($locallyProductGroups as $handle => $locallyProducts) {
$product = $locallyProducts->first()->product;
$variantSkus = $product->variants->pluck('sku')->toArray();
$barcodes = $locallyProducts->pluck('barcode')->toArray();
if (array_values_match($variantSkus, $barcodes)) {
$this->info('Product #' . $product->id . ' matches');
$results->matches++;
} else {
$this->warn('Product #' . $product->id . ' no match');
$results->no_matches++;
$this->line('Locally barcodes');
dump($barcodes);
$this->line('Variant SKUs:');
dump($variantSkus);
$this->line('');
}
}
$this->line('');
$this->info('Results:');
dump($results);
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment