Skip to content

Order Model

DB Table Name{wp_db_prefix}_fct_orders
SchemaCheck Schema
Source Filefluent-cart/app/Models/Order.php
Name SpaceFluentCart\App\Models
ClassFluentCart\App\Models\Order

Attributes

AttributeData TypeComment
idIntegerPrimary Key
statusStringOrder status (draft, pending, processing, completed, etc.)
parent_idIntegerParent order ID (for renewals/child orders)
receipt_numberIntegerReceipt number
invoice_noStringInvoice number
fulfillment_typeStringFulfillment type (physical, digital)
typeStringOrder type (subscription, renewal, etc.)
modeStringOrder mode (live, test)
shipping_statusStringShipping status
customer_idIntegerCustomer ID (cast to integer)
payment_methodStringPayment method
payment_statusStringPayment status
payment_method_titleStringPayment method title
currencyStringCurrency code
subtotalIntegerSubtotal in cents (cast to double)
discount_taxIntegerDiscount tax in cents (cast to double)
manual_discount_totalIntegerManual discount total in cents (cast to double)
coupon_discount_totalIntegerCoupon discount total in cents (cast to double)
shipping_taxIntegerShipping tax in cents (cast to double)
shipping_totalIntegerShipping total in cents (cast to double)
tax_totalIntegerTax total in cents (cast to double)
total_amountIntegerTotal amount in cents (cast to double)
total_paidIntegerTotal paid in cents
total_refundIntegerTotal refund in cents
rateDecimalExchange rate
tax_behaviorIntegerTax behavior (0=no_tax, 1=exclusive, 2=inclusive)
noteTextOrder notes
ip_addressTextCustomer IP address
completed_atDate TimeCompletion timestamp
refunded_atDate TimeRefund timestamp
uuidStringUnique identifier
configJSONOrder configuration (auto-encoded/decoded via accessor/mutator)
created_atDate TimeCreation timestamp
updated_atDate TimeLast update timestamp

Usage

Please check Model Basic for Common methods.

Accessing Attributes

php
$order = FluentCart\App\Models\Order::find(1);

$order->id; // returns order ID
$order->status; // returns order status
$order->total_amount; // returns total amount in cents
$order->currency; // returns currency code
$order->customer_id; // returns customer ID

Methods

Along with Global Model methods, this model has few helper methods.

updateStatus($key, $newStatus)

Update order status

  • Parameters: $key (String) - Status key, $newStatus (String) - New status
  • Returns FluentCart\App\Models\Order - Updated order instance
php
$order = FluentCart\App\Models\Order::find(1);
$order->updateStatus('status', 'completed');

updatePaymentStatus($newStatus)

Update payment status

  • Parameters: $newStatus (String) - New payment status
  • Returns FluentCart\App\Models\Order - Updated order instance
php
$order = FluentCart\App\Models\Order::find(1);
$order->updatePaymentStatus('paid');

getMeta($metaKey, $defaultValue = false)

Get order meta value

  • Parameters: $metaKey (String) - Meta key, $defaultValue (Mixed) - Default value
  • Returns Mixed - Meta value or default
php
$order = FluentCart\App\Models\Order::find(1);
$metaValue = $order->getMeta('custom_field', 'default');

updateMeta($metaKey, $value)

Update order meta value

  • Parameters: $metaKey (String) - Meta key, $value (Mixed) - Meta value
  • Returns FluentCart\App\Models\OrderMeta - Meta instance
php
$order = FluentCart\App\Models\Order::find(1);
$meta = $order->updateMeta('custom_field', 'new_value');

deleteMeta($metaKey)

Delete order meta

  • Parameters: $metaKey (String) - Meta key
  • Returns Boolean - True if deleted
php
$order = FluentCart\App\Models\Order::find(1);
$deleted = $order->deleteMeta('custom_field');

getTotalPaidAmount()

Get total paid amount from succeeded transactions

  • Returns Integer - Total paid amount in cents
