x-wp/wc-data-type

A library for working and standardizing working with WooCommerce data types.

Maintainers

Package info

github.com/x-wp/wc-data-type

Homepage

pkg:composer/x-wp/wc-data-type

Fund package maintenance!

seebeen

Statistics

Installs: 94

Dependents: 3

Suggesters: 0

Stars: 1

Open Issues: 1

v1.4.1 2026-04-04 20:54 UTC

README

WC Data Type

Model-driven custom data objects for WooCommerce

Packagist Version Packagist PHP Version Static Badge Static Badge GitHub Actions Workflow Status

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

  1. Model-driven setup: Define object shape with the #[Model] attribute.
  2. WooCommerce-style objects: Extend XWC_Data and keep a familiar CRUD workflow.
  3. Typed properties: Support for core props, meta props, and taxonomy props.
  4. Shared object helpers: Load single objects or collections through package utilities.
  5. Extensible architecture: Swap in custom data stores, factories, and meta stores when needed.
  6. 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\Model
  • XWC_Data
  • xwc_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.