WC_API_Products::edit_product_attribute_term()publicWC 2.5.0

Edit a product attribute term.

Метод класса: WC_API_Products{}

Возвращает

Массив|WP_Error.

Использование

$WC_API_Products = new WC_API_Products();
$WC_API_Products->edit_product_attribute_term( $attribute_id, $id, $data );
$attribute_id(int) (обязательный)
Attribute ID.
$id(int) (обязательный)
the attribute ID.
$data(массив) (обязательный)
-

Список изменений

С версии 2.5.0 Введена.

Код WC_API_Products::edit_product_attribute_term() WC 8.7.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() ) );
	}
}