SlideShare a Scribd company logo
WooCommerce CRUD
and Data Store
Akeda Bagus
Akeda Bagus
• Code wrangler at
Automattic.
• Currently working on
WooCommerce
extensions and
marketplace.
CRUD and Data Store
in WooCommerce?
Why we need that?
Create, Read, Update, Delete
• WooCommerce has some resource types: orders, products, customers,
coupons, etc.
• With CRUD in resource, the developer doesn’t need to know the internal
complexities when interacting with WooCommerce resource types.
• Increase DRY. It can be used in WooCommerce REST API and CLIs.
• Once abstracted, the underlying data storage of resource can be
anything (CPT, custom table, external service, or dummy storage).
• Increase test coverage.
• For more detail see https://woocommerce.wordpress.com/2016/10/27/
the-new-crud-classes-in-woocommerce-2-7/.
Resource Interaction
without Data CRUD
$order_id = 123;
// Retrieving.
$billing_name = get_post_meta( $order_id,
‘_billing_first_name’, true ) . ‘ ‘ . get_post_meta( $order_id,
‘_billing_last_name’, true );
// Updating.
update_post_meta( $order_id, ‘_billing_first_name’, ‘Akeda’ );
update_post_meta( $order_id, ‘_billing_last_name’, ‘Bagus’ );
Resource Interaction
without Data CRUD
• Developer needs to know WooCommerce meta
keys and expected value’s type to store for each
resource type.
• Data storage is tied to WP tables.
• Unit test needs to be set up with the test DB unless
all WP dependencies are mocked.
Resource Interaction
with Data CRUD
$order_id = 123;
$order = wc_get_order( $order_id );
// Retrieving.
$billing_name = $order->get_billing_first_name() . ‘ ‘ .
$order->get_billing_last_name();
// Updating.
$order->set_billing_first_name( ‘Akeda’ );
$order->set_billing_last_name( ‘Bagus’ );
$order->save();
Resource Interaction
with Data CRUD
• Developer only needs to know the Data CRUD
methods.
• Data CRUD objects handle flow of data and any
validations. Less work for developers!
• Easier to test the resource.
Resource Interaction
with Data CRUD
// Setters and getters on the resource.
// Basically set_{property} and get_{property}.
$product = wc_get_product( 123 );
$product->set_description( ‘My cool product’ );
$product->set_sku( ‘WOO123’ );
$product->set_price( 15000 );
$product->get_description(); // My cool product
$product->get_sku(); // WOO123
$product->get_price(); // 15000
Resource Interaction
with Data CRUD
// Saving and deleting with save() and delete().
$product = wc_get_product( 123 );
$product->set_price( 25000 );
// Internally, this will pass the action to the data store to
// persist the change. If no ID, it implies creation otherwise
// it will update existing product.
$product->save();
// This will delete the product from the DB.
$product->delete();
Resource Interaction
with Data CRUD
• All properties on resource types can be found at WooCommerce
GitHub’s wiki.
• https://github.com/woocommerce/woocommerce/wiki/CRUD-Objects-
in-3.0
• Once you know available properties, the pattern is easy to remember:
• get_{property} to retrieve the property.
• set_{property} to set the property.
• Use save() and delete() to create/update and delete the
resource.
Resource Interaction
with Data CRUD
• All Data CRUD objects must extend abstract WC_Data.
• WC_Data handles the communication with the data store, so the
implementation only needs to extend the abstract and define some
properties that are available via setters and getters.
• Examples of WooCommerce Data CRUD classes:
• WC_Coupon
• WC_Order
• WC_Product
• WC_Customer
Data Store
• Bridge Data CRUDs and storage layer.
• If Data CRUDs need data, they ask Data Store.
Same thing when saving and deleting.
• Data Store can be powered by CPT, custom table,
remote API, or anything. It’s easy to swap the data
store of a Data CRUD class.
• WooCommerce built-in Data CRUDs are shipped
with CPT and custom table Data Store.
Data Store
• Data Store should be started by defining interface. The implementation can
be CPT, custom table, external API, or anything.
• Methods defined in the interface can be create(), read(),
read_meta(), delete_meta(), etc.
• Every Data Store for Data CRUD object should implement that interface. For
CPT, the create() could use wp_insert_post(), while for external API
the create() could use wp_remote_post().
• A method may accepts reference to $data, which has a type of WC_Data.
• For more detail see https://github.com/woocommerce/woocommerce/wiki/
Data-Stores.
Data Store
// Example of Data Store interface.
interface WC_Object_Data_Store_Interface {
public function create( &$data );
public function read( &$data );
public function update( &$data );
public function delete( &$data, $args = array() );
public function read_meta( &$data );
public function delete_meta( &$data, $meta );
public function add_meta( &$data, $meta );
public function update_meta( &$data, $meta );
}
Data Store
// Example create() method from Coupon CPT Data Store. With CPT
// as the underlying storage, create() uses wp_insert_post().
public function create( WC_Coupon &$coupon ) {
// ...
$coupon_id = wp_insert_post( [
‘post_type’ => ‘shop_coupon’,
‘post_title’ => $coupon->get_code(),
// ...
] );
if ( $coupon_id ) {
$coupon->set_id( $coupon_id );
// ...
}
}
Data Store
// Example delete_order_item() method from order item
// Data Store which uses custom table.
public function delete_order_item( $item_id ) {
global $wpdb;
$wpdb->query( /* ... */ );
$wpdb->query( /* ... */ );
}
Data Store
// Example create() with external API request.
public function create( WC_Data &$data ) {
// ...
$result = wp_remote_post( /* ... */ );
if ( ! empty( $result[‘id’] ) ) {
$data->set_id( $result[‘id’] );
}
// ...
}
Data CRUD and Data Store Interaction
Data CRUD and Data Store Interaction
Data CRUD and Data Store Interaction
Data CRUD and Data Store Interaction
Creating your own CRUD
in your extension (CPT Data Store)
• Let’s use Restaurant as an example of a custom resource.
• First, define some properties that a Restaurant needs. For example:
• Name
• Address
• Cuisine
• Opening hours
• Map the properties in the CRUD class.
• Start Restaurant data store by using CPT as the underlying storage.
Creating your own CRUD
in your extension (CPT Data Store)
namespace GedexWCJKTRestaurantData_Objects;
class Restaurant extends WC_Data {
// Name value pairs (name => default value).
protected $data = [
‘name’ => ‘’,
‘address’ => ‘’,
‘cuisine’ => ‘’,
‘opening_hours’ => ‘9am - 10pm’,
];
public function __construct( $restaurant = 0 ) { ... }
public function get_name( $context = ‘view’ ) { ... }
public function set_name( $name ) { ... }
}
Creating your own CRUD
in your extension (CPT Data Store)
namespace GedexWCJKTRestaurantData_Objects;
class Restaurant extends WC_Data {
public function __construct( $restaurant = 0 ) {
parent::__construct( $restaurant );
if ( is_numeric( $restaurant ) && $restaurant > 0 ) {
$this->set_id( $restaurant );
} elseif ( $restaurant instanceof self ) {
$this->set_id( $restaurant->get_id() );
} else if ( ! empty( $restaurant->ID ) ) {
$this->set_id( $restaurant->ID );
} else {
$this->set_object_read( true );
}
$this->data_store = WC_Data_Store::load( ‘restaurant’ );
if ( $this->get_id() > 0 ) {
$this->data_store->read( $this );
}
}
}
Creating your own CRUD
in your extension (CPT Data Store)
namespace GedexWCJKTRestaurantData_Objects;
class Restaurant extends WC_Data {
public function get_name( $context = ‘view’ ) {
return $this->get_prop( ‘name’, $context );
}
public function set_name( $name ) {
$this->set_prop( ‘name’, $name )
}
// Do the same for other properties.
// ...
}
Creating your own CRUD
in your extension (CPT Data Store)
namespace GedexWCJKTRestaurantData_Stores;
use GedexWCJKTRestaurantData_ObjectsRestaurant;
interface Restaurant_Interface {
public function create( Restaurant &$restaurant );
public function read( Restaurant &$restaurant );
public function update( Restaurant &$restaurant );
public function delete( Restaurant &$restaurant );
}
Creating your own CRUD
in your extension (CPT Data Store)
namespace GedexWCJKTRestaurantData_Stores;
use GedexWCJKTRestaurantData_ObjectsRestaurant;
class Restaurant_CPT extends WC_Data_Store_WP
implements Restaurant_Interface
{
public function create( Restaurant &$restaurant ) { }
public function read( Restaurant &$restaurant ) { }
public function update( Restaurant &$restaurant ) { }
public function delete( Restaurant &$restaurant ) { }
}
Creating your own CRUD
in your extension (CPT Data Store)
• Let’s see in action.
More Information for Data CRUD and Data
Store
• https://github.com/woocommerce/woocommerce/wiki/
CRUD-Objects-in-3.0
• https://github.com/woocommerce/woocommerce/wiki/
Data-Stores
• Order with custom table is scheduled in Q1 2018. See
the roadmap: https://trello.com/c/2QGrEbBW/113-an-
order-database-that-can-scale-feature-plugin.
• If you develop WooCommerce extension make sure it’s
ready for this.
Thank you!

