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:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user