E-commerce websites often lose out on sales due to performance or stability problems. These problems are not present initially when the sites have comparatively lesser data compared to the data a successful site has after maybe 6 or 7 months. As is the case with WooCommerce where higher Order/Product volume can really slow down your site performance.
Understanding WooCommerce CRUD Operators
WooCommerce has come up with the new CRUD operators to overcome the issue of performance and stability. These operators also improve the manner in which data is retrieved not only by WooCommerce for core operations but also help plugin authors in achieving the same.
What is CRUD?
CRUD is not that scary as the image suggests 🙂 but CRUD is an abbreviation for Create, Read, Update and Delete. These are the basic operations that can be performed on some sort of stored data.
Apart from site performance what are the benefits that will be achieved by using CRUD Operators?
Some of the benefits of using WooCommerce CRUD Operators are as follows:
- Developers will be able to access structured data by using CRUD Operators.
- Validation and data flow can be controlled.
- The size of the code drastically reduces when compared to traditional way of accessing data. This helps in maintaining the code in a long run.
- Everything gets unified.
- The data can be moved elsewhere, for example custom tables, without affecting existing code.
WooCommerce CRUD Objects
CRUD Objects were introduced in WooCommerce since version 3.0.0.
The following WooCommerce objects are handled by CRUD Objects and data-stores:
- Order Line Items
- Customer Downloads
- Payment Tokens
- Shipping Zones
The Power of WC_Data
WC_Data class is an abstract class used for creating protected properties for an object to which the class is being extended. The purpose of creating an abstract class is to have certain properties as base properties and few methods as base methods which can be used across all the objects.
For example Products, Orders and Coupons all of them have some common properties and all of them have some common methods. Similarly if we are creating any new object by extending
WC_Data then we may have access to these base properties and methods which can be extended as per requirements.
WooCommerce documentation states that
WC_Dataclass is the basic implementation for CRUD objects – they should extend this.
One of the important property of
WC_Data is the
$data property which is present across all the objects. The
$data property contains an array of properties supported in each object. Another property of importance is
$id which is the ID of the object.
Let us look at an example on how
$data property might look. The below structure is an example of
WC_Coupons object used for managing Coupons.
Increase WooCommerce Store Sales
"Customer service is great, they go above and beyond to assist and corrected any issues I had with setting everything up in a timely manner. I use the abandoned cart plugin and am very satisfied with the results and carts recovered." - Bran Weintraub
Setters and Getters
The above structure gave an overview of how the
$data property may look like. The next question arises is how to get and set these properties since
$data is a private property ?
WC_Data class that we have extended has two methods –
set_prop(). Let us discuss how to set and get property
used_by (property for maintaining the users who have used Coupons) shown in the above
used_by property we will create a new setter method called
set_used_by() and to get data we will create method
get_used_by as shown below.
Similarly, we can get and set other properties as well using
An important point about
set_prop() function is that it applies various filters based on the context and handle changes. The
$context variable used in the above snippet has two possible values –
view context should be used for getting data for use on the frontend or display whereas edit context should be used when showing values to edit in the backend, and for saving in the database.
CRUD Operators do not read data from databases. The actual read from databases is done by the data stores that have been extended.
WC_Data_Store has method called as
load which will do the actual read operation from the database. Parameter passed to the
load method is the object name for which the data needs to be retrieved.
Constructors will be the best place to load the data from database and store it in the local
data_store private variable.
Let us consider an example of a
WC_Coupons read operation performed when the class is being created.
id already exists, then it indicates the object has already being read from database and
data_store already has the updated object.
$this->data_store->read( $this ) will read the latest object that was read earlier.
The setter methods as explained above set a particular property in
$data object of
WC_Data. But this does not actually save or update in the database.
Let us relate the save/update operation with the
WC_Coupons object that we have used while demonstrating getters and setters. Let us consider that user wants to update Coupons Usage Count. The count will first be updated using a setter function
set_usage_count which will be part of
WC_Data class will save the changes in the database using the
save function as demonstrated below.
Save action must be performed for changes to persist otherwise the changes will be discarded.
The above snippet demonstrates on how to save the data-store that has been modified. The data-store can be modified using custom action hooks dedicated to the current object before the actual save takes place.
The data-store will either update data in the database if an ID already exists or it will create a new ID and save it in the database as a new object.
Delete operation has a provision to either trash the object or permanently delete the object.
$force_delete parameter passed to the function determines whether to permanently delete the object from the database or temporarily trash the object. After deleting the object the
ID of the object is set to 0.
CRUD Operations as mentioned above can be very handy and useful in creating new objects such as Subscriptions or Memberships for WooCommerce. As a result the management of data right from Creating to Updating to Deleting can be handled with ease and much more effectiveness than conventional method of handling data.