More Related Content

PPTX
Magento Dependency Injection
PDF
Dig Deeper into WordPress - WD Meetup Cairo
PDF
WordPress as an application framework
PDF
Intro to advanced caching in WordPress
PPTX
HirshHorn theme: how I created it
PPTX
PPTX
Build your own entity with Drupal
PDF
Ajax nested form and ajax upload in rails
Magento Dependency Injection
Dig Deeper into WordPress - WD Meetup Cairo
WordPress as an application framework
Intro to advanced caching in WordPress
HirshHorn theme: how I created it
Build your own entity with Drupal
Ajax nested form and ajax upload in rails

What's hot (20)

PDF
50 Laravel Tricks in 50 Minutes
PDF
Your Entity, Your Code
PDF
Top Ten Reasons to Use EntityFieldQuery in Drupal
PDF
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
PDF
Drupal Step-by-Step: How We Built Our Training Site, Part 1
PDF
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
PPT
Propel sfugmd
PDF
Your code sucks, let's fix it - DPC UnCon
PDF
Hibernate
PDF
Virtual Madness @ Etsy
PDF
Building iPhone Web Apps using "classic" Domino
PDF
Meet Magento Sweden - Magento 2 Layout and Code Compilation for Performance
PDF
jQuery secrets
PDF
Rapid Prototyping with PEAR
ZIP
Drupal Development (Part 2)
PPTX
Windows Azure Storage & Sql Azure
PPTX
Optimizing Magento by Preloading Data
PDF
Drupal & javascript
PPTX
Layout discovery. Drupal Summer Barcelona 2017
PDF
Drupal is Stupid (But I Love It Anyway)
50 Laravel Tricks in 50 Minutes
Your Entity, Your Code
Top Ten Reasons to Use EntityFieldQuery in Drupal
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
Drupal Step-by-Step: How We Built Our Training Site, Part 1
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
Propel sfugmd
Your code sucks, let's fix it - DPC UnCon
Hibernate
Virtual Madness @ Etsy
Building iPhone Web Apps using "classic" Domino
Meet Magento Sweden - Magento 2 Layout and Code Compilation for Performance
jQuery secrets
Rapid Prototyping with PEAR
Drupal Development (Part 2)
Windows Azure Storage & Sql Azure
Optimizing Magento by Preloading Data
Drupal & javascript
Layout discovery. Drupal Summer Barcelona 2017
Drupal is Stupid (But I Love It Anyway)
Ad

