WC_API_Products::edit_product_attribute_term( int $attribute_id, int $id, array $data )

Edit a product attribute term.


Description Description


Parameters Parameters

$attribute_id

(Required) Attribute ID.

$id

(Required) the attribute ID.

$data

(Required)


Top ↑

Return Return

(array|WP_Error)


Top ↑

Source Source

File: includes/legacy/api/v3/class-wc-api-products.php

2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
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() ) );
    }
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.5.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.