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

Commit d634a85

Browse files
committed
feat: resolve existing attribute value on options retrieval
1 parent 127d0ca commit d634a85

File tree

3 files changed

+108
-2
lines changed

3 files changed

+108
-2
lines changed

src/Jobs/UpdateProductAttributeGroup.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace Grayloon\Magento\Jobs;
44

55
use Grayloon\Magento\Magento;
6+
use Grayloon\Magento\Models\MagentoCustomAttribute;
67
use Grayloon\Magento\Models\MagentoCustomAttributeType;
8+
use Grayloon\Magento\Support\HasCustomAttributes;
79
use Illuminate\Bus\Queueable;
810
use Illuminate\Contracts\Queue\ShouldQueue;
911
use Illuminate\Foundation\Bus\Dispatchable;
@@ -12,7 +14,7 @@
1214

1315
class UpdateProductAttributeGroup implements ShouldQueue
1416
{
15-
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
17+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, HasCustomAttributes;
1618

1719
/**
1820
* The Magento Custom Attribute Type.
@@ -46,5 +48,7 @@ public function handle()
4648
'display_name' => $api['default_frontend_label'] ?? $this->type->display_name,
4749
'options' => $api['options'] ?? [],
4850
]);
51+
52+
$this->updateCustomAttributeTypeValues($this->type);
4953
}
5054
}

src/Support/HasCustomAttributes.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace Grayloon\Magento\Support;
44

5+
use Illuminate\Support\Str;
6+
use Grayloon\Magento\Models\MagentoCustomAttribute;
57
use Grayloon\Magento\Jobs\UpdateProductAttributeGroup;
68
use Grayloon\Magento\Models\MagentoCustomAttributeType;
7-
use Illuminate\Support\Str;
89

910
trait HasCustomAttributes
1011
{
@@ -51,4 +52,21 @@ protected function resolveCustomAttributeValue($type, $value)
5152

5253
return $value;
5354
}
55+
56+
/**
57+
* Mass updates all Custom Attribute values from the resolved options.
58+
*
59+
* @param [type] $type
60+
* @return void
61+
*/
62+
protected function updateCustomAttributeTypeValues($type)
63+
{
64+
MagentoCustomAttribute::where('attribute_type_id', $type->id)
65+
->get()
66+
->each(fn($attribute) => $attribute->update([
67+
'value' => $this->resolveCustomAttributeValue($type, $attribute->value)
68+
]));
69+
70+
return $this;
71+
}
5472
}

tests/Support/HasCustomAttributesTest.php

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

55
use Grayloon\Magento\Jobs\UpdateProductAttributeGroup;
6+
use Grayloon\Magento\Models\MagentoCustomAttribute;
67
use Grayloon\Magento\Models\MagentoCustomAttributeType;
78
use Grayloon\Magento\Support\HasCustomAttributes;
89
use Grayloon\Magento\Tests\TestCase;
@@ -91,6 +92,84 @@ public function test_resolves_raw_value_from_option_not_supplied_in_options()
9192

9293
$this->assertEquals('Unknown', $value);
9394
}
95+
96+
public function test_updates_attribute_value_based_on_options()
97+
{
98+
$type = factory(MagentoCustomAttributeType::class)->create([
99+
'name' => 'foo_bar',
100+
'options' => [
101+
[
102+
'label' => 'New York',
103+
'value' => '1',
104+
],
105+
[
106+
'label' => 'Los Angeles',
107+
'value' => '2',
108+
],
109+
],
110+
]);
111+
112+
$attribute = factory(MagentoCustomAttribute::class)->create([
113+
'attribute_type_id' => $type->id,
114+
'value' => '1',
115+
]);
116+
117+
(new FakeSupportingClass)->exposedUpdateCustomAttributeTypeValues($type);
118+
119+
$this->assertEquals('New York', $attribute->fresh()->value);
120+
}
121+
122+
public function test_updates_multiple_attribute_value_based_on_options()
123+
{
124+
$type = factory(MagentoCustomAttributeType::class)->create([
125+
'name' => 'foo_bar',
126+
'options' => [
127+
[
128+
'label' => 'New York',
129+
'value' => '1',
130+
],
131+
[
132+
'label' => 'Los Angeles',
133+
'value' => '2',
134+
],
135+
],
136+
]);
137+
138+
$attributes = factory(MagentoCustomAttribute::class, 10)->create([
139+
'attribute_type_id' => $type->id,
140+
'value' => '1',
141+
]);
142+
143+
(new FakeSupportingClass)->exposedUpdateCustomAttributeTypeValues($type);
144+
145+
$this->assertEquals(10, $attributes->fresh()->where('value', 'New York')->count());
146+
}
147+
148+
public function test_missing_option_keeps_raw_attribute_value()
149+
{
150+
$type = factory(MagentoCustomAttributeType::class)->create([
151+
'name' => 'foo_bar',
152+
'options' => [
153+
[
154+
'label' => 'New York',
155+
'value' => '1',
156+
],
157+
[
158+
'label' => 'Los Angeles',
159+
'value' => '2',
160+
],
161+
],
162+
]);
163+
164+
$attribute = factory(MagentoCustomAttribute::class)->create([
165+
'attribute_type_id' => $type->id,
166+
'value' => 'Unknown',
167+
]);
168+
169+
(new FakeSupportingClass)->exposedUpdateCustomAttributeTypeValues($type);
170+
171+
$this->assertEquals('Unknown', $attribute->fresh()->value);
172+
}
94173
}
95174

96175
class FakeSupportingClass
@@ -106,4 +185,9 @@ public function exposedResolveCustomAttributeValue($type, $value)
106185
{
107186
return $this->resolveCustomAttributeValue($type, $value);
108187
}
188+
189+
public function exposedUpdateCustomAttributeTypeValues($type)
190+
{
191+
return $this->updateCustomAttributeTypeValues($type);
192+
}
109193
}

0 commit comments

Comments
 (0)