Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add attribute data to ProductOption and ProductOptionValue #1736

Draft
wants to merge 9 commits into
base: 1.x
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Lunar\Base\Migration;

class AddAttributesToProductOptionsTable extends Migration
{
public function up()
{
Schema::table($this->prefix.'product_options', function (Blueprint $table) {
$table->json('attribute_data')->nullable();
});
}

public function down()
{
Schema::table($this->prefix.'product_options', function ($table) {
$table->dropColumn('attribute_data');
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Lunar\Base\Migration;

class AddAttributesToProductOptionValuesTable extends Migration
{
public function up()
{
Schema::table($this->prefix.'product_option_values', function (Blueprint $table) {
$table->json('attribute_data')->nullable();
});
}

public function down()
{
Schema::table($this->prefix.'product_option_values', function ($table) {
$table->dropColumn('attribute_data');
});
}
}
4 changes: 4 additions & 0 deletions packages/core/src/Base/AttributeManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Lunar\Models\Customer;
use Lunar\Models\CustomerGroup;
use Lunar\Models\Product;
use Lunar\Models\ProductOption;
use Lunar\Models\ProductOptionValue;
use Lunar\Models\ProductVariant;

class AttributeManifest
Expand All @@ -24,6 +26,8 @@ class AttributeManifest
protected $baseTypes = [
Product::class,
ProductVariant::class,
ProductOption::class,
ProductOptionValue::class,
ModelsCollection::class,
Customer::class,
Brand::class,
Expand Down
20 changes: 20 additions & 0 deletions packages/core/src/Models/ProductOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Lunar\Base\BaseModel;
use Lunar\Base\Casts\AsAttributeData;
use Lunar\Base\Traits\HasAttributes;
use Lunar\Base\Traits\HasMacros;
use Lunar\Base\Traits\HasMedia;
use Lunar\Base\Traits\HasTranslations;
Expand All @@ -21,11 +24,13 @@
* @property \Illuminate\Support\Collection $label
* @property int $position
* @property ?string $handle
* @property ?array $attribute_data
* @property ?\Illuminate\Support\Carbon $created_at
* @property ?\Illuminate\Support\Carbon $updated_at
*/
class ProductOption extends BaseModel implements SpatieHasMedia
{
use HasAttributes;
use HasFactory;
use HasMacros;
use HasMedia;
Expand All @@ -41,6 +46,7 @@ class ProductOption extends BaseModel implements SpatieHasMedia
'name' => AsArrayObject::class,
'label' => AsArrayObject::class,
'shared' => 'boolean',
'attribute_data' => AsAttributeData::class,
];

/**
Expand Down Expand Up @@ -88,4 +94,18 @@ public function products(): BelongsToMany
"{$prefix}product_product_option"
)->withPivot(['position'])->orderByPivot('position');
}

/**
* Get the mapped attributes relation.
*/
public function mappedAttributes(): MorphToMany
{
$prefix = config('lunar.database.table_prefix');

return $this->morphToMany(
Attribute::class,
'attributable',
"{$prefix}attributables"
)->withTimestamps();
}
}
20 changes: 20 additions & 0 deletions packages/core/src/Models/ProductOptionValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Lunar\Base\BaseModel;
use Lunar\Base\Casts\AsAttributeData;
use Lunar\Base\Traits\HasAttributes;
use Lunar\Base\Traits\HasMacros;
use Lunar\Base\Traits\HasMedia;
use Lunar\Base\Traits\HasTranslations;
Expand All @@ -18,11 +21,13 @@
* @property int $product_option_id
* @property string $name
* @property int $position
* @property ?array $attribute_data
* @property ?\Illuminate\Support\Carbon $created_at
* @property ?\Illuminate\Support\Carbon $updated_at
*/
class ProductOptionValue extends BaseModel implements SpatieHasMedia
{
use HasAttributes;
use HasFactory;
use HasMacros;
use HasMedia;
Expand All @@ -35,6 +40,7 @@ class ProductOptionValue extends BaseModel implements SpatieHasMedia
*/
protected $casts = [
'name' => AsCollection::class,
'attribute_data' => AsAttributeData::class,
];

/**
Expand Down Expand Up @@ -69,4 +75,18 @@ public function variants(): BelongsToMany
'variant_id',
);
}

/**
* Get the mapped attributes relation.
*/
public function mappedAttributes(): MorphToMany
{
$prefix = config('lunar.database.table_prefix');

return $this->morphToMany(
Attribute::class,
'attributable',
"{$prefix}attributables"
)->withTimestamps();
}
}