php
$order = FluentCart\App\Models\Order::find(1);
$totalPaid = $order->getTotalPaidAmount();

getTotalRefundAmount()

Get total refund amount from refunded transactions

  • Returns Integer - Total refund amount in cents
php
$order = FluentCart\App\Models\Order::find(1);
$totalRefund = $order->getTotalRefundAmount();

recountTotalPaidAndRefund()

Recount total paid and refund amounts. Updates total_refund and sets payment_status to refunded or partially refunded as appropriate.

  • Returns FluentCart\App\Models\Order - Updated order instance
php
$order = FluentCart\App\Models\Order::find(1);
$order->recountTotalPaidAndRefund();

syncOrderAfterRefund($type, $refundedAmount)

Sync order after refund

  • Parameters: $type (String) - Refund type ('full' or 'partial'), $refundedAmount (Integer) - Refund amount
  • Returns FluentCart\App\Models\Order - Updated order instance
php
$order = FluentCart\App\Models\Order::find(1);
$order->syncOrderAfterRefund('full', 1000);

updateRefundedItems($refundedItemIds, $refundedAmount)

Update refunded items. Distributes refund amount proportionally across the specified order items.

  • Parameters: $refundedItemIds (Array) - Order item IDs, $refundedAmount (Integer) - Refund amount
php
$order = FluentCart\App\Models\Order::find(1);
$order->updateRefundedItems([1, 2, 3], 1000);

recountTotalPaid()

Recount total paid amount (paid minus refunded)

  • Returns FluentCart\App\Models\Order - Updated order instance
php
$order = FluentCart\App\Models\Order::find(1);
$order->recountTotalPaid();

getLatestTransactionAttribute()

Get latest non-refund transaction (accessor, accessed as latest_transaction attribute)

  • Returns FluentCart\App\Models\OrderTransaction|null - Latest transaction
php
$order = FluentCart\App\Models\Order::find(1);
$transaction = $order->latest_transaction;

isSubscription()

Check if order is subscription

  • Returns Boolean - True if order has subscription items
php
$order = FluentCart\App\Models\Order::find(1);
$isSubscription = $order->isSubscription();

getViewUrl($type = 'customer')

Get order view URL

  • Parameters: $type (String) - View type ('customer' or 'admin')
  • Returns String - View URL
php
$order = FluentCart\App\Models\Order::find(1);
$viewUrl = $order->getViewUrl('admin');

getLatestTransaction()

Get latest non-refund transaction (method call, unlike the accessor attribute)

  • Returns FluentCart\App\Models\OrderTransaction|null - Latest transaction
php
$order = FluentCart\App\Models\Order::find(1);
$transaction = $order->getLatestTransaction();

currentSubscription()

Get current active subscription for this order

  • Returns FluentCart\App\Models\Subscription|null - Current active subscription
php
$order = FluentCart\App\Models\Order::find(1);
$subscription = $order->currentSubscription();

getDownloads($scope = 'email')

Get order downloads. Returns downloadable files associated with the order items, filtered by product variation authorization.

  • Parameters: $scope (String) - Download scope
  • Returns Array - Download data
php
$order = FluentCart\App\Models\Order::find(1);
$downloads = $order->getDownloads('email');

getLicenses($with = ['product', 'productVariant'])

Get order licenses (requires Pro and active license module)

  • Parameters: $with (Array) - Relationships to eager load (default: ['product', 'productVariant'])
  • Returns Illuminate\Database\Eloquent\Collection|null - Licenses collection or null if module inactive
php
$order = FluentCart\App\Models\Order::find(1);
$licenses = $order->getLicenses(['product', 'productVariant']);

getDownloadsById($orderId)

Get downloads for a specific order by ID

  • Parameters: $orderId (Integer) - Order ID
  • Returns Array - Download data
php
$order = FluentCart\App\Models\Order::find(1);
$downloads = $order->getDownloadsById(42);

getReceiptUrl()

Get receipt URL

  • Returns String - Receipt URL
