Skip to content

Instantly share code, notes, and snippets.

@ssatz
Created January 12, 2018 05:35
Show Gist options
  • Save ssatz/51d426a5c170a61c32896edb14b33add to your computer and use it in GitHub Desktop.
Save ssatz/51d426a5c170a61c32896edb14b33add to your computer and use it in GitHub Desktop.
map amazon product
<?php
namespace App\Console\Commands;
use App\ProductStore;
use App\ProductStoreMap;
use Illuminate\Console\Command;
use Maatwebsite\Excel\Facades\Excel;
class MapAmazonProduct extends Command
{
private $productStoreObj;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'mapproduct:amazon';
/**
* 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 mixed
*/
public function handle(ProductStoreMap $productStoreObj)
{
$this->productStoreObj = $productStoreObj;
$productCount = 0;
try {
$amazonStoreId = ProductStore::where(['name' => 'Amazon.in'])->first()->id;
$flipartData = ProductStoreMap::all();
Excel::filter('chunk')->load(storage_path('amazon_product_map.csv'))->chunk(250,
function ($results) use ($amazonStoreId, $flipartData, $productCount, $productStoreObj) {
$mappedProducts = [];
$index = 0;
foreach ($results as $row) {
$flipkartProduct = $flipartData->where('product_id', $row->flipkartid)->first();
if (!is_null($flipkartProduct)) {
$mappedProducts[$index]['product_store_id'] = $amazonStoreId;
$mappedProducts[$index]['product_detail_id'] = $flipkartProduct->product_detail_id;
$mappedProducts[$index]['product_id'] = $row->amazonid;
$index++;
}
}
$productCount = $productCount + count($mappedProducts);
$productStoreObj->insert($mappedProducts);
});
} catch (\Exception $e) {
$this->info($e->getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment