Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit eed9af0

Browse files
authored
Merge pull request #16 from grayloon/custom-attribute-resolver
Resolve custom attribute values
2 parents 5f9e29e + fc108dc commit eed9af0

18 files changed

+746
-46
lines changed

README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,6 @@ Get a list of all categories:
6969
$magento->api('categories')->all($pageSize = 50, $currentPage = 1);
7070
```
7171

72-
Get a count of all categories:
73-
```php
74-
$magento->api('categories')->count();
75-
```
76-
7772
#### Customers
7873

7974
Get a list of customers:
@@ -99,14 +94,15 @@ Get a list of products:
9994
$magento->api('products')->all($pageSize = 50, $currentPage = 1);
10095
```
10196

102-
Get a count of all products:
97+
Get info about a product by the product SKU:
10398
```php
104-
$magento->api('products')->count();
99+
$magento->api('products')->show($sku);
105100
```
106101

107-
Get info about a product by the product SKU:
102+
#### Product Attributes
103+
Retrieve specific product attribute information:
108104
```php
109-
$magento->api('products')->show($sku);
105+
$magento->api('productAttributes')->show($attributeCode)
110106
```
111107

112108
#### Schema

src/Api/ProductAttributes.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Grayloon\Magento\Api;
4+
5+
class ProductAttributes extends AbstractApi
6+
{
7+
/**
8+
* Retrieve specific product attribute information.
9+
*
10+
* @param string $attribute
11+
* @return array
12+
*/
13+
public function show($attribute)
14+
{
15+
return $this->get('/products/attributes/'.$attribute);
16+
}
17+
}

src/Database/Factories/MagentoCustomAttributeFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
use Faker\Generator as Faker;
66
use Grayloon\Magento\Models\MagentoCategory;
77
use Grayloon\Magento\Models\MagentoCustomAttribute;
8+
use Grayloon\Magento\Models\MagentoCustomAttributeType;
89
use Grayloon\Magento\Models\MagentoProduct;
910

1011
$factory->define(MagentoCustomAttribute::class, function (Faker $faker) {
1112
return [
12-
'attribute_type' => $faker->bs,
13+
'attribute_type' => $faker->catchPhrase,
14+
'attribute_type_id' => factory(MagentoCustomAttributeType::class)->create(),
1315
'value' => $faker->catchPhrase,
1416
'attributable_type' => $faker->randomElement([MagentoProduct::class, MagentoCategory::class]),
1517
'attributable_id' => fn (array $attribute) => factory($attribute['attributable_type']),
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
/** @var \Illuminate\Database\Eloquent\Factory $factory */
4+
5+
use Faker\Generator as Faker;
6+
use Grayloon\Magento\Models\MagentoCustomAttributeType;
7+
8+
$factory->define(MagentoCustomAttributeType::class, function (Faker $faker) {
9+
return [
10+
'name' => $faker->catchPhrase,
11+
'display_name' => $faker->catchPhrase,
12+
];
13+
});

src/Database/Migrations/2020_07_06_214528_create_magento_custom_attributes_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function up()
1616
Schema::create('magento_custom_attributes', function (Blueprint $table) {
1717
$table->id();
1818
$table->string('attribute_type')->index();
19+
$table->integer('attribute_type_id')->index();
1920
$table->text('value');
2021
$table->bigInteger('attributable_id')->index();
2122
$table->string('attributable_type')->index();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateMagentoCustomAttributeTypesTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('magento_custom_attribute_types', function (Blueprint $table) {
17+
$table->id();
18+
$table->string('name')->index();
19+
$table->text('display_name');
20+
$table->text('options')->nullable();
21+
$table->timestamps();
22+
});
23+
}
24+
25+
/**
26+
* Reverse the migrations.
27+
*
28+
* @return void
29+
*/
30+
public function down()
31+
{
32+
Schema::dropIfExists('magento_custom_attribute_options');
33+
}
34+
}

src/Models/MagentoCustomAttribute.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,14 @@ public function attributable()
2222
{
2323
return $this->morphTo();
2424
}
25+
26+
/**
27+
* The 'attribute_type_id" belongs to the Custom Attribute Type.
28+
*
29+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
30+
*/
31+
public function type()
32+
{
33+
return $this->belongsTo(MagentoCustomAttributeType::class, 'attribute_type_id');
34+
}
2535
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Grayloon\Magento\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class MagentoCustomAttributeType extends Model
8+
{
9+
/**
10+
* The attributes that aren't mass assignable.
11+
*
12+
* @var array
13+
*/
14+
protected $guarded = [];
15+
16+
/**
17+
* The attributes that should be cast to native types.
18+
*
19+
* @var array
20+
*/
21+
protected $casts = [
22+
'options' => 'array',
23+
];
24+
}

src/Support/MagentoCategories.php

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Grayloon\Magento\Magento;
66
use Grayloon\Magento\Models\MagentoCategory;
7+
use Grayloon\Magento\Models\MagentoCustomAttributeType;
78

89
class MagentoCategories extends PaginatableMagentoService
910
{
@@ -94,15 +95,67 @@ protected function findAttributeByKey($key, $attributes)
9495
protected function syncCustomAttributes($attributes, $category)
9596
{
9697
foreach ($attributes as $attribute) {
97-
if (is_array($attribute['value'])) {
98-
$attribute['value'] = json_encode($attribute['value']);
99-
}
98+
$type = $this->resolveCustomAttributeType($attribute['attribute_code']);
99+
$value = $this->resolveCustomAttributeValue($type, $attribute['value']);
100100

101-
$category->customAttributes()->updateOrCreate(['attribute_type' => $attribute['attribute_code']], [
102-
'value' => $attribute['value'],
103-
]);
101+
$category
102+
->customAttributes()
103+
->updateOrCreate(['attribute_type_id' => $type->id], [
104+
'attribute_type' => $attribute['attribute_code'],
105+
'value' => $value,
106+
]);
104107
}
105108

106109
return $this;
107110
}
111+
112+
/**
113+
* Resolve the Custom Attribute Type by the Attribute Code.
114+
*
115+
* @param string $attributeCode
116+
* @return \Grayloon\Magento\Models\MagentoCustomAttributeType
117+
*/
118+
protected function resolveCustomAttributeType($attributeCode)
119+
{
120+
$type = MagentoCustomAttributeType::where('name', $attributeCode)
121+
->first();
122+
123+
if (! $type) {
124+
$api = (new Magento())->api('productAttributes')
125+
->show($attributeCode)
126+
->json();
127+
128+
$type = MagentoCustomAttributeType::create([
129+
'name' => $attributeCode,
130+
'display_name' => $api['default_frontend_label'] ?? $attributeCode,
131+
'options' => $api['options'] ?? [],
132+
]);
133+
}
134+
135+
return $type;
136+
}
137+
138+
/**
139+
* Resolve the Custom Attribute Value by the provided options.
140+
*
141+
* @param \Grayloon\Magento\Models\MagentoCustomAttributeType $type
142+
* @param string $value;
143+
* @return string|null
144+
*/
145+
protected function resolveCustomAttributeValue($type, $value)
146+
{
147+
if ($type->options) {
148+
foreach ($type->options as $option) {
149+
if ($option['value'] == $value) {
150+
return $option['label'];
151+
}
152+
}
153+
}
154+
155+
if (is_array($value)) {
156+
$value = json_encode($value);
157+
}
158+
159+
return $value;
160+
}
108161
}

src/Support/MagentoCustomers.php

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Grayloon\Magento\Support;
44

55
use Grayloon\Magento\Magento;
6+
use Grayloon\Magento\Models\MagentoCustomAttributeType;
67
use Grayloon\Magento\Models\MagentoCustomer;
78

89
class MagentoCustomers extends PaginatableMagentoService
@@ -82,16 +83,68 @@ protected function syncCustomAttributes($attributes, $customer)
8283
}
8384

8485
foreach ($attributes as $attribute) {
85-
if (is_array($attribute['value'])) {
86-
$attribute['value'] = json_encode($attribute['value']);
87-
}
86+
$type = $this->resolveCustomAttributeType($attribute['attribute_code']);
87+
$value = $this->resolveCustomAttributeValue($type, $attribute['value']);
88+
89+
$customer
90+
->customAttributes()
91+
->updateOrCreate(['attribute_type_id' => $type->id], [
92+
'attribute_type' => $attribute['attribute_code'],
93+
'value' => $value,
94+
]);
95+
}
96+
97+
return $this;
98+
}
8899

89-
$customer->customAttributes()->updateOrCreate(['attribute_type' => $attribute['attribute_code']], [
90-
'value' => $attribute['value'] ?? '',
100+
/**
101+
* Resolve the Custom Attribute Type by the Attribute Code.
102+
*
103+
* @param string $attributeCode
104+
* @return \Grayloon\Magento\Models\MagentoCustomAttributeType
105+
*/
106+
protected function resolveCustomAttributeType($attributeCode)
107+
{
108+
$type = MagentoCustomAttributeType::where('name', $attributeCode)
109+
->first();
110+
111+
if (! $type) {
112+
$api = (new Magento())->api('productAttributes')
113+
->show($attributeCode)
114+
->json();
115+
116+
$type = MagentoCustomAttributeType::create([
117+
'name' => $attributeCode,
118+
'display_name' => $api['default_frontend_label'] ?? $attributeCode,
119+
'options' => $api['options'] ?? [],
91120
]);
92121
}
93122

94-
return $this;
123+
return $type;
124+
}
125+
126+
/**
127+
* Resolve the Custom Attribute Value by the provided options.
128+
*
129+
* @param \Grayloon\Magento\Models\MagentoCustomAttributeType $type
130+
* @param string $value;
131+
* @return string|null
132+
*/
133+
protected function resolveCustomAttributeValue($type, $value)
134+
{
135+
if ($type->options) {
136+
foreach ($type->options as $option) {
137+
if ($option['value'] == $value) {
138+
return $option['label'];
139+
}
140+
}
141+
}
142+
143+
if (is_array($value)) {
144+
$value = json_encode($value);
145+
}
146+
147+
return $value;
95148
}
96149

97150
/**

0 commit comments

Comments
 (0)