x-wp / wc-data-type
A library for working and standardizing working with WooCommerce data types.
Fund package maintenance!
Requires
- php: >=8.0
- symfony/polyfill-php81: ^1.30
- symfony/polyfill-php83: ^1.32
- x-wp/di-implementation: ^1
- x-wp/helper-classes: ^1
- x-wp/helper-functions: ^1
Requires (Dev)
- oblak/wordpress-coding-standard: ^1
- php-stubs/wordpress-stubs: ^6.5
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpstan/phpstan-deprecation-rules: ^1.1
- phpunit/phpunit: ^9.6
- swissspidy/phpstan-no-private: ^0.2.0
- szepeviktor/phpstan-wordpress: ^1.3
- x-wp/di: ^1.0 || ^2.0
- yoast/phpunit-polyfills: ^4.0
Suggests
- automattic/jetpack-autoloader: Allow for better interoperability with other plugins that use this package.
This package is auto-updated.
Last update: 2026-04-04 20:54:30 UTC
README
WC Data Type
Model-driven custom data objects for WooCommerce
This library provides a standardized way to define and work with custom WooCommerce-style data objects. Define a model once, then reuse consistent property handling, factories, queries, and data-store behavior around it.
Key Features
- Model-driven setup: Define object shape with the
#[Model]attribute. - WooCommerce-style objects: Extend
XWC_Dataand keep a familiar CRUD workflow. - Typed properties: Support for core props, meta props, and taxonomy props.
- Shared object helpers: Load single objects or collections through package utilities.
- Extensible architecture: Swap in custom data stores, factories, and meta stores when needed.
- WordPress-native integration: Designed for plugin code already built around WordPress and WooCommerce lifecycles.
Installation
You can install this package via Composer:
composer require x-wp/wc-data-type
Tip
We recommend using automattic/jetpack-autoloader with this package to reduce autoloading conflicts in WordPress environments.
Usage
Below is a simple example that defines a custom data object and loads it through the package helpers.
Defining a model
<?php use XWC\Data\Decorators\Model; #[Model( name: 'book', table: '{{PREFIX}}books', core_props: array( 'title' => array( 'default' => '', 'type' => 'string', ), 'slug' => array( 'default' => '', 'type' => 'slug', ), 'price' => array( 'default' => 0, 'type' => 'float', ), ), )] final class Book extends XWC_Data { protected $object_type = 'book'; }
Loading objects
<?php $book = xwc_get_object(15, 'book', null); $books = xwc_get_objects( 'book', array( 'limit' => 10, 'orderby' => 'title', 'order' => 'ASC', ), );
Generated getters and setters follow the declared props, so classes like Book can expose methods such as get_title(), set_title(), get_slug(), and set_price().
Testing
The package ships with a PHPUnit suite that boots a WordPress test environment and exercises model definitions, object factories, queries, props, and runtime object behavior.
Run the suite with:
composer test
To prepare the local WordPress test environment first:
composer test:install
composer test
To clean the local test environment:
composer test:clean
Documentation
For package-specific usage, start with the public entrypoints used throughout the library:
XWC\Data\Decorators\ModelXWC_Dataxwc_get_object()xwc_get_objects()
Additional project information is available in the repository.
For maintainers preparing prereleases or stable tags, see docs/release-process.md.