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

Support attributes on customer groups #1443

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 AddAttributesToCustomerGroupsTable extends Migration
{
public function up()
{
Schema::table($this->prefix.'customer_groups', function (Blueprint $table) {
$table->json('attribute_data')->after('default')->nullable();
});
}

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

Expand All @@ -25,6 +26,7 @@ class AttributeManifest
ProductVariant::class,
ModelsCollection::class,
Customer::class,
CustomerGroup::class,
Brand::class,
// Order::class,
];
Expand Down
29 changes: 29 additions & 0 deletions packages/core/src/Models/CustomerGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Lunar\Base\BaseModel;
use Lunar\Base\Casts\AsAttributeData;
use Lunar\Base\Traits\HasAttributes;
use Lunar\Base\Traits\HasDefaultRecord;
use Lunar\Base\Traits\HasMacros;
use Lunar\Base\Traits\HasTranslations;
use Lunar\Database\Factories\CustomerGroupFactory;

/**
* @property int $id
* @property string $name
* @property string $handle
* @property bool $default
* @property ?array $attribute_data
* @property ?\Illuminate\Support\Carbon $created_at
* @property ?\Illuminate\Support\Carbon $updated_at
*/
Expand All @@ -22,11 +26,20 @@ class CustomerGroup extends BaseModel
use HasDefaultRecord;
use HasFactory;
use HasMacros;
use HasAttributes;
use HasTranslations;

/**
* {@inheritDoc}
*/
protected $guarded = [];

/**
* {@inheritDoc}
*/
protected $casts = [
'attribute_data' => AsAttributeData::class,
];

/**
* Return a new factory instance for the model.
Expand All @@ -48,4 +61,20 @@ public function customers(): BelongsToMany
"{$prefix}customer_customer_group"
)->withTimestamps();
}

/**
* Get the mapped attributes relation.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function mappedAttributes()
{
$prefix = config('lunar.database.table_prefix');

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