x-wp / wc-data-attribute-tax
WooCommerce attribute taxonomy data objects
Fund package maintenance!
Requires
- php: >=8.1
- x-wp/wc-data-type: ^1.4
Requires (Dev)
- oblak/wordpress-coding-standard: ^1
- php-stubs/woocommerce-stubs: ^9.0
- 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
- symfony/var-dumper: ^6.4
- szepeviktor/phpstan-wordpress: ^1.3
- x-wp/di: ^1.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-06 15:04:08 UTC
README
WC Data Attribute Tax
WooCommerce attribute taxonomy data objects
This package provides XWC_Attribute_Tax data objects for working with WooCommerce product attribute taxonomies. Built on the x-wp/wc-data-type framework, it wraps WooCommerce's wc_*_attribute() functions in a model-driven CRUD workflow with typed properties, a dedicated factory, and convenience helpers.
Installation
composer require x-wp/wc-data-attribute-tax
Tip
We recommend using automattic/jetpack-autoloader with this package to reduce autoloading conflicts in WordPress environments.
Usage
Loading an attribute
<?php // By ID $attribute = xwc_get_attribute_tax( 5 ); // By name/slug $attribute = xwc_get_attribute_tax( 'color' ); // By label (creates if it doesn't exist) $attribute = XWC_Attribute_Tax::from_label( 'Color' );
Working with attribute properties
<?php $attribute = xwc_get_attribute_tax( 'color' ); $attribute->get_label(); // 'Color' $attribute->get_name(); // 'color' $attribute->get_type(); // 'select' $attribute->get_orderby(); // 'menu_order' $attribute->get_public(); // true $attribute->get_taxonomy_name(); // 'pa_color' $attribute->get_terms(); // array of WP_Term objects
Converting to WC_Product_Attribute
<?php $attribute = xwc_get_attribute_tax( 'color' ); $wc_attr = $attribute->get_wc_attribute( position: 0 );
Utility functions
<?php // Data store access $data_store = xwc_att_ds(); // Get attribute by ID (with default) $attribute = xwc_get_attribute_tax( $id, false ); // Get a fresh instance by ID $attribute = xwc_get_attribute_tax_object( $id ); // Find attribute ID by label $id = xwc_get_attribute_tax_id_by_label( 'Color' );
Testing
The package ships with a PHPUnit suite that boots a WordPress + WooCommerce test environment.
Local prerequisites:
- Docker with
docker compose - PHP
- WP-CLI (
wp) - MySQL client tools (
mysqlandmysqladmin) curlunzip
Run the suite with:
composer test
To prepare the local WordPress test environment first:
composer test:install
composer test
composer test:install starts the MySQL service, downloads WordPress, installs WooCommerce, and prepares the WordPress test suite under .cache/wp-tests.
To fully reset the local test environment:
composer test:clean
That command stops the test containers and removes the cached WordPress test files.
Documentation
Core classes:
XWC_Attribute_Tax— attribute taxonomy entityXWC_Attribute_Tax_Factory— string-aware factory for resolving attributes by name or labelXWC_Attribute_Tax_Data_Store— data persistence layer using WooCommerce attribute functions
For framework-level documentation, see x-wp/wc-data-type.