WC_API_Products::edit_product_attribute_term() public WC 2.5.0
Edit a product attribute term.
{} It's a method of the class: WC_API_Products{}
Hooks from the method
Return
Array|WP_Error.
Usage
$WC_API_Products = new WC_API_Products(); $WC_API_Products->edit_product_attribute_term( $attribute_id, $id, $data );
- $attribute_id(int) (required)
- Attribute ID.
- $id(int) (required)
- the attribute ID.
- $data(array) (required)
- -
Changelog
Since 2.5.0 | Introduced. |
Code of WC_API_Products::edit_product_attribute_term() WC API Products::edit product attribute term WC 5.0.0
public function edit_product_attribute_term( $attribute_id, $id, $data ) {
global $wpdb;
try {
if ( ! isset( $data['product_attribute_term'] ) ) {
throw new WC_API_Exception( 'woocommerce_api_missing_product_attribute_term_data', sprintf( __( 'No %1$s data specified to edit %1$s', 'woocommerce' ), 'product_attribute_term' ), 400 );
}
$id = absint( $id );
$data = $data['product_attribute_term'];
// Check permissions.
if ( ! current_user_can( 'manage_product_terms' ) ) {
throw new WC_API_Exception( 'woocommerce_api_user_cannot_edit_product_attribute', __( 'You do not have permission to edit product attributes', 'woocommerce' ), 401 );
}
$taxonomy = wc_attribute_taxonomy_name_by_id( $attribute_id );
if ( ! $taxonomy ) {
throw new WC_API_Exception( 'woocommerce_api_invalid_product_attribute_id', __( 'A product attribute with the provided ID could not be found', 'woocommerce' ), 404 );
}
$data = apply_filters( 'woocommerce_api_edit_product_attribute_term_data', $data, $this );
$args = array();
// Update name.
if ( isset( $data['name'] ) ) {
$args['name'] = wc_clean( wp_unslash( $data['name'] ) );
}
// Update slug.
if ( isset( $data['slug'] ) ) {
$args['slug'] = sanitize_title( wp_unslash( $data['slug'] ) );
}
$term = wp_update_term( $id, $taxonomy, $args );
if ( is_wp_error( $term ) ) {
throw new WC_API_Exception( 'woocommerce_api_cannot_edit_product_attribute_term', $term->get_error_message(), 400 );
}
do_action( 'woocommerce_api_edit_product_attribute_term', $id, $data );
return $this->get_product_attribute_term( $attribute_id, $id );
} catch ( WC_API_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
}