Expand CRUD data storage in Woocommerce

Have you heard of CRUD? In programming

, it represents creation, read , update , and to delete . Use Woocommerce, including products, orders, customers, coupons and other data stored in your database.

Woocommerce recent changes related to CRUD means that the platform is developing in a more scalability direction. Note: This article is indeed technical and assumes that you have a certain understanding of the PHP code.

Audio learner? The main point of this article comes from my speech in WoOoconf 2017.

]

The more scalability Woocommerce

In order to make our developers easier, we have added a special CRUD object to WooCommerce 3.0. This replaces all previous posts in WooCommerce, and all extensions must also be used to make the WooCommerce site expand.

At the same time, we added another set of objects named Data Stores, aiming Woocommerce data is loaded in the database and saved from the database. These abstract aims to make WooCommerce process a large number of orders, products and customers. This is achieved by connecting to Woocommerce to store data in different ways.

Each data storage realization determines the data storage and retrieval method of data, whether it is from the database or other ways to store data. By using them, Woocommerce can now be extended to corporate sales, so that millions of orders can be processed. What must be done is to add a filter that redefines woocommerce to store data. In additionThese structures also provide you with greater flexibility in the way to save and load data for the Woocommerce store. For each type of WOOCOMMERCE data (product, order, customer, etc.), you can now determine where the data should come from and where it should go by programming. This allows store builders to customize Woocommerce through interaction with other systems to meet their needs, or upgrade the performance of different parts of Woocommerce as needed.

In addition You provide you with greater flexibility in the way to save and load data for the Woocommerce store.

What does this mean? Well, consider the possibility: Save the order data in different databases from WPDB.

Obtain product data from the existing database on another system.

Sharing product inventory across multiple sites (examples are as follows).

Obtain product data from other sources through API.

Use the CLI command to load and save from one data storage to another (built -in import/export).

    Extended data storage

  • With the help of CRUD and data storage abstraction, Woocommerce no longer cares about where the data comes from or how to save. You can control it yourself.

  • In the following example, I retrieved inventory count from a separate service and only changed the inventory count of each product object. One of the benefits of doing this is that I can make multiple Woocommerce websites sell from the same amount of inventory and ensure that I do not sell my inventory.

This is the entrance point

Install_data_store

filter.

] This file contains bidirecti]

Public function __construct () { add_filter (\u0026#39; woocommerce_data_stores \u0026#39;, array ($ this, \u0026#39;Install_data_store \u0026#39;));

} Public function install_data_store ($ Stores) { Include_once DiRName (__file__). \u0026#39;/class-PRODUCT-Inventory-Data-Store.php \u0026#39;;

$ Instance \u003d New WC_PRODUCT_INVENTORY_DATA_STORE ($ Stores [\u0026#39; Product \u0026#39;]);

Return $ Stores;

}

}

View raw class-woocommerce-part-inventory -datastore-plugin.php hostted with \u0026#10084; by github

wc_object_data_interface [123 123 123 ] And

WC_PRODUCT_DATA_STORE_INTERFACE

.

This data storage aims to expand existing data storage (usually the default POST yuan implementation). It uses existing data storage in its constructor and only modifies the method of processing inventory. Everything else is stored normally through existing data storage.

] This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

Show Hidden Characters

Class WC_PRODUCT_DATA_ST) {

$ Stores [\u0026#39; produt \u0026#39;]\u003d $ Instance;
New WC_PRODUCT_INVENTORY_DATA_STORE_PLUGIN ();
For actual data storage, two interfaces need to be achieved:
]

$ This-

\u0026 gt;

Parent_instance \u003d $ This-

\u0026 gt;

Create_parent_instance ($ Parent_data_Store);

View raw class-Product-INVEntory-Data-Store.php Hosted with \u0026#10084; by Github

For most necessary functions, this data storage only calls parent data storage: [[[[[[[[[[[

This file contains bidirectional unicode text that may be interpreted or compiled diferently.File in an Editor That Reveals Hidden Unicode Characters. Learn More About Bidirectional Unicode Character show hidden characters123] public function update(\u0026 amp; $ spec) {

$ this-

paint_instance-

update($ Product);

How to read this data storage introduced is how to read the introduction of this data storage. Take inventory. For this situation, it usually reads from parent data storage, but the amount of inventory will be covered before returning:

\u0026 gt; \u0026 gt;
}
] View raw class-propuct-inventory-data-store.php hosth \u0026#10084; by github
]

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

Show hidden characters

3]

Public functions (\u0026 amp; $ Product) {

] $ This- \u0026 gt; Parent_instance- \u0026 gt; Read ($ Product); [

[

[

[
$ inventory_url \u003d \u0026#39; http: // localhost: 8080/api/inventory/\u0026#39;.(); $ report \u003d wp_remote_get ($ inventory_url);
$ response\u003d wp_remote_retrieve_body ($ request);

$ data \u003d json_decode ($ response);

123] $ Product-

\u0026 gt;

set_stock_quantity ($ data);

}

View raw class-partum Hosted with \u0026#10084; by github

With the help of CRUD and data storage abstraction, Woocommerce no longer cares about where the data comes from or how to save. You can control it yourself.

The amount of inventory above was retrieved from the API. In real -world applications, this API call may be transferred to the inventory control system, POS, and even another WooCommerce site.

The full code of the above example is located at: https://github.com/coderkevin/woocommerce-partuct-inventory-datastore

The infinite possibility of CRUD objects and data storage possibilities As shown in the above example, the possibility is endless. WooCommerce 3.3 will be released on January 30, including another exciting milestone related to CRUD. More information: WOOCOMMERCE 3.3 New Webhooks CRUD. Give full play to your imagination, use CRUD objects and data to store data solutions in a novel and interesting way, and ensure that in the comments or join the Woocommerce Slack community let us know your performance.

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *