Skip to content

Add attribute/attribute_term filtering to wc_get_products - #65

Open
aines-ldr wants to merge 1 commit into
royalplugins:mainfrom
aines-ldr:aines-ldr-patch-1
Open

Add attribute/attribute_term filtering to wc_get_products#65
aines-ldr wants to merge 1 commit into
royalplugins:mainfrom
aines-ldr:aines-ldr-patch-1

Conversation

@aines-ldr

Copy link
Copy Markdown

Adds optional attribute/attribute_term params to filter products by WooCommerce global attribute term, using tax_query

Summary

wc_get_products currently supports filtering by category, status, type, and search, but there's no way to filter by a global WooCommerce attribute (e.g. pa_color) and a specific term (e.g. black-color). This mirrors a filter that already exists in wp-admin (edit.php?taxonomy=pa_color&term=black-color&post_type=product) but isn't exposed through the MCP tool. For stores with many variable products, this makes it impossible for an AI agent to answer "which products use color X" without paginating through every product.

Changes

  • Added two new optional parameters to the wc_get_products tool schema: attribute (taxonomy slug, e.g. pa_color) and attribute_term (term slug or numeric term_id, e.g. black-color).
  • When both are present, builds a tax_query clause and passes it to wc_get_products() (WooCommerce's own query helper, which already supports tax_query via WP_Query).
  • Accepts either a term slug (recommended) or a numeric term_id — detected automatically via is_numeric().
  • Validates the taxonomy exists via taxonomy_exists() before querying, throwing a clear exception otherwise.
  • Fully backwards compatible: both params are optional, existing calls behave exactly as before.

Testing

  • MCP client(s) used: Claude Desktop (via Claude.ai custom connector)
  • WordPress version: 7.0
  • PHP version: 8.2
  • Tested against a live WooCommerce store with 80+ variable products and attribute terms with similar names (e.g. black vs black-color), confirming the term-slug lookup resolves to the exact intended term rather than a fuzzy match.
  • Verified existing calls to wc_get_products without the new params are unaffected.

Adds optional attribute/attribute_term params to filter products by WooCommerce global attribute term, using tax_query
@aines-ldr
aines-ldr requested a review from royalplugins as a code owner July 29, 2026 06:24
@royalplugins

Copy link
Copy Markdown
Owner

Thanks for the PR! Code looks good, tax_query approach + numeric-vs-slug detection + taxonomy_exists() guard are all correct.

Two small style fixes before merge:

  1. Tabs, not spaces — the new lines in execute_tool() (roughly lines 511–523 of the diff) use spaces; the rest of WooCommerce.php is tab-indented. Please re-indent to match.
  2. Trailing whitespace — the empty line right before $products = wc_get_products( $query_args ); has trailing spaces. Strip them.

Push a fixup commit to the same branch and we'll merge. Thanks again.

RP Team

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants