feat: Enhance product support in Cart and OrderItem models

- Added new fields 'billing_cycle' and 'product_pricing_id' to Cart and OrderItem models.
- Updated product retrieval logic to support both new and legacy systems.
- Introduced relationships for Product and ProductPricings in Cart and OrderItem models.
- Created migration to add new fields to the carts and order_item tables.
- Updated frontend service index to handle new pricing structure and billing cycles.
- Improved UI components for better user experience in product selection and pricing display.
- Refactored action buttons in various views for consistency and improved styling.
This commit is contained in:
pbao9
2026-02-09 08:04:56 +07:00
parent 4d4bf020d4
commit 057353a2e1
15 changed files with 590 additions and 250 deletions
+38
View File
@@ -16,14 +16,52 @@ class Cart extends Model
'price',
'quantity',
'months',
'billing_cycle', // Add new field for billing cycle
'product_pricing_id', // Add new field to link to ProductPricings
];
/**
* Get product - support both new and legacy system
*/
public function product()
{
// New flow: Check if product exists in Product table first
if ($this->product_id) {
$product = Product::find($this->product_id);
if ($product) {
return $product;
}
}
// Legacy flow: fallback to old system based on type_product
return match ($this->type_product) {
3 => $this->belongsTo(Hosting::class, 'product_id'),
4 => $this->belongsTo(VPS::class, 'product_id'),
default => null,
};
}
/**
* Relationship: Cart belongs to Product (new system)
*/
public function productNew()
{
return $this->belongsTo(Product::class, 'product_id');
}
/**
* Relationship: Cart belongs to Customer
*/
public function customer()
{
return $this->belongsTo(Customer::class, 'customer_id');
}
/**
* Relationship: Cart has pricing info
*/
public function pricing()
{
return $this->belongsTo(ProductPricings::class, 'product_pricing_id');
}
}
+25
View File
@@ -25,6 +25,15 @@ class OrderItem extends Model
public function getProductAttribute()
{
// New flow: Check if product exists in Product table first
if ($this->product_id) {
$product = Product::find($this->product_id);
if ($product) {
return $product;
}
}
// Legacy flow: fallback to old system
return match ($this->product_type) {
3 => Hosting::find($this->product_id),
4 => Vps::find($this->product_id),
@@ -32,6 +41,22 @@ class OrderItem extends Model
};
}
/**
* Relationship: OrderItem belongs to Product (new system)
*/
public function product()
{
return $this->belongsTo(Product::class, 'product_id');
}
/**
* Relationship: OrderItem has pricing info
*/
public function pricing()
{
return $this->belongsTo(ProductPricings::class, 'product_pricing_id');
}
public function os()
+20
View File
@@ -39,4 +39,24 @@ class Os extends Model
->withTimestamps()
->withPivot('product_type');
}
/**
* Many-to-many relationship với Products (new table: os_product)
*/
public function products()
{
return $this->belongsToMany(\App\Models\Product::class, 'os_product', 'os_id', 'product_id')
->withTimestamps();
}
/**
* Get display name with OS Group
*/
public function getNameAttribute()
{
if ($this->osGroup) {
return $this->osGroup->name . ' ' . $this->version;
}
return $this->version;
}
}
+43
View File
@@ -79,6 +79,22 @@ class Product extends Model
return $this->hasMany(ProductPricings::class, 'product_id');
}
/**
* Relationship: Product has many order items
*/
public function orderItems()
{
return $this->hasMany(OrderItem::class, 'product_id');
}
/**
* Relationship: Product has many orders through order items
*/
public function orders()
{
return $this->hasManyThrough(Order::class, OrderItem::class, 'product_id', 'id', 'id', 'order_id');
}
/**
* Legacy: Many-to-many categories (if still needed)
*/
@@ -86,4 +102,31 @@ class Product extends Model
{
return $this->belongsToMany(ProductCategories::class, 'product_product_category', 'product_id', 'product_category_id');
}
/**
* Get monthly pricing (or first available pricing)
*/
public function getMonthlyPricingAttribute()
{
return $this->pricings()
->where('is_active', 1)
->where('billing_cycle', 'monthly')
->first()
?? $this->pricings()->where('is_active', 1)->orderBy('price')->first();
}
/**
* Get display price (sale price if available, otherwise regular price)
*/
public function getPriceAttribute()
{
$pricing = $this->monthlyPricing;
return $pricing ? $pricing->price : 0;
}
public function getPriceSaleAttribute()
{
$pricing = $this->monthlyPricing;
return $pricing ? $pricing->price_sale : null;
}
}
+3 -1
View File
@@ -10,6 +10,8 @@ class ProductCategories extends Model
{
use HasFactory, SoftDeletes;
protected $table = 'product_categories';
protected $fillable = [
'name',
'slug',
@@ -30,6 +32,6 @@ class ProductCategories extends Model
public function products()
{
return $this->belongsToMany(Product::class, 'product_product_category', 'product_category_id', 'product_id');
return $this->hasMany(Product::class, 'product_categories_id');
}
}