-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Product import command & add utility to start imports (#46)
- Loading branch information
Showing
8 changed files
with
277 additions
and
58 deletions.
There are no files selected for viewing
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
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
35 changes: 35 additions & 0 deletions
35
resources/views/utilities/import_utility/imports.blade.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
@extends('statamic::layout') | ||
@section('title', __('Import')) | ||
|
||
@section('content') | ||
<div class="flex items-center justify-between"> | ||
<h1>{{ __('Import categories') }}</h1> | ||
</div> | ||
|
||
<div class="mt-3 card"> | ||
<form action="{{ cp_route('utilities.imports.import-categories') }}" method="POST"> | ||
@csrf | ||
|
||
<p class="mb-2">@lang('The import of non-existing categories may be started through the button below.')</p> | ||
|
||
<div class="flex items-center space-x-3"> | ||
<button type="submit" class="btn-primary">@lang("Import categories")</button> | ||
</div> | ||
</form> | ||
</div> | ||
|
||
<div class="flex items-center justify-between mt-5"> | ||
<h1>{{ __('Import products') }}</h1> | ||
</div> | ||
|
||
<div class="mt-3 card"> | ||
<form action="{{ cp_route('utilities.imports.import-products') }}" method="POST"> | ||
@csrf | ||
|
||
<p class="mb-2">@lang('The import of non-existing products may be started through the button below.')</p> | ||
<div class="flex items-center space-x-3"> | ||
<button type="submit" class="btn-primary">@lang("Import products")</button> | ||
</div> | ||
</form> | ||
</div> | ||
@stop |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Rapidez\Statamic\Actions; | ||
|
||
use Statamic\Facades\Entry; | ||
use ReflectionClass; | ||
|
||
class StatamicEntryAction | ||
{ | ||
public static function createEntry(array $attributes, array $values = []): void | ||
{ | ||
if (Entry::query()->where($attributes)->count()) { | ||
// Entry was already created. | ||
return; | ||
} | ||
|
||
/** @var \Statamic\Entries\Entry $entry */ | ||
$entry = Entry::make(); | ||
$values = array_merge($attributes, $values); | ||
|
||
static::setEntryData($entry, $values)->save(); | ||
} | ||
|
||
public static function setEntryData(\Statamic\Entries\Entry $entry, array $values = []) : \Statamic\Entries\Entry | ||
{ | ||
$reflectedEntry = new ReflectionClass($entry); | ||
foreach ($values as $key => $value) { | ||
// Check if the key is a statamic setter | ||
if (!$reflectedEntry->hasMethod($key) || $reflectedEntry->getMethod($key)->getNumberOfParameters() < 1) { | ||
continue; | ||
} | ||
|
||
$entry->$key($value); | ||
unset($values[$key]); | ||
} | ||
|
||
$entry->merge($values); | ||
|
||
return $entry; | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace Rapidez\Statamic\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\Event; | ||
use Rapidez\Core\Facades\Rapidez; | ||
use Rapidez\Statamic\Actions\StatamicEntryAction; | ||
use Statamic\Facades\Site; | ||
|
||
class ImportProducts extends Command | ||
{ | ||
protected $signature = 'rapidez:statamic:import:products {--site=* : Sites handles or urls to process, if none are passed it will be done for all sites (Default will be all sites)}'; | ||
|
||
protected $description = 'Create a new product entry for a product if it does not exist.'; | ||
|
||
public function handle() | ||
{ | ||
$productModel = config('rapidez.models.product'); | ||
|
||
/** @var StatamicEntryAction $statamicEntryAction */ | ||
$statamicEntryAction = app(StatamicEntryAction::class); | ||
|
||
$sites = $this->option('site'); | ||
$sites = $sites | ||
? array_filter(array_map(fn($handle) => (Site::get($handle) ?: Site::findByUrl($handle)) ?: $this->output->warning(__('No site found with handle or url: :handle', ['handle' => $handle])), $sites)) | ||
: Site::all(); | ||
|
||
$bar = $this->output->createProgressBar(count($sites)); | ||
$bar->start(); | ||
foreach($sites as $site) { | ||
$bar->display(); | ||
|
||
$siteAttributes = $site->attributes(); | ||
if (!isset($siteAttributes['magento_store_id'])) { | ||
continue; | ||
} | ||
|
||
Rapidez::setStore($siteAttributes['magento_store_id']); | ||
|
||
$products = $productModel::query() | ||
->selectAttributes(['entity_id', 'sku', 'name', 'url_key']) | ||
->lazy(); | ||
|
||
foreach ($products as $product) { | ||
$statamicEntryAction::createEntry( | ||
[ | ||
'collection' => config('rapidez-statamic.import.products.collection', 'products'), | ||
'blueprint' => config('rapidez-statamic.import.products.blueprint', 'product'), | ||
'site' => $site->handle(), | ||
'linked_product' => $product->sku, | ||
], | ||
array_merge([ | ||
'locale' => $site->handle(), | ||
'site' => $site->handle(), | ||
], ...Event::dispatch('rapidez-statamic:product-entry-data', ['product' => $product])) | ||
); | ||
} | ||
|
||
$bar->advance(); | ||
} | ||
$bar->finish(); | ||
|
||
return static::SUCCESS; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Rapidez\Statamic\Http\Controllers; | ||
|
||
use Statamic\Facades\CP\Toast; | ||
use Illuminate\Contracts\View\View; | ||
use Illuminate\Http\RedirectResponse; | ||
use Illuminate\Support\Facades\Artisan; | ||
use Statamic\Http\Controllers\Controller; | ||
|
||
class ImportsController extends Controller | ||
{ | ||
public function __invoke() : View | ||
{ | ||
return view('rapidez-statamic::utilities.import_utility.imports'); | ||
} | ||
|
||
public function importCategories() : RedirectResponse | ||
{ | ||
Artisan::queue('rapidez:statamic:import:categories --all') | ||
->onQueue('imports'); | ||
|
||
Toast::success(__('The import of categories has started!'))->duration(5000); | ||
|
||
return redirect(cp_route('utilities.imports')); | ||
} | ||
|
||
public function importProducts() : RedirectResponse | ||
{ | ||
Artisan::queue('rapidez:statamic:import:products') | ||
->onQueue('imports'); | ||
|
||
Toast::success(__('The import of products has started!'))->duration(5000); | ||
|
||
return redirect(cp_route('utilities.imports')); | ||
} | ||
} |
Oops, something went wrong.