php
$order = FluentCart\App\Models\Order::find(1);
$receiptUrl = $order->getReceiptUrl();

addLog($title, $description = '', $type = 'info', $by = '')

Add order log

  • Parameters: $title (String) - Log title, $description (String) - Description, $type (String) - Log type, $by (String) - Created by
php
$order = FluentCart\App\Models\Order::find(1);
$order->addLog('Status Updated', 'Order status changed to completed', 'info', 'admin');

canBeRefunded()

Check if order can be refunded. Returns false if the order has been upgraded to another order.

  • Returns Boolean - True if order can be refunded
php
$order = FluentCart\App\Models\Order::find(1);
$canBeRefunded = $order->canBeRefunded();

generateReceiptNumber()

Generate receipt number and invoice number if not already set

  • Returns FluentCart\App\Models\Order - Updated order instance
php
$order = FluentCart\App\Models\Order::find(1);
$order->generateReceiptNumber();

canBeDeleted()

Check if order can be deleted. Validates order status, payment status, mode, and subscription state. Test mode orders can always be deleted. Live orders must be canceled or on-hold, and must not have an active subscription.

  • Returns Boolean|WP_Error - True if order can be deleted, or WP_Error with reason
php
$order = FluentCart\App\Models\Order::find(1);
$canBeDeleted = $order->canBeDeleted();
if (is_wp_error($canBeDeleted)) {
    echo $canBeDeleted->get_error_message();
}

Relations

This model has the following relationships that you can use

parentOrder

Access the parent order.

  • Returns FluentCart\App\Models\Order
php
$order = FluentCart\App\Models\Order::find(1);
$parentOrder = $order->parentOrder;

children

Access the child orders.

  • Returns Illuminate\Database\Eloquent\Collection of FluentCart\App\Models\Order
php
$order = FluentCart\App\Models\Order::find(1);
$children = $order->children;

transactions

Access the order transactions.

  • Returns Illuminate\Database\Eloquent\Collection of FluentCart\App\Models\OrderTransaction
php
$order = FluentCart\App\Models\Order::find(1);
$transactions = $order->transactions;

subscriptions

Access the order subscriptions.

  • Returns Illuminate\Database\Eloquent\Collection of FluentCart\App\Models\Subscription
php
$order = FluentCart\App\Models\Order::find(1);
$subscriptions = $order->subscriptions;

order_items

Access the order items.

  • Returns Illuminate\Database\Eloquent\Collection of FluentCart\App\Models\OrderItem
php
$order = FluentCart\App\Models\Order::find(1);
$items = $order->order_items;

filteredOrderItems

Access the filtered order items based on priority rules for payment_type (onetime > subscription > adjustment).

  • Returns Illuminate\Database\Eloquent\Collection of FluentCart\App\Models\OrderItem
php
$order = FluentCart\App\Models\Order::find(1);
$filteredItems = $order->filteredOrderItems;

customer

Access the customer.

  • Returns FluentCart\App\Models\Customer
php
$order = FluentCart\App\Models\Order::find(1);
$customer = $order->customer;

orderMeta

Access the order metadata.

  • Returns Illuminate\Database\Eloquent\Collection of FluentCart\App\Models\OrderMeta
php
$order = FluentCart\App\Models\Order::find(1);
$meta = $order->orderMeta;

orderTaxRates

Access the order tax rates.

  • Returns Illuminate\Database\Eloquent\Collection of FluentCart\App\Models\OrderTaxRate
php
$order = FluentCart\App\Models\Order::find(1);
$taxRates = $order->orderTaxRates;

appliedCoupons

Access the applied coupons.

  • Returns Illuminate\Database\Eloquent\Collection of FluentCart\App\Models\AppliedCoupon
php
$order = FluentCart\App\Models\Order::find(1);
$appliedCoupons = $order->appliedCoupons;

usedCoupons

Access the used coupons (through the applied coupons intermediate table).

  • Returns Illuminate\Database\Eloquent\Collection of FluentCart\App\Models\Coupon