Viewers also liked (10)

PDF
Performance Tuning and Security Hardening using Drop-In and Must-Use Plugins ...
PDF
Headless CMS featuring WordPress by Dreb Bits
PDF
Gutenberg for Modern Editing by Niels Lange
PDF
Independent Digital Worker ( Digital Nomad ) by Victorina Augusklamasia
PPTX
Lesson Learned: My Freelance Experience by Aris Setiawan
PPTX
Multisite Implementation Within Nonprofit Organization by Wigid Triyadi
PDF
Five Things to Worry Later for Stress-free Site Making by Hafiz Rahman
PDF
Optimizing Your Travel Blog by Farchan Noorrachman
PDF
How to build an online shop using WooCommerce for Indonesian Market by Agus m...
PDF
Experience to Share: Paragraph Improvisation by Indri handayani
Performance Tuning and Security Hardening using Drop-In and Must-Use Plugins ...
Headless CMS featuring WordPress by Dreb Bits
Gutenberg for Modern Editing by Niels Lange
Independent Digital Worker ( Digital Nomad ) by Victorina Augusklamasia
Lesson Learned: My Freelance Experience by Aris Setiawan
Multisite Implementation Within Nonprofit Organization by Wigid Triyadi
Five Things to Worry Later for Stress-free Site Making by Hafiz Rahman
Optimizing Your Travel Blog by Farchan Noorrachman
How to build an online shop using WooCommerce for Indonesian Market by Agus m...
Experience to Share: Paragraph Improvisation by Indri handayani
Ad

