Skip to content
This repository was archived by the owner on Oct 27, 2022. It is now read-only.

Commit a7b6e8a

Browse files
authored
Merge pull request #29 from grayloon/customer-group-pricing
Ability to store and sync tier pricing
2 parents f2abf50 + ec10643 commit a7b6e8a

20 files changed

+633
-6
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"require": {
1919
"php": "^7.4",
2020
"doctrine/dbal": "^2.9|^3.0",
21-
"grayloon/laravel-magento-api": "0.*",
21+
"grayloon/laravel-magento-api": "0.x",
2222
"guzzlehttp/guzzle": "^7.0",
2323
"illuminate/support": "^8.0"
2424
},
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace Grayloon\MagentoStorage\Console;
4+
5+
use Exception;
6+
use Grayloon\Magento\Magento;
7+
use Grayloon\MagentoStorage\Models\MagentoCustomerGroup;
8+
use Grayloon\MagentoStorage\Support\HasMagentoCustomerGroups;
9+
use Illuminate\Console\Command;
10+
11+
class SyncMagentoCustomerGroups extends Command
12+
{
13+
use HasMagentoCustomerGroups;
14+
15+
protected $signature = 'magento:sync-customer-groups';
16+
protected $description = 'Syncs the Magento Customer Groups from the Magento API.';
17+
18+
protected $bar;
19+
protected $count;
20+
protected $pages;
21+
protected $pageSize = 100;
22+
protected $magento;
23+
24+
public function handle(Magento $magento)
25+
{
26+
$this->magento = $magento;
27+
28+
$this->info('Retrieving Magento Customer Groups from Magento...');
29+
$this->resolveTotalGroups();
30+
$this->info("{$this->count} Groups Found.");
31+
32+
if (! $this->count) {
33+
return $this->error('No Customer Groups found. Sync Customer Group cancelled.');
34+
}
35+
36+
$this->newLine();
37+
$this->bar->start();
38+
39+
for ($currentPage = 1; $this->pages > $currentPage; $currentPage++) {
40+
$groups = $this->magento->api('customerGroups')
41+
->search($this->pageSize, $currentPage);
42+
43+
if (! $groups->ok()) {
44+
throw new Exception('Unable to locate Customer Groups.');
45+
}
46+
47+
$this->updateOrCreateCustomerGroups($groups->json());
48+
}
49+
50+
$this->bar->finish();
51+
$this->newLine();
52+
$this->newLine();
53+
$this->info(MagentoCustomerGroup::count() . ' groups synced from the Magento API to your application.');
54+
}
55+
56+
/**
57+
* The total count of Customer Groups.
58+
*
59+
* @throws \Exception
60+
* @return $this
61+
*/
62+
protected function resolveTotalGroups()
63+
{
64+
if (config('magento.store_code')) {
65+
$this->newLine();
66+
$this->info('Default Store Code: "'. config('magento.store_code') .'" is configured in your env.');
67+
$this->info('We will only sync those customer groups associated with "'. config('magento.store_code') .'".');
68+
}
69+
70+
$groups = $this->magento->api('customerGroups')->search();
71+
72+
if (! $groups->ok() || ! isset($groups->json()['total_count'])) {
73+
throw new Exception('Unable to locate Customer Groups.');
74+
}
75+
76+
$this->count = $groups->json()['total_count'];
77+
$this->bar = $this->output
78+
->createProgressBar($this->count);
79+
$this->pages = ceil(($this->count / $this->pageSize) + 1);
80+
81+
return $this;
82+
}
83+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Grayloon\MagentoStorage\Database\Factories;
4+
5+
use Grayloon\MagentoStorage\Models\MagentoCustomerGroup;
6+
use Illuminate\Database\Eloquent\Factories\Factory;
7+
8+
class MagentoCustomerGroupFactory extends Factory
9+
{
10+
/**
11+
* The name of the factory's corresponding model.
12+
*
13+
* @var string
14+
*/
15+
protected $model = MagentoCustomerGroup::class;
16+
17+
/**
18+
* Define the model's default state.
19+
*
20+
* @return array
21+
*/
22+
public function definition()
23+
{
24+
return [
25+
'code' => $this->faker->company,
26+
'tax_class_id' => $this->faker->randomNumber(),
27+
'tax_class_name' => $this->faker->catchPhrase,
28+
];
29+
}
30+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Grayloon\MagentoStorage\Database\Factories;
4+
5+
use Grayloon\MagentoStorage\Models\MagentoTierPrice;
6+
use Illuminate\Database\Eloquent\Factories\Factory;
7+
8+
class MagentoTierPriceFactory extends Factory
9+
{
10+
/**
11+
* The name of the factory's corresponding model.
12+
*
13+
* @var string
14+
*/
15+
protected $model = MagentoTierPrice::class;
16+
17+
/**
18+
* Define the model's default state.
19+
*
20+
* @return array
21+
*/
22+
public function definition()
23+
{
24+
return [
25+
'magento_product_id' => MagentoProductFactory::new(),
26+
'customer_group_id' => MagentoCustomerGroupFactory::new(),
27+
'value' => $this->faker->randomFloat(2, 1, 500),
28+
'quantity' => $this->faker->numberBetween(1, 100),
29+
'extension_attributes' => [],
30+
];
31+
}
32+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateMagentoCustomerGroupsTable extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::create('magento_customer_groups', function (Blueprint $table) {
12+
$table->id();
13+
$table->string('code');
14+
$table->bigInteger('tax_class_id');
15+
$table->string('tax_class_name');
16+
$table->timestamps();
17+
$table->timestamp('synced_at')->useCurrent();
18+
});
19+
}
20+
21+
public function down()
22+
{
23+
Schema::dropIfExists('magento_customer_groups');
24+
}
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateMagentoTierPricesTable extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::create('magento_tier_prices', function (Blueprint $table) {
12+
$table->id();
13+
$table->bigInteger('magento_product_id')->index();
14+
$table->bigInteger('customer_group_id')->index();
15+
$table->decimal('value', 15, 4)->default(0.00);
16+
$table->integer('quantity')->default(0);
17+
$table->text('extension_attributes')->nullable();
18+
$table->timestamps();
19+
});
20+
}
21+
22+
public function down()
23+
{
24+
Schema::dropIfExists('magento_tier_prices');
25+
}
26+
}

