Automattic\WooCommerce\Admin\API

ProductAttributes::format_custom_attribute_items_for_response()protectedWC 1.0

Format custom attribute items for response (mimic the structure of a taxonomy - backed attribute).

Method of the class: ProductAttributes{}

No Hooks.

Return

Array.

Usage

// protected - for code of main (parent) or child class
$result = $this->format_custom_attribute_items_for_response( $custom_attributes );
$custom_attributes(array) (required)
- CustomAttributeTraits::get_custom_attributes().

ProductAttributes::format_custom_attribute_items_for_response() code WC 8.6.1

protected function format_custom_attribute_items_for_response( $custom_attributes ) {
	$response = array();

	foreach ( $custom_attributes as $attribute_key => $attribute_value ) {
		$data = array(
			'id'           => $attribute_key,
			'name'         => $attribute_value['name'],
			'slug'         => $attribute_key,
			'type'         => 'select',
			'order_by'     => 'menu_order',
			'has_archives' => false,
		);

		$item_response = rest_ensure_response( $data );
		$item_response->add_links( $this->prepare_links( (object) array( 'attribute_id' => $attribute_key ) ) );
		$item_response = $this->prepare_response_for_collection(
			$item_response
		);

		$response[] = $item_response;
	}

	return $response;
}