Skip to main content
PHP Models = API Interface — The PHP models provide a fluent, Laravel-like interface to the SureCart REST API. Each model corresponds directly to an API resource.For complete details on available properties, query parameters, and expandable relations, refer to the API Reference.
The PHP models provide a fluent syntax for querying the database through the API. In this document, we will demonstrate various methods for retrieving models.
Blocking HTTP requests — Each model query makes a synchronous HTTP request to the SureCart API. This means PHP execution will wait for the response before continuing.Use these methods in contexts where blocking is acceptable:
  • AJAX/REST handlers.
  • Admin dashboard pages and requests.
  • WP-CLI commands.
  • Cron jobs and background tasks.
Avoid using model queries directly in front-end page rendering, as they will slow down page load times and negatively impact user experience.

Retrieving

SureCart’s PHP models allow you to fetch data from the SureCart API using a familiar, Laravel-like syntax. You can retrieve single records by ID, query collections with filters, paginate results, and refresh stale data — all with clean, expressive methods.

Retrieving a model by id

The model’s all method will retrieve a record with a specific ID.
use SureCart\Models\Product;
$product = Product::find('8ba8d60f-5277-4e6b-807c-dee8166446d5');

Retrieving several models.

The model’s all method will retrieve the first 10 records from the model’s associated database table:
use SureCart\Models\Product;

$products = Product::get();
foreach ($products as $product) {
    echo $product->name;
}

Building queries

To query by specific parameters, you can use the where method. This method sends query parameters over the API and returns the matching records.
The query parameters available in the where() method match the API endpoint’s query parameters. See each model’s API Reference (e.g., Products API) for all available filters.
use SureCart\Models\Product;
$archived_products = Product::where([
	'archived' => false
])->get();

Pagination

You can paginate and get subsquent pages of results by using the paginate method. This method takes 2 parameters, page and per_page. The default pagination per_page limit is 100.
use SureCart\Models\Product;

$archived_products = Product::where([
	'archived' => false
])->paginate([
	'per_page' => 20,
	'page' => 2
]);
Result:
[
	'object' => 'product',
	'pagination' => [
		'count' => 100,
		'limit' => 20,
		'page' => 2
	],
	'data' => [
		// 20 products
		[
			'id' => '8ba8d60f-5277-4e6b-807c-dee8166446d5',
			...
		]
		...
	]
]

Refreshing Models

If you already have an instance of a model that was retrieved, you can “refresh” the model using the fresh method. The fresh method will re-retrieve the model from the database. The existing model instance will not be affected:
$product = Product::where([
	'ids' => [
		'8ba8d60f-5277-4e6b-807c-dee8166446d5',
		'07fd28e0-e735-4acf-86d8-4d7855055e0b'
	]
])->first();

$freshProduct = $product->fresh();
The refresh method will re-hydrate the existing model using fresh data from the database:
$product = Product::create([
	'name' => 'New Product',
]);
$product->name = 'Updated Product';
$product->refresh();
$flight->name; // "New Product"

Expanding Relations

Many models allow you to request additional information from relational models by using the with method. This parameter is available on all models and applies to the response of that model only. Model relations can be expanded in two ways.
Each model’s expandable fields are documented in the API Reference. Look for properties marked as “expandable” in the response schemas.

Expanding a single relation

In many cases, an object contains the ID of a related object in its response properties. For example, a price has an associated product ID. Those models can be queried together in one call. ID fields that can be expanded into models are noted in this documentation with the “expandable” label. Here’s an example:
$prices = \SureCart\Models\Price::with([
	'product'
])->paginate(['page'=> 1]);
$promo = \SureCart\Models\Promotion::with([
	'coupon'
])->find('8ba8d60f-5277-4e6b-807c-dee8166446d5');

Expanding list relations

In some cases, such as a list of all prices for a product, there are available fields that are not included in responses by default. You can request these fields as an expanded response by using the with method. Fields that can be included in an expanded response are noted in this documentation with the “expandable” label.
$products = \SureCart\Models\Product::with([
	'prices'
])->paginate(['page'=> 1]);

Recursively expanding relations

