Skip to content

Commit e451be6

Browse files
committed
2 parents 4cf15a9 + 1f00ed1 commit e451be6

9 files changed

+273
-12
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
"spatie/laravel-medialibrary": "^9.0.0",
3434
"spatie/laravel-activitylog": "^4.1",
3535
"laravel/scout": "^9.3",
36-
"yab/laravel-scout-mysql-driver": "^5.0",
3736
"cartalyst/converter": "^6.1",
37+
"yab/laravel-scout-mysql-driver": "^5.0",
3838
"kalnoy/nestedset": "^6.0"
3939
},
4040
"require-dev": {

src/Base/Purchasable.php

+7
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,11 @@ public function getIdentifier();
7070
* @return bool
7171
*/
7272
public function isShippable();
73+
74+
/**
75+
* Return the thumbnail for the purchasable item.
76+
*
77+
* @return string
78+
*/
79+
public function getThumbnail();
7380
}

src/Console/InstallGetCandy.php

+230-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,20 @@
22

33
namespace GetCandy\Console;
44

5+
use GetCandy\Models\Attribute;
6+
use GetCandy\FieldTypes\TranslatedText;
7+
use GetCandy\Hub\Models\Staff;
8+
use GetCandy\Models\AttributeGroup;
9+
use GetCandy\Models\Channel;
10+
use GetCandy\Models\Collection;
11+
use GetCandy\Models\Country;
12+
use GetCandy\Models\Currency;
13+
use GetCandy\Models\CustomerGroup;
14+
use GetCandy\Models\Language;
15+
use GetCandy\Models\ProductType;
16+
use GetCandy\Models\TaxClass;
517
use Illuminate\Console\Command;
18+
use Illuminate\Support\Facades\DB;
619
use Illuminate\Support\Facades\File;
720

821
class InstallGetCandy extends Command
@@ -28,22 +41,218 @@ class InstallGetCandy extends Command
2841
*/
2942
public function handle(): void
3043
{
31-
$this->info('Installing GetCandy...');
44+
DB::transaction(function () {
45+
$this->info('Installing GetCandy...');
3246

33-
$this->info('Publishing configuration...');
47+
$this->info('Publishing configuration...');
3448

35-
if (! $this->configExists('getcandy')) {
36-
$this->publishConfiguration();
37-
} else {
38-
if ($this->shouldOverwriteConfig()) {
39-
$this->info('Overwriting configuration file...');
40-
$this->publishConfiguration($force = true);
49+
if (! $this->configExists('getcandy')) {
50+
$this->publishConfiguration();
4151
} else {
42-
$this->info('Existing configuration was not overwritten');
52+
if ($this->shouldOverwriteConfig()) {
53+
$this->info('Overwriting configuration file...');
54+
$this->publishConfiguration($force = true);
55+
} else {
56+
$this->info('Existing configuration was not overwritten');
57+
}
4358
}
44-
}
4559

46-
$this->info('GetCandy is now installed.');
60+
$this->info('Publishing hub assets');
61+
62+
$this->publishResources();
63+
64+
if (! Country::count()) {
65+
$this->info('Importing countries');
66+
$this->call('getcandy:import:address-data');
67+
}
68+
69+
if (! Channel::whereDefault(true)->exists()) {
70+
$this->info('Setting up default channel');
71+
72+
Channel::create([
73+
'name' => 'Webstore',
74+
'handle' => 'webstore',
75+
'default' => true,
76+
'url' => 'localhost',
77+
]);
78+
}
79+
80+
if (! Staff::whereAdmin(true)->exists()) {
81+
$this->info('Create an admin user');
82+
83+
$firstname = $this->ask('Whats your first name?');
84+
$lastname = $this->ask('Whats your last name?');
85+
$email = $this->ask('Whats your email address?');
86+
$password = $this->secret('Enter a password');
87+
88+
Staff::create([
89+
'firstname' => $firstname,
90+
'lastname' => $lastname,
91+
'email' => $email,
92+
'password' => bcrypt($password),
93+
'admin' => true,
94+
]);
95+
}
96+
97+
if (! Language::count()) {
98+
$this->info('Adding default language');
99+
100+
Language::create([
101+
'code' => 'en',
102+
'name' => 'English',
103+
'default' => true,
104+
]);
105+
}
106+
107+
if (! Currency::whereDefault(true)->exists()) {
108+
$this->info('Adding a default currency (USD)');
109+
110+
Currency::create([
111+
'code' => 'USD',
112+
'name' => 'US Dollar',
113+
'exchange_rate' => 1,
114+
'format' => '${value}',
115+
'decimal_point' => '.',
116+
'thousand_point' => ',',
117+
'decimal_places' => 2,
118+
'default' => true,
119+
]);
120+
}
121+
122+
if (! CustomerGroup::whereDefault(true)->exists()) {
123+
$this->info('Adding a default customer group.');
124+
125+
CustomerGroup::create([
126+
'name' => 'Retail',
127+
'handle' => 'retail',
128+
'default' => true,
129+
]);
130+
}
131+
132+
if (! TaxClass::count()) {
133+
$this->info('Adding a default tax class.');
134+
135+
TaxClass::create([
136+
'name' => 'Default Tax Class',
137+
]);
138+
}
139+
140+
if (! Attribute::count()) {
141+
$this->info('Setting up initial attributes');
142+
143+
$group = AttributeGroup::create([
144+
'attributable_type' => ProductType::class,
145+
'name' => collect([
146+
'en' => 'Details',
147+
]),
148+
'handle' => 'details',
149+
'position' => 1,
150+
]);
151+
152+
$collectionGroup = AttributeGroup::create([
153+
'attributable_type' => Collection::class,
154+
'name' => collect([
155+
'en' => 'Details',
156+
]),
157+
'handle' => 'collection_details',
158+
'position' => 1,
159+
]);
160+
161+
Attribute::create([
162+
'attribute_type' => ProductType::class,
163+
'attribute_group_id' => $group->id,
164+
'position' => 1,
165+
'name' => [
166+
'en' => 'Name',
167+
],
168+
'handle' => 'name',
169+
'section' => 'main',
170+
'type' => TranslatedText::class,
171+
'required' => true,
172+
'default_value' => null,
173+
'configuration' => [
174+
'type' => 'text',
175+
],
176+
'system' => true,
177+
]);
178+
179+
Attribute::create([
180+
'attribute_type' => Collection::class,
181+
'attribute_group_id' => $collectionGroup->id,
182+
'position' => 1,
183+
'name' => [
184+
'en' => 'Name',
185+
],
186+
'handle' => 'name',
187+
'section' => 'main',
188+
'type' => TranslatedText::class,
189+
'required' => true,
190+
'default_value' => null,
191+
'configuration' => [
192+
'type' => 'text',
193+
],
194+
'system' => true,
195+
]);
196+
197+
Attribute::create([
198+
'attribute_type' => ProductType::class,
199+
'attribute_group_id' => $group->id,
200+
'position' => 2,
201+
'name' => [
202+
'en' => 'Description',
203+
],
204+
'handle' => 'description',
205+
'section' => 'main',
206+
'type' => TranslatedText::class,
207+
'required' => true,
208+
'default_value' => null,
209+
'configuration' => [
210+
'type' => 'richtext',
211+
],
212+
'system' => true,
213+
]);
214+
215+
Attribute::create([
216+
'attribute_type' => Collection::class,
217+
'attribute_group_id' => $collectionGroup->id,
218+
'position' => 2,
219+
'name' => [
220+
'en' => 'Description',
221+
],
222+
'handle' => 'description',
223+
'section' => 'main',
224+
'type' => TranslatedText::class,
225+
'required' => true,
226+
'default_value' => null,
227+
'configuration' => [
228+
'type' => 'richtext',
229+
],
230+
'system' => true,
231+
]);
232+
}
233+
234+
if (! ProductType::count()) {
235+
$this->info('Adding a product type.');
236+
237+
$type = ProductType::create([
238+
'name' => 'Stock',
239+
]);
240+
241+
$type->mappedAttributes()->attach(
242+
Attribute::whereAttributeType(ProductType::class)->get()->pluck('id')
243+
);
244+
}
245+
246+
$this->info('GetCandy is now installed.');
247+
248+
if ($this->confirm('Would you like to show some love by starring the repo?')) {
249+
$exec = PHP_OS_FAMILY === 'Windows' ? 'start' : 'open';
250+
251+
exec("{$exec} https://github.com/getcandy/getcandy");
252+
253+
$this->line("Thanks, you're awesome!");
254+
}
255+
});
47256
}
48257

49258
/**
@@ -54,6 +263,9 @@ public function handle(): void
54263
*/
55264
private function configExists($fileName): bool
56265
{
266+
if (! File::isDirectory(config_path($fileName))) {
267+
return false;
268+
}
57269
return ! empty(File::allFiles(config_path($fileName)));
58270
}
59271

@@ -89,4 +301,11 @@ private function publishConfiguration($forcePublish = false): void
89301

90302
$this->call('vendor:publish', $params);
91303
}
304+
305+
private function publishResources()
306+
{
307+
$this->call('vendor:publish', [
308+
'--tag' => 'getcandy:hub:public',
309+
]);
310+
}
92311
}