Similar to WooCommerce CRUD and Data Store by Akeda Bagus (20)

PPTX
Magento Indexes
PDF
Utilization of zend an ultimate alternate for intense data processing
KEY
Yii Introduction
PPT
Mysocial databasequeries
PPT
Mysocial databasequeries
PDF
Effective Android Data Binding
PDF
WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul...
PDF
購物車程式架構簡介
PDF
Doctrine For Beginners
PDF
Spca2014 hillier 3rd party_javascript_libraries
PDF
Angular.js Fundamentals
PDF
Becoming a better WordPress Developer
PDF
You're Doing it Wrong - WordCamp Orlando
PDF
Why is crud a bad idea - focus on real scenarios
PDF
Understanding backbonejs
PDF
Agile Data concept introduction
PDF
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
PDF
You're Doing it Wrong - WordCamp Atlanta
PDF
Introduction to Zend Framework web services
PPTX
Cart creation-101217222728-phpapp01
Magento Indexes
Utilization of zend an ultimate alternate for intense data processing
Yii Introduction
Mysocial databasequeries
Mysocial databasequeries
Effective Android Data Binding
WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul...
購物車程式架構簡介
Doctrine For Beginners
Spca2014 hillier 3rd party_javascript_libraries
Angular.js Fundamentals
Becoming a better WordPress Developer
You're Doing it Wrong - WordCamp Orlando
Why is crud a bad idea - focus on real scenarios
Understanding backbonejs
Agile Data concept introduction
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
You're Doing it Wrong - WordCamp Atlanta
Introduction to Zend Framework web services
Cart creation-101217222728-phpapp01

Recently uploaded (20)

PDF
Empathic Computing: Creating Shared Understanding
PDF
Encapsulation theory and applications.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPT
Teaching material agriculture food technology
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
A Presentation on Artificial Intelligence
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
cuic standard and advanced reporting.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
Empathic Computing: Creating Shared Understanding
Encapsulation theory and applications.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Building Integrated photovoltaic BIPV_UPV.pdf
The AUB Centre for AI in Media Proposal.docx
Machine learning based COVID-19 study performance prediction
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
20250228 LYD VKU AI Blended-Learning.pptx
Teaching material agriculture food technology
CIFDAQ's Market Insight: SEC Turns Pro Crypto
A Presentation on Artificial Intelligence
Reach Out and Touch Someone: Haptics and Empathic Computing
The Rise and Fall of 3GPP – Time for a Sabbatical?
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Review of recent advances in non-invasive hemoglobin estimation
cuic standard and advanced reporting.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Encapsulation_ Review paper, used for researhc scholars

