Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions includes/Integrations/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public static function get_tools() {
'category' => [ 'type' => 'string', 'description' => 'Category slug to filter by' ],
'search' => [ 'type' => 'string', 'description' => 'Search term' ],
'type' => [ 'type' => 'string', 'description' => 'Product type (simple, variable, grouped, external)' ],
'attribute' => [ 'type' => 'string', 'description' => 'Global attribute taxonomy slug to filter by, e.g. pa_color (returned by wc_get_product_attributes)' ],
'attribute_term' => [ 'type' => 'string', 'description' => 'Term slug or term_id within the attribute to filter by, e.g. black-color (returned by wc_get_attribute_terms). Requires attribute to also be set.' ],
],
],
],
Expand Down Expand Up @@ -505,6 +507,22 @@ public static function execute_tool( $name, $args ) {
if ( ! empty( $args['type'] ) ) {
$query_args['type'] = sanitize_text_field( $args['type'] );
}

if ( ! empty( $args['attribute'] ) && ! empty( $args['attribute_term'] ) ) {
$taxonomy = sanitize_text_field( $args['attribute'] );
$term_value = sanitize_text_field( $args['attribute_term'] );
if ( ! taxonomy_exists( $taxonomy ) ) {
throw new \Exception( 'Unknown attribute taxonomy: ' . $taxonomy );
}
$query_args['tax_query'] = [
[
'taxonomy' => $taxonomy,
'field' => is_numeric( $term_value ) ? 'term_id' : 'slug',
'terms' => is_numeric( $term_value ) ? intval( $term_value ) : $term_value,
],
];
}

$products = wc_get_products( $query_args );
return array_map( [ __CLASS__, 'format_product_summary' ], $products );

Expand Down