src/DataTypes/ShippingOption.php

+8
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,12 @@ public function isShippable()
110110
{
111111
return false;
112112
}
113+
114+
/**
115+
* {@inheritDoc}
116+
*/
117+
public function getThumbnail()
118+
{
119+
return null;
120+
}
113121
}

src/Models/CustomerGroup.php

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ class CustomerGroup extends BaseModel
1212
{
1313
use HasFactory, HasMedia, HasDefaultRecord;
1414

15+
/**
16+
* {@inheritDoc}
17+
*/
18+
protected $guarded = [];
19+
1520
/**
1621
* Return a new factory instance for the model.
1722
*

src/Models/Order.php

+4
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ public function refunds()
175175

176176
public function toSearchableArray()
177177
{
178+
if (config('scout.driver') == 'mysql') {
179+
return $this->only(array_keys($this->getAttributes()));
180+
}
181+
178182
return [
179183
'id' => $this->id,
180184
'reference' => $this->reference,

src/Models/Product.php

+4
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ public function dissociate($product, $type = null)
144144

145145
public function toSearchableArray()
146146
{
147+
if (config('scout.driver') == 'mysql') {
148+
return $this->only(array_keys($this->getAttributes()));
149+
}
150+
147151
$attributes = $this->getAttributes();
148152

149153
$data = Arr::except($attributes, 'attribute_data');

src/Models/ProductOption.php

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public function values()
6363
*/
6464
public function toSearchableArray()
6565
{
66+
if (config('scout.driver') == 'mysql') {
67+
return $this->only(array_keys($this->getAttributes()));
68+
}
6669
return [
6770
'id' => $this->id,
6871
'name' => $this->translate('name'),

src/Models/ProductVariant.php

+11
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,15 @@ public function getIdentifier()
179179
{
180180
return $this->sku;
181181
}
182+
183+
/**
184+
* {@inheritDoc}
185+
*/
186+
public function getThumbnail()
187+
{
188+
if ($thumbnail = $this->product->thumbnail) {
189+
return $thumbnail->getUrl('small');
190+
}
191+
return null;
192+
}
182193
}

0 commit comments

Comments
 (0)