WooCommerce CRUD and Data Store by Akeda Bagus

  • 1. WooCommerce CRUD and Data Store Akeda Bagus
  • 2. Akeda Bagus • Code wrangler at Automattic. • Currently working on WooCommerce extensions and marketplace.
  • 3. CRUD and Data Store in WooCommerce? Why we need that?
  • 4. Create, Read, Update, Delete • WooCommerce has some resource types: orders, products, customers, coupons, etc. • With CRUD in resource, the developer doesn’t need to know the internal complexities when interacting with WooCommerce resource types. • Increase DRY. It can be used in WooCommerce REST API and CLIs. • Once abstracted, the underlying data storage of resource can be anything (CPT, custom table, external service, or dummy storage). • Increase test coverage. • For more detail see https://woocommerce.wordpress.com/2016/10/27/ the-new-crud-classes-in-woocommerce-2-7/.
  • 5. Resource Interaction without Data CRUD $order_id = 123; // Retrieving. $billing_name = get_post_meta( $order_id, ‘_billing_first_name’, true ) . ‘ ‘ . get_post_meta( $order_id, ‘_billing_last_name’, true ); // Updating. update_post_meta( $order_id, ‘_billing_first_name’, ‘Akeda’ ); update_post_meta( $order_id, ‘_billing_last_name’, ‘Bagus’ );
  • 6. Resource Interaction without Data CRUD • Developer needs to know WooCommerce meta keys and expected value’s type to store for each resource type. • Data storage is tied to WP tables. • Unit test needs to be set up with the test DB unless all WP dependencies are mocked.
  • 7. Resource Interaction with Data CRUD $order_id = 123; $order = wc_get_order( $order_id ); // Retrieving. $billing_name = $order->get_billing_first_name() . ‘ ‘ . $order->get_billing_last_name(); // Updating. $order->set_billing_first_name( ‘Akeda’ ); $order->set_billing_last_name( ‘Bagus’ ); $order->save();
  • 8. Resource Interaction with Data CRUD • Developer only needs to know the Data CRUD methods. • Data CRUD objects handle flow of data and any validations. Less work for developers! • Easier to test the resource.
  • 9. Resource Interaction with Data CRUD // Setters and getters on the resource. // Basically set_{property} and get_{property}. $product = wc_get_product( 123 ); $product->set_description( ‘My cool product’ ); $product->set_sku( ‘WOO123’ ); $product->set_price( 15000 ); $product->get_description(); // My cool product $product->get_sku(); // WOO123 $product->get_price(); // 15000
  • 10. Resource Interaction with Data CRUD // Saving and deleting with save() and delete(). $product = wc_get_product( 123 ); $product->set_price( 25000 ); // Internally, this will pass the action to the data store to // persist the change. If no ID, it implies creation otherwise // it will update existing product. $product->save(); // This will delete the product from the DB. $product->delete();
  • 11. Resource Interaction with Data CRUD • All properties on resource types can be found at WooCommerce GitHub’s wiki. • https://github.com/woocommerce/woocommerce/wiki/CRUD-Objects- in-3.0 • Once you know available properties, the pattern is easy to remember: • get_{property} to retrieve the property. • set_{property} to set the property. • Use save() and delete() to create/update and delete the resource.
  • 12. Resource Interaction with Data CRUD • All Data CRUD objects must extend abstract WC_Data. • WC_Data handles the communication with the data store, so the implementation only needs to extend the abstract and define some properties that are available via setters and getters. • Examples of WooCommerce Data CRUD classes: • WC_Coupon • WC_Order • WC_Product • WC_Customer
  • 13. Data Store • Bridge Data CRUDs and storage layer. • If Data CRUDs need data, they ask Data Store. Same thing when saving and deleting. • Data Store can be powered by CPT, custom table, remote API, or anything. It’s easy to swap the data store of a Data CRUD class. • WooCommerce built-in Data CRUDs are shipped with CPT and custom table Data Store.
  • 14. Data Store • Data Store should be started by defining interface. The implementation can be CPT, custom table, external API, or anything. • Methods defined in the interface can be create(), read(), read_meta(), delete_meta(), etc. • Every Data Store for Data CRUD object should implement that interface. For CPT, the create() could use wp_insert_post(), while for external API the create() could use wp_remote_post(). • A method may accepts reference to $data, which has a type of WC_Data. • For more detail see https://github.com/woocommerce/woocommerce/wiki/ Data-Stores.
  • 15. Data Store // Example of Data Store interface. interface WC_Object_Data_Store_Interface { public function create( &$data ); public function read( &$data ); public function update( &$data ); public function delete( &$data, $args = array() ); public function read_meta( &$data ); public function delete_meta( &$data, $meta ); public function add_meta( &$data, $meta ); public function update_meta( &$data, $meta ); }
  • 16. Data Store // Example create() method from Coupon CPT Data Store. With CPT // as the underlying storage, create() uses wp_insert_post(). public function create( WC_Coupon &$coupon ) { // ... $coupon_id = wp_insert_post( [ ‘post_type’ => ‘shop_coupon’, ‘post_title’ => $coupon->get_code(), // ... ] ); if ( $coupon_id ) { $coupon->set_id( $coupon_id ); // ... } }
  • 17. Data Store // Example delete_order_item() method from order item // Data Store which uses custom table. public function delete_order_item( $item_id ) { global $wpdb; $wpdb->query( /* ... */ ); $wpdb->query( /* ... */ ); }
  • 18. Data Store // Example create() with external API request. public function create( WC_Data &$data ) { // ... $result = wp_remote_post( /* ... */ ); if ( ! empty( $result[‘id’] ) ) { $data->set_id( $result[‘id’] ); } // ... }
  • 19. Data CRUD and Data Store Interaction
  • 20. Data CRUD and Data Store Interaction
  • 21. Data CRUD and Data Store Interaction
  • 22. Data CRUD and Data Store Interaction
  • 23. Creating your own CRUD in your extension (CPT Data Store) • Let’s use Restaurant as an example of a custom resource. • First, define some properties that a Restaurant needs. For example: • Name • Address • Cuisine • Opening hours • Map the properties in the CRUD class. • Start Restaurant data store by using CPT as the underlying storage.
  • 24. Creating your own CRUD in your extension (CPT Data Store) namespace GedexWCJKTRestaurantData_Objects; class Restaurant extends WC_Data { // Name value pairs (name => default value). protected $data = [ ‘name’ => ‘’, ‘address’ => ‘’, ‘cuisine’ => ‘’, ‘opening_hours’ => ‘9am - 10pm’, ]; public function __construct( $restaurant = 0 ) { ... } public function get_name( $context = ‘view’ ) { ... } public function set_name( $name ) { ... } }
  • 25. Creating your own CRUD in your extension (CPT Data Store) namespace GedexWCJKTRestaurantData_Objects; class Restaurant extends WC_Data { public function __construct( $restaurant = 0 ) { parent::__construct( $restaurant ); if ( is_numeric( $restaurant ) && $restaurant > 0 ) { $this->set_id( $restaurant ); } elseif ( $restaurant instanceof self ) { $this->set_id( $restaurant->get_id() ); } else if ( ! empty( $restaurant->ID ) ) { $this->set_id( $restaurant->ID ); } else { $this->set_object_read( true ); } $this->data_store = WC_Data_Store::load( ‘restaurant’ ); if ( $this->get_id() > 0 ) { $this->data_store->read( $this ); } } }
  • 26. Creating your own CRUD in your extension (CPT Data Store) namespace GedexWCJKTRestaurantData_Objects; class Restaurant extends WC_Data { public function get_name( $context = ‘view’ ) { return $this->get_prop( ‘name’, $context ); } public function set_name( $name ) { $this->set_prop( ‘name’, $name ) } // Do the same for other properties. // ... }
  • 27. Creating your own CRUD in your extension (CPT Data Store) namespace GedexWCJKTRestaurantData_Stores; use GedexWCJKTRestaurantData_ObjectsRestaurant; interface Restaurant_Interface { public function create( Restaurant &$restaurant ); public function read( Restaurant &$restaurant ); public function update( Restaurant &$restaurant ); public function delete( Restaurant &$restaurant ); }
  • 28. Creating your own CRUD in your extension (CPT Data Store) namespace GedexWCJKTRestaurantData_Stores; use GedexWCJKTRestaurantData_ObjectsRestaurant; class Restaurant_CPT extends WC_Data_Store_WP implements Restaurant_Interface { public function create( Restaurant &$restaurant ) { } public function read( Restaurant &$restaurant ) { } public function update( Restaurant &$restaurant ) { } public function delete( Restaurant &$restaurant ) { } }
  • 29. Creating your own CRUD in your extension (CPT Data Store) • Let’s see in action.
  • 30. More Information for Data CRUD and Data Store • https://github.com/woocommerce/woocommerce/wiki/ CRUD-Objects-in-3.0 • https://github.com/woocommerce/woocommerce/wiki/ Data-Stores • Order with custom table is scheduled in Q1 2018. See the roadmap: https://trello.com/c/2QGrEbBW/113-an- order-database-that-can-scale-feature-plugin. • If you develop WooCommerce extension make sure it’s ready for this.