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

Commit 127d0ca

Browse files
authored
Merge pull request #18 from grayloon/product-attribute-groups-performance-improvements
Product attribute groups performance improvements
2 parents eed9af0 + 20b3ef6 commit 127d0ca

File tree

9 files changed

+292
-234
lines changed

9 files changed

+292
-234
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Grayloon\Magento\Jobs;
4+
5+
use Grayloon\Magento\Magento;
6+
use Grayloon\Magento\Models\MagentoCustomAttributeType;
7+
use Illuminate\Bus\Queueable;
8+
use Illuminate\Contracts\Queue\ShouldQueue;
9+
use Illuminate\Foundation\Bus\Dispatchable;
10+
use Illuminate\Queue\InteractsWithQueue;
11+
use Illuminate\Queue\SerializesModels;
12+
13+
class UpdateProductAttributeGroup implements ShouldQueue
14+
{
15+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
16+
17+
/**
18+
* The Magento Custom Attribute Type.
19+
*
20+
* @var \Grayloon\Magento\Models\MagentoCustomAttributeType
21+
*/
22+
public $type;
23+
24+
/**
25+
* Create a new job instance.
26+
*
27+
* @return void
28+
*/
29+
public function __construct(MagentoCustomAttributeType $type)
30+
{
31+
$this->type = $type;
32+
}
33+
34+
/**
35+
* Execute the job.
36+
*
37+
* @return void
38+
*/
39+
public function handle()
40+
{
41+
$api = (new Magento())->api('productAttributes')
42+
->show($this->type->name)
43+
->json();
44+
45+
$this->type->update([
46+
'display_name' => $api['default_frontend_label'] ?? $this->type->display_name,
47+
'options' => $api['options'] ?? [],
48+
]);
49+
}
50+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Grayloon\Magento\Support;
4+
5+
use Grayloon\Magento\Jobs\UpdateProductAttributeGroup;
6+
use Grayloon\Magento\Models\MagentoCustomAttributeType;
7+
use Illuminate\Support\Str;
8+
9+
trait HasCustomAttributes
10+
{
11+
/**
12+
* Resolve the Custom Attribute Type by the Attribute Code.
13+
*
14+
* @param string $attributeCode
15+
* @return \Grayloon\Magento\Models\MagentoCustomAttributeType
16+
*/
17+
protected function resolveCustomAttributeType($attributeCode)
18+
{
19+
$type = MagentoCustomAttributeType::firstOrCreate(['name' => $attributeCode], [
20+
'display_name' => Str::title(Str::snake(Str::studly($attributeCode), ' ')),
21+
'options' => [],
22+
]);
23+
24+
if ($type->wasRecentlyCreated) {
25+
UpdateProductAttributeGroup::dispatch($type);
26+
}
27+
28+
return $type;
29+
}
30+
31+
/**
32+
* Resolve the Custom Attribute Value by the provided options.
33+
*
34+
* @param \Grayloon\Magento\Models\MagentoCustomAttributeType $type
35+
* @param string $value;
36+
* @return string|null
37+
*/
38+
protected function resolveCustomAttributeValue($type, $value)
39+
{
40+
if ($type->options) {
41+
foreach ($type->options as $option) {
42+
if ($option['value'] == $value) {
43+
return $option['label'];
44+
}
45+
}
46+
}
47+
48+
if (is_array($value)) {
49+
$value = json_encode($value);
50+
}
51+
52+
return $value;
53+
}
54+
}

src/Support/MagentoCategories.php

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
use Grayloon\Magento\Magento;
66
use Grayloon\Magento\Models\MagentoCategory;
7-
use Grayloon\Magento\Models\MagentoCustomAttributeType;
87

98
class MagentoCategories extends PaginatableMagentoService
109
{
10+
use HasCustomAttributes;
11+
1112
/**
1213
* The amount of total categories.
1314
*
@@ -108,54 +109,4 @@ protected function syncCustomAttributes($attributes, $category)
108109

109110
return $this;
110111
}
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-
}
161112
}

src/Support/MagentoCustomers.php

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
namespace Grayloon\Magento\Support;
44

55
use Grayloon\Magento\Magento;
6-
use Grayloon\Magento\Models\MagentoCustomAttributeType;
76
use Grayloon\Magento\Models\MagentoCustomer;
87

98
class MagentoCustomers extends PaginatableMagentoService
109
{
10+
use HasCustomAttributes;
11+
1112
/**
1213
* The amount of total customers.
1314
*
@@ -97,56 +98,6 @@ protected function syncCustomAttributes($attributes, $customer)
9798
return $this;
9899
}
99100

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'] ?? [],
120-
]);
121-
}
122-
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;
148-
}
149-
150101
/**
151102
* Sync the Magento 2 Customer Addresses with the Magento Customer.
152103
*

src/Support/MagentoProducts.php

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
use Grayloon\Magento\Jobs\DownloadMagentoProductImage;
66
use Grayloon\Magento\Magento;
7-
use Grayloon\Magento\Models\MagentoCustomAttributeType;
87
use Grayloon\Magento\Models\MagentoExtensionAttribute;
98
use Grayloon\Magento\Models\MagentoExtensionAttributeType;
109
use Grayloon\Magento\Models\MagentoProduct;
1110
use Grayloon\Magento\Models\MagentoProductCategory;
1211

1312
class MagentoProducts extends PaginatableMagentoService
1413
{
14+
use HasCustomAttributes;
15+
1516
/**
1617
* The amount of total products.
1718
*
@@ -199,54 +200,4 @@ protected function applyConditionalRules($attribute, $product)
199200

200201
return $this;
201202
}
202-
203-
/**
204-
* Resolve the Custom Attribute Type by the Attribute Code.
205-
*
206-
* @param string $attributeCode
207-
* @return \Grayloon\Magento\Models\MagentoCustomAttributeType
208-
*/
209-
protected function resolveCustomAttributeType($attributeCode)
210-
{
211-
$type = MagentoCustomAttributeType::where('name', $attributeCode)
212-
->first();
213-
214-
if (! $type) {
215-
$api = (new Magento())->api('productAttributes')
216-
->show($attributeCode)
217-
->json();
218-
219-
$type = MagentoCustomAttributeType::create([
220-
'name' => $attributeCode,
221-
'display_name' => $api['default_frontend_label'] ?? $attributeCode,
222-
'options' => $api['options'] ?? [],
223-
]);
224-
}
225-
226-
return $type;
227-
}
228-
229-
/**
230-
* Resolve the Custom Attribute Value by the provided options.
231-
*
232-
* @param \Grayloon\Magento\Models\MagentoCustomAttributeType $type
233-
* @param string $value;
234-
* @return string|null
235-
*/
236-
protected function resolveCustomAttributeValue($type, $value)
237-
{
238-
if ($type->options) {
239-
foreach ($type->options as $option) {
240-
if ($option['value'] == $value) {
241-
return $option['label'];
242-
}
243-
}
244-
}
245-
246-
if (is_array($value)) {
247-
$value = json_encode($value);
248-
}
249-
250-
return $value;
251-
}
252203
}

0 commit comments

Comments
 (0)