Last active
February 8, 2022 01:42
-
-
Save dmaksimov/0bf63f818f6b17926dc652c9724b0929 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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