x-wp/wc-data-attribute-tax

WooCommerce attribute taxonomy data objects

Maintainers

Package info

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

pkg:composer/x-wp/wc-data-attribute-tax

Fund package maintenance!

seebeen

Statistics

Installs: 3

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.1 2026-04-06 15:03 UTC

This package is auto-updated.

Last update: 2026-04-06 15:04:08 UTC


README

WC Data Attribute Tax

WooCommerce attribute taxonomy data objects

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

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 (mysql and mysqladmin)
  • curl
  • unzip

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 entity
  • XWC_Attribute_Tax_Factory — string-aware factory for resolving attributes by name or label
  • XWC_Attribute_Tax_Data_Store — data persistence layer using WooCommerce attribute functions

For framework-level documentation, see x-wp/wc-data-type.