Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ Get a list of all categories:
$magento->api('categories')->all($pageSize = 50, $currentPage = 1);
```

Get a count of all categories:
```php
$magento->api('categories')->count();
```

#### Customers

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

Get a count of all products:
Get info about a product by the product SKU:
```php
$magento->api('products')->count();
$magento->api('products')->show($sku);
```

Get info about a product by the product SKU:
#### Product Attributes
Retrieve specific product attribute information:
```php
$magento->api('products')->show($sku);
$magento->api('productAttributes')->show($attributeCode)
```

#### Schema
Expand Down
17 changes: 17 additions & 0 deletions src/Api/ProductAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Grayloon\Magento\Api;

class ProductAttributes extends AbstractApi
{
/**
* Retrieve specific product attribute information.
*
* @param string $attribute
* @return array
*/
public function show($attribute)
{
return $this->get('/products/attributes/'.$attribute);
}
}
4 changes: 3 additions & 1 deletion src/Database/Factories/MagentoCustomAttributeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
use Faker\Generator as Faker;
use Grayloon\Magento\Models\MagentoCategory;
use Grayloon\Magento\Models\MagentoCustomAttribute;
use Grayloon\Magento\Models\MagentoCustomAttributeType;
use Grayloon\Magento\Models\MagentoProduct;

$factory->define(MagentoCustomAttribute::class, function (Faker $faker) {
return [
'attribute_type' => $faker->bs,
'attribute_type' => $faker->catchPhrase,
'attribute_type_id' => factory(MagentoCustomAttributeType::class)->create(),
'value' => $faker->catchPhrase,
'attributable_type' => $faker->randomElement([MagentoProduct::class, MagentoCategory::class]),
'attributable_id' => fn (array $attribute) => factory($attribute['attributable_type']),
Expand Down
13 changes: 13 additions & 0 deletions src/Database/Factories/MagentoCustomAttributeTypeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use Faker\Generator as Faker;
use Grayloon\Magento\Models\MagentoCustomAttributeType;

$factory->define(MagentoCustomAttributeType::class, function (Faker $faker) {
return [
'name' => $faker->catchPhrase,
'display_name' => $faker->catchPhrase,
];
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function up()
Schema::create('magento_custom_attributes', function (Blueprint $table) {
$table->id();
$table->string('attribute_type')->index();
$table->integer('attribute_type_id')->index();
$table->text('value');
$table->bigInteger('attributable_id')->index();
$table->string('attributable_type')->index();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateMagentoCustomAttributeTypesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('magento_custom_attribute_types', function (Blueprint $table) {
$table->id();
$table->string('name')->index();
$table->text('display_name');
$table->text('options')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('magento_custom_attribute_options');
}
}
10 changes: 10 additions & 0 deletions src/Models/MagentoCustomAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,14 @@ public function attributable()
{
return $this->morphTo();
}

/**
* The 'attribute_type_id" belongs to the Custom Attribute Type.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function type()
{
return $this->belongsTo(MagentoCustomAttributeType::class, 'attribute_type_id');
}
}
24 changes: 24 additions & 0 deletions src/Models/MagentoCustomAttributeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Grayloon\Magento\Models;

use Illuminate\Database\Eloquent\Model;

class MagentoCustomAttributeType extends Model
{
/**
* The attributes that aren't mass assignable.
*
* @var array
*/
protected $guarded = [];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'options' => 'array',
];
}
65 changes: 59 additions & 6 deletions src/Support/MagentoCategories.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Grayloon\Magento\Magento;
use Grayloon\Magento\Models\MagentoCategory;
use Grayloon\Magento\Models\MagentoCustomAttributeType;

class MagentoCategories extends PaginatableMagentoService
{
Expand Down Expand Up @@ -94,15 +95,67 @@ protected function findAttributeByKey($key, $attributes)
protected function syncCustomAttributes($attributes, $category)
{
foreach ($attributes as $attribute) {
if (is_array($attribute['value'])) {
$attribute['value'] = json_encode($attribute['value']);
}
$type = $this->resolveCustomAttributeType($attribute['attribute_code']);
$value = $this->resolveCustomAttributeValue($type, $attribute['value']);

$category->customAttributes()->updateOrCreate(['attribute_type' => $attribute['attribute_code']], [
'value' => $attribute['value'],
]);
$category
->customAttributes()
->updateOrCreate(['attribute_type_id' => $type->id], [
'attribute_type' => $attribute['attribute_code'],
'value' => $value,
]);
}

return $this;
}

/**
* Resolve the Custom Attribute Type by the Attribute Code.
*
* @param string $attributeCode
* @return \Grayloon\Magento\Models\MagentoCustomAttributeType
*/
protected function resolveCustomAttributeType($attributeCode)
{
$type = MagentoCustomAttributeType::where('name', $attributeCode)
->first();

if (! $type) {
$api = (new Magento())->api('productAttributes')
->show($attributeCode)
->json();

$type = MagentoCustomAttributeType::create([
'name' => $attributeCode,
'display_name' => $api['default_frontend_label'] ?? $attributeCode,
'options' => $api['options'] ?? [],
]);
}

return $type;
}

/**
* Resolve the Custom Attribute Value by the provided options.
*
* @param \Grayloon\Magento\Models\MagentoCustomAttributeType $type
* @param string $value;
* @return string|null
*/
protected function resolveCustomAttributeValue($type, $value)
{
if ($type->options) {
foreach ($type->options as $option) {
if ($option['value'] == $value) {
return $option['label'];
}
}
}

if (is_array($value)) {
$value = json_encode($value);
}

return $value;
}
}
65 changes: 59 additions & 6 deletions src/Support/MagentoCustomers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Grayloon\Magento\Support;

use Grayloon\Magento\Magento;
use Grayloon\Magento\Models\MagentoCustomAttributeType;
use Grayloon\Magento\Models\MagentoCustomer;

class MagentoCustomers extends PaginatableMagentoService
Expand Down Expand Up @@ -82,16 +83,68 @@ protected function syncCustomAttributes($attributes, $customer)
}

foreach ($attributes as $attribute) {
if (is_array($attribute['value'])) {
$attribute['value'] = json_encode($attribute['value']);
}
$type = $this->resolveCustomAttributeType($attribute['attribute_code']);
$value = $this->resolveCustomAttributeValue($type, $attribute['value']);

$customer
->customAttributes()
->updateOrCreate(['attribute_type_id' => $type->id], [
'attribute_type' => $attribute['attribute_code'],
'value' => $value,
]);
}

return $this;
}

$customer->customAttributes()->updateOrCreate(['attribute_type' => $attribute['attribute_code']], [
'value' => $attribute['value'] ?? '',
/**
* Resolve the Custom Attribute Type by the Attribute Code.
*
* @param string $attributeCode
* @return \Grayloon\Magento\Models\MagentoCustomAttributeType
*/
protected function resolveCustomAttributeType($attributeCode)
{
$type = MagentoCustomAttributeType::where('name', $attributeCode)
->first();

if (! $type) {
$api = (new Magento())->api('productAttributes')
->show($attributeCode)
->json();

$type = MagentoCustomAttributeType::create([
'name' => $attributeCode,
'display_name' => $api['default_frontend_label'] ?? $attributeCode,
'options' => $api['options'] ?? [],
]);
}

return $this;
return $type;
}

/**
* Resolve the Custom Attribute Value by the provided options.
*
* @param \Grayloon\Magento\Models\MagentoCustomAttributeType $type
* @param string $value;
* @return string|null
*/
protected function resolveCustomAttributeValue($type, $value)
{
if ($type->options) {
foreach ($type->options as $option) {
if ($option['value'] == $value) {
return $option['label'];
}
}
}

if (is_array($value)) {
$value = json_encode($value);
}

return $value;
}

/**
Expand Down
Loading