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.
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.
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’sall method will retrieve a record with a specific ID.
Retrieving several models.
The model’sall method will retrieve the first 10 records from the model’s associated database table:
Building queries
To query by specific parameters, you can use thewhere method. This method sends query parameters over the API and returns the matching records.
Pagination
You can paginate and get subsquent pages of results by using thepaginate method. This method takes 2 parameters, page and per_page. The default pagination per_page limit is 100.
Refreshing Models
If you already have an instance of a model that was retrieved, you can “refresh” the model using thefresh method. The fresh method will re-retrieve the model from the database. The existing model instance will not be affected:
refresh method will re-hydrate the existing model using fresh data from the database:
Expanding Relations
Many models allow you to request additional information from relational models by using thewith 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.
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: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 thewith method. Fields that can be included in an expanded response are noted in this documentation with the “expandable” label.
Recursively expanding relations
You can expand recursively by specifying nested fields after a dot (.). For example, requestingcustomer and customer.billing_address on an order will expand the customer and the customer’s billing address.
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:
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.
Inserting, Updating, and Deleting
Creating a Model
To create a new model, instantiate it with attributes and call thesave method:
save:
Updating a Model
Use the staticupdate method directly by passing an id in the attributes:
update with the new attributes:
save:
Deleting a Model
Use thedelete method to remove a model:
Error Handling
All model methods can return a\WP_Error object if the API request fails. Always check for errors when working with models:
find, get, save, and delete:
Utility Methods
Getting the First Result
Use thefirst method to retrieve a single record from a query:
Converting to Array or Object
Models can be converted to arrays or standard objects:Array Access
Models implementArrayAccess, so you can access attributes using array syntax:
Dirty Checking
You can check if a model’s attributes have been modified since it was retrieved: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
Customers
Orders & Payments
Subscriptions
Shipping & Fulfillment
Tax
Licensing
Affiliates
Media
Account & Settings
Reviews
Other
All models are located in the
SureCart\Models namespace: