Skip to content

Order Item Model

Individual items within an order with product snapshots, prescription handling, and pricing management.

Overview

The OrderItem model represents individual products within an order, maintaining product snapshots for historical accuracy and supporting prescription requirements. It handles pricing calculations, discounts, and prescription approval workflows.

Attributes

AttributeTypeDescription
idintPrimary key
order_idintForeign key to Order
product_idintForeign key to Product
product_namestringProduct name at time of order
product_skustringProduct SKU at time of order
product_descriptionstringProduct description at time of order
product_snapshotjsonComplete product data snapshot
quantityintQuantity ordered
unit_priceintUnit price in cents
total_priceintTotal price in cents (quantity × unit_price)
discount_amountintDiscount amount in cents
requires_prescriptionbooleanWhether item requires prescription
prescription_statusstringPrescription status (pending, approved, rejected)
prescription_notesstringPrescription-related notes
notesstringAdditional item notes
metadatajsonAdditional JSON data
created_attimestampCreation timestamp
updated_attimestampLast update timestamp

Relationships

Belongs To

  • belongsTo(Order) - Parent order
  • belongsTo(Product) - Associated product

Key Features

Product Snapshots

  • Maintains historical product data
  • Ensures order accuracy over time
  • Preserves pricing and product information

Prescription Management

  • Prescription requirement tracking
  • Approval workflow support
  • Status monitoring and notes

Pricing Management

  • Unit price and total price tracking
  • Discount handling
  • Automatic total calculation

Data Integrity

  • Product snapshot preservation
  • Historical accuracy maintenance
  • Audit trail support

Usage Examples

Creating an Order Item

php
$orderItem = OrderItem::create([
    'order_id' => $order->id,
    'product_id' => $product->id,
    'product_name' => $product->name,
    'product_sku' => $product->sku,
    'product_description' => $product->description,
    'product_snapshot' => $product->toArray(),
    'quantity' => 2,
    'unit_price' => 2500, // $25.00
    'total_price' => 5000, // $50.00
    'discount_amount' => 500, // $5.00 discount
    'requires_prescription' => true,
    'prescription_status' => 'pending',
    'prescription_notes' => 'Requires doctor approval',
    'notes' => 'Customer requested expedited shipping',
    'metadata' => [
        'prescription_id' => 'RX123456',
        'doctor_name' => 'Dr. Smith'
    ]
]);

Creating from Cart Item

php
// Convert cart item to order item
$orderItem = OrderItem::createFromCartItem($cartItem, $order);

Getting Associated Data

php
// Get parent order
$order = $orderItem->order;

// Get product information
$product = $orderItem->product;

// Get product snapshot data
$snapshot = $orderItem->product_snapshot;

Pricing Operations

php
// Get formatted prices
$unitPrice = $orderItem->unit_price_formatted; // "$25.00"
$totalPrice = $orderItem->total_price_formatted; // "$50.00"
$discount = $orderItem->discount_amount_formatted; // "$5.00"
$finalPrice = $orderItem->final_price_formatted; // "$45.00"

// Update total price
$orderItem->updateTotalPrice();

// Get final price after discount
$finalPrice = $orderItem->final_price; // 4500 (in cents)

Prescription Management

php
// Check prescription requirements
if ($orderItem->needs_prescription_approval) {
    // Handle prescription approval
}

// Check prescription status
if ($orderItem->is_prescription_approved) {
    // Process approved item
}

if ($orderItem->is_prescription_rejected) {
    // Handle rejected item
}

if ($orderItem->is_prescription_pending) {
    // Handle pending item
}

Scopes

Prescription Scopes

php
// Get items requiring prescription
OrderItem::requiresPrescription()->get();

// Get approved prescription items
OrderItem::prescriptionApproved()->get();

// Get pending prescription items
OrderItem::prescriptionPending()->get();

// Get rejected prescription items
OrderItem::prescriptionRejected()->get();

Accessors

Formatted Prices

php
$orderItem->unit_price_formatted; // "$25.00"
$orderItem->total_price_formatted; // "$50.00"
$orderItem->discount_amount_formatted; // "$5.00"
$orderItem->final_price_formatted; // "$45.00"

Prescription Status

php
$orderItem->needs_prescription_approval; // bool
$orderItem->is_prescription_approved; // bool
$orderItem->is_prescription_rejected; // bool
$orderItem->is_prescription_pending; // bool

Financial Calculations

php
$orderItem->final_price; // int (in cents)

Methods

updateTotalPrice()

Recalculates the total price based on quantity and unit price.

php
$orderItem->updateTotalPrice();

createFromCartItem()

Static method to create an order item from a cart item.

php
$orderItem = OrderItem::createFromCartItem($cartItem, $order);

Product Snapshots

The product_snapshot field contains a complete snapshot of the product data at the time of order creation:

php
$snapshot = [
    'id' => 1,
    'name' => 'Aspirin 500mg',
    'sku' => 'ASP-500',
    'description' => 'Pain relief medication',
    'price' => 2500,
    'category_id' => 5,
    // ... all product attributes
];

This ensures that even if the product is later modified or deleted, the order maintains accurate historical information.