WC_API_Products::get_product_attribute( string $id, string|null $fields = null )
Get the product attribute for the given ID
Description Description
Parameters Parameters
- $id
-
(Required) product attribute term ID
- $fields
-
(Optional) fields to limit response to
Default value: null
Return Return
(array|WP_Error)
Source Source
File: includes/legacy/api/v2/class-wc-api-products.php
public function get_product_attribute( $id, $fields = null ) { global $wpdb; try { $id = absint( $id ); // Validate ID if ( empty( $id ) ) { throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_id', __( 'Invalid product attribute ID', 'woocommerce' ), 400 ); } // Permissions check if ( ! current_user_can( 'manage_product_terms' ) ) { throw new WC_API_Exception( 'woocommerce_api_user_cannot_read_product_categories', __( 'You do not have permission to read product attributes', 'woocommerce' ), 401 ); } $attribute = $wpdb->get_row( $wpdb->prepare( " SELECT * FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = %d ", $id ) ); if ( is_wp_error( $attribute ) || is_null( $attribute ) ) { throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_id', __( 'A product attribute with the provided ID could not be found', 'woocommerce' ), 404 ); } $product_attribute = array( 'id' => intval( $attribute->attribute_id ), 'name' => $attribute->attribute_label, 'slug' => wc_attribute_taxonomy_name( $attribute->attribute_name ), 'type' => $attribute->attribute_type, 'order_by' => $attribute->attribute_orderby, 'has_archives' => (bool) $attribute->attribute_public, ); return array( 'product_attribute' => apply_filters( 'woocommerce_api_product_attribute_response', $product_attribute, $id, $fields, $attribute, $this ) ); } catch ( WC_API_Exception $e ) { return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) ); } }
Changelog Changelog
Version | Description |
---|---|
2.4.0 | Introduced. |