php
$order = FluentCart\App\Models\Order::find(1);
$usedCoupons = $order->usedCoupons;

shipping_address

Access the shipping address.

  • Returns FluentCart\App\Models\OrderAddress
php
$order = FluentCart\App\Models\Order::find(1);
$address = $order->shipping_address;

billing_address

Access the billing address.

  • Returns FluentCart\App\Models\OrderAddress
php
$order = FluentCart\App\Models\Order::find(1);
$address = $order->billing_address;

order_addresses

Access the order addresses.

  • Returns Illuminate\Database\Eloquent\Collection of FluentCart\App\Models\OrderAddress
php
$order = FluentCart\App\Models\Order::find(1);
$addresses = $order->order_addresses;

licenses

Access the order licenses.

  • Returns Illuminate\Database\Eloquent\Collection of FluentCartPro\App\Modules\Licensing\Models\License
php
$order = FluentCart\App\Models\Order::find(1);
$licenses = $order->licenses;

labels

Access the order labels (morph many relationship).

  • Returns Illuminate\Database\Eloquent\Collection of FluentCart\App\Models\LabelRelationship
php
$order = FluentCart\App\Models\Order::find(1);
$labels = $order->labels;

renewals

Access the order renewals (child orders of type 'renewal', excluding canceled, failed, and on-hold).

  • Returns Illuminate\Database\Eloquent\Collection of FluentCart\App\Models\Order
php
$order = FluentCart\App\Models\Order::find(1);
$renewals = $order->renewals;

orderOperation

Access the order operation record.

  • Returns FluentCart\App\Models\OrderOperation
php
$order = FluentCart\App\Models\Order::find(1);
$operation = $order->orderOperation;

Scopes

This model has the following scopes that you can use

Search orders by query. Searches across order ID, status, total amount, payment status, payment method, invoice number, order item titles, and customer name/email.

  • Parameters: $search (String) - Search query
php
$orders = FluentCart\App\Models\Order::searchBy('john')->get();

ofPaymentStatus($status)

Get orders by payment status

  • Parameters: $status (String) - Payment status
php
$orders = FluentCart\App\Models\Order::ofPaymentStatus('paid')->get();

ofOrderStatus($status)

Get orders by order status

  • Parameters: $status (String) - Order status
php
$orders = FluentCart\App\Models\Order::ofOrderStatus('completed')->get();

ofShippingStatus($status)

Get orders by shipping status

  • Parameters: $status (String) - Shipping status
php
$orders = FluentCart\App\Models\Order::ofShippingStatus('shipped')->get();

ofOrderType($type)

Get orders by order type

  • Parameters: $type (String) - Order type
php
$orders = FluentCart\App\Models\Order::ofOrderType('payment')->get();

ofPaymentMethod($methodName)

Get orders by payment method

  • Parameters: $methodName (String) - Payment method name
php
$orders = FluentCart\App\Models\Order::ofPaymentMethod('stripe')->get();

applyCustomFilters($filters)

Apply custom filters

  • Parameters: $filters (Array) - Filter array
php
$orders = FluentCart\App\Models\Order::applyCustomFilters([
    'status' => ['value' => ['completed', 'processing']]
])->get();

Usage Examples

Creating an Order

php
use FluentCart\App\Models\Order;

$order = Order::create([
    'customer_id' => 1,
    'status' => 'pending',
    'payment_method' => 'stripe',
    'currency' => 'USD',
    'total_amount' => 9999 // $99.99 in cents
]);

Retrieving Orders

php
// Get orders by payment status
$orders = Order::ofPaymentStatus('paid')->get();

// Get order by ID
$order = Order::find(1);

// Get order with items and customer
$order = Order::with(['order_items', 'customer'])->find(1);

Updating an Order

php
$order = Order::find(1);
$order->status = 'completed';
$order->completed_at = now();
$order->save();

Deleting an Order

php
$order = Order::find(1);
$order->delete();

FluentCart developer documentation