You can expand recursively by specifying nested fields after a dot (.). For example, requesting customer and customer.billing_address on an order will expand the customer and the customer’s billing address.
$orders = \SureCart\Models\Order::with([
    'customer',
	'customer.billing_address'
])->paginate(['page'=> 1]);
You can use the with method on any endpoint that returns expandable fields, including list, create, and update endpoints. Expanding list requests are plural, but expanding objects within a list is singular. For example, if you wanted to retrieve an order’s line items and each line item’s price, you would pass checkout, checkout.line_items and line_item.price as expand parameters. Example:
$orders = \SureCart\Models\Order::with( [
   'checkout',
   'checkout.line_items',
   'line_item.price',
   'price.product'
] )->paginate( [ 'page' => 1 ] );
Then you can access expanded relations:
foreach ( $orders['data'] as $order ) {
    $checkout = $order->checkout;
    $line_items = $checkout->line_items->data;
    
    foreach ( $line_items as $line_item ) {
        $price = $line_item->price;
        $product = $price->product;
        
        echo $product->name . ' - ' . $price->amount;
    }
}
You can expand multiple objects at once by identifying multiple items in the with array. Expansions have a maximum depth of two levels, and you can expand up to 10 objects. This is not only limited to get or paginate. You can use this on create or update requests as well. This will allow you to create or update a model and fetch its relations all in one request.
$price = \SureCart\Models\Price::create([
	'name' => 'New Price',
	'amount' => 5000, // $50.00
	'currency' => 'usd',
	'product' => '8ba8d60f-5277-4e6b-807c-dee8166446d5',
])->with([
	'product'
])->save();

$product_name = $price->product->nname;

Inserting, Updating, and Deleting

Creating a Model

To create a new model, instantiate it with attributes and call the save method:
For the full list of required and optional attributes when creating a model, see the corresponding “Create” endpoint in the API Reference.
use SureCart\Models\Product;

$product = Product::create([
    'name' => 'iPhone',
	'description' => 'iPhone is a smartphone'
]);
Or set individual attributes and call save:
use SureCart\Models\Product;

// create ths instance.
$product = new Product([
	'name' => 'iPhone',
	'description' => 'iPhone is a smartphone'
]);

// do some other logic with product.

// save the product.
$product->save();

Updating a Model

Use the static update method directly by passing an id in the attributes:
use SureCart\Models\Product;

Product::update([
    'id' => $product_id,
    'name' => 'iPhone Pro',
    'description' => 'The latest iPhone'
]);
Or retrieve the model first, then call update with the new attributes:
use SureCart\Models\Product;

$product = Product::find( $product_id );
$product->update([
    'name' => 'iPhone Pro',
    'description' => 'The latest iPhone'
]);
Or modify individual attributes and call save:
use SureCart\Models\Product;

$product = Product::find( $product_id );
$product->name = 'iPhone Pro';
$product->description = 'The latest iPhone';
$product->save();

Deleting a Model

Use the delete method to remove a model:
use SureCart\Models\Product;

$product = Product::find( $product_id );
$product->delete();
You can also delete directly by passing an ID:
use SureCart\Models\Product;

Product::delete( $product_id );

Error Handling

All model methods can return a \WP_Error object if the API request fails. Always check for errors when working with models:
use SureCart\Models\Product;

$product = Product::find('invalid-id');

if (is_wp_error($product)) {
	// Handle the error
	$error_message = $product->get_error_message();
	$error_code = $product->get_error_code();
	error_log("Failed to find product: $error_message");
	return;
}

// Safe to use $product here
echo $product->name;
This applies to all model operations including find, get, save, and delete:
$product = new Product(['name' => 'Test']);
$result = $product->save();

if (is_wp_error($result)) {
	// Handle validation or API errors
	foreach ($result->get_error_messages() as $message) {
		echo $message;
	}
}

Utility Methods

Getting the First Result

Use the first method to retrieve a single record from a query:
$product = Product::where(['archived' => false])->first();

Converting to Array or Object

Models can be converted to arrays or standard objects:
$product = Product::find($id);

// Convert to array
$array = $product->toArray();

// Convert to stdClass object
$object = $product->toObject();

Array Access

Models implement ArrayAccess, so you can access attributes using array syntax:
$product = Product::find($id);