src/Jobs/SyncMagentoProductCategory.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace Grayloon\MagentoStorage\Jobs;
44

5+
use Grayloon\MagentoStorage\Models\MagentoCategory;
6+
use Grayloon\MagentoStorage\Models\MagentoProduct;
7+
use Grayloon\MagentoStorage\Models\MagentoProductCategory;
58
use Illuminate\Bus\Queueable;
6-
use Illuminate\Queue\SerializesModels;
7-
use Illuminate\Queue\InteractsWithQueue;
89
use Illuminate\Contracts\Queue\ShouldQueue;
910
use Illuminate\Foundation\Bus\Dispatchable;
10-
use Grayloon\MagentoStorage\Models\MagentoProduct;
11-
use Grayloon\MagentoStorage\Models\MagentoCategory;
12-
use Grayloon\MagentoStorage\Models\MagentoProductCategory;
11+
use Illuminate\Queue\InteractsWithQueue;
12+
use Illuminate\Queue\SerializesModels;
1313

1414
class SyncMagentoProductCategory implements ShouldQueue
1515
{

src/MagentoStorageServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public function register()
8484
Console\SyncMagentoCustomAttributeTypesCommand::class,
8585
Console\SyncMagentoWebsitesCommand::class,
8686
Console\MagentoCleanAttributesCommand::class,
87+
Console\SyncMagentoCustomerGroups::class,
8788
]);
8889
}
8990
}

src/Models/MagentoCustomer.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,14 @@ public function addresses()
5959
{
6060
return $this->hasMany(MagentoCustomerAddress::class, 'customer_id');
6161
}
62+
63+
/**
64+
* The Group ID belongs to the Magento Customer Group.
65+
*
66+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
67+
*/
68+
public function customerGroup()
69+
{
70+
return $this->belongsTo(MagentoCustomerGroup::class, 'group_id');
71+
}
6272
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Grayloon\MagentoStorage\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class MagentoCustomerGroup extends Model
9+
{
10+
use HasFactory;
11+
12+
/**
13+
* The attributes that are mass assignable.
14+
*
15+
* @var array
16+
*/
17+
protected $fillable = [
18+
'id',
19+
'code',
20+
'tax_class_id',
21+
'tax_class_name',
22+
'synced_at',
23+
];
24+
25+
/**
26+
* The attributes that should be cast to native types.
27+
*
28+
* @var array
29+
*/
30+
protected $casts = [
31+
'synced_at' => 'datetime',
32+
];
33+
}

0 commit comments

Comments
 (0)