// These are equivalent
echo $product->name;
echo $product['name'];

Dirty Checking

You can check if a model’s attributes have been modified since it was retrieved:
$product = Product::find($id);

$product->isDirty();        // false - no changes yet

$product->name = 'New Name';
$product->isDirty();        // true - has unsaved changes
$product->isDirty('name');  // true - specific attribute changed

$dirty = $product->getDirty(); // ['name' => 'New Name']

Available Models

SureCart provides PHP models for interacting with the following resources. Each model maps directly to an API resource — see the API Reference for full property details, query parameters, and expandable fields.

Products

ModelAPI ReferenceDescription
ProductProductsPhysical or digital products
PricePricesPricing options for products
ProductCollectionProduct CollectionsProduct collections/categories
ProductGroupProduct GroupsProduct groupings
ProductMediaProduct MediasMedia attached to products
VariantVariantsProduct variants
VariantOptionVariant OptionsVariant option definitions
BumpBumpsOrder bumps
UpsellUpsellsUpsell offers
UpsellFunnelUpsell FunnelsUpsell funnel configurations
SwapSwapsProduct swap configurations
DownloadDownloadsDownloadable files
CouponCouponsDiscount coupons
PromotionPromotionsApplied promotions

Customers

ModelAPI ReferenceDescription
CustomerCustomersCustomer records
BalanceTransactionBalance TransactionsBalance transaction records

Orders & Payments

ModelAPI ReferenceDescription
OrderOrdersCompleted orders
CheckoutCheckoutsCheckout sessions
AbandonedCheckoutAbandoned CheckoutsAbandoned checkout recovery
LineItemLine ItemsOrder line items
PurchasePurchasesIndividual purchase records
ChargeChargesPayment charges
InvoiceInvoicesInvoices for orders
RefundRefundsRefund records
PaymentIntentPayment IntentsPayment intent records
PaymentMethodPayment MethodsStored payment methods
ManualPaymentMethodManual Payment MethodsManual payment method configs
ProcessorProcessorsPayment processors
FeeFeesOrder fees
DisputeDisputesPayment disputes

Subscriptions

ModelAPI ReferenceDescription
SubscriptionSubscriptionsRecurring subscriptions
PeriodPeriodsSubscription billing periods
CancellationActCancellation ActsSubscription cancellations
CancellationReasonCancellation ReasonsCancellation reason options

Shipping & Fulfillment

ModelAPI ReferenceDescription
FulfillmentFulfillmentsOrder fulfillments
FulfillmentItemFulfillment ItemsFulfillment line items
ShippingMethodShipping MethodsShipping method configurations
ShippingProfileShipping ProfilesShipping profile settings
ShippingRateShipping RatesShipping rate definitions
ShippingZoneShipping ZonesShipping zone configurations
ReturnRequestReturn RequestsReturn request records
ReturnItemReturn ItemsReturn request items

Tax

ModelAPI ReferenceDescription
TaxRegistrationTax RegistrationsTax registration records
TaxZoneTax ZonesTax zone configurations

Licensing

ModelAPI ReferenceDescription
LicenseLicensesSoftware licenses
ActivationActivationsLicense activations

Affiliates

ModelAPI ReferenceDescription
AffiliationAffiliationsAffiliate accounts
AffiliationRequestAffiliation RequestsAffiliate application requests
AffiliationProductAffiliation ProductsAffiliate product associations
ReferralReferralsAffiliate referral records
ReferralItemReferral ItemsReferral line items
PayoutPayoutsAffiliate payouts
PayoutGroupPayout GroupsGrouped payout batches
ClickClicksAffiliate click tracking

Media

ModelAPI ReferenceDescription
MediaMediasMedia files

Account & Settings

ModelAPI ReferenceDescription
AccountAccountStore account settings
BrandBrandBranding configurations
WebhookWebhook EndpointsWebhook endpoint configurations

Reviews

ModelAPI ReferenceDescription
ReviewReviewsProduct reviews

Other

ModelAPI ReferenceDescription
EventEventsEvent log records
ExportExportsData exports
All models are located in the SureCart\Models namespace:
use SureCart\Models\Product;
use SureCart\Models\Customer;
use SureCart\Models\Order;
// etc.