Automattic\WooCommerce\Internal\RestApi\Routes\V4\Settings\Products\Schema

ProductSettingsSchema::get_item_responsepublicWC 1.0

Get product settings data by transforming WC_Settings_Products data into REST API format.

Method of the class: ProductSettingsSchema{}

No Hooks.

Returns

Array.

Usage

$ProductSettingsSchema = new ProductSettingsSchema();
$ProductSettingsSchema->get_item_response( $item, $request, $include_fields ): array;
$item(mixed) (required)
Settings products instance.
$request(WP_REST_Request) (required)
Request object.
$include_fields(array)
Fields to include.
Default: array()

ProductSettingsSchema::get_item_response() code WC 10.4.3

public function get_item_response( $item, WP_REST_Request $request, array $include_fields = array() ): array {
	$raw_settings = $item;

	// Transform raw settings into grouped format based on title/sectionend markers.
	$groups           = array();
	$values           = array();
	$current_group    = null;
	$current_group_id = null;

	foreach ( $raw_settings as $setting ) {
		$setting_type = $setting['type'] ?? '';

		// Handle section titles - start of a new group.
		if ( 'title' === $setting_type ) {
			$current_group_id = $setting['id'] ?? '';
			$current_group    = array(
				'title'       => $setting['title'] ?? '',
				'description' => $setting['desc'] ?? '',
				'order'       => isset( $setting['order'] ) ? (int) $setting['order'] : 999,
				'fields'      => array(),
			);
			continue;
		}

		// Handle section ends - save the current group.
		if ( 'sectionend' === $setting_type ) {
			if ( $current_group && $current_group_id ) {
				$groups[ $current_group_id ] = $current_group;
			}
			$current_group    = null;
			$current_group_id = null;
			continue;
		}

		// Skip title and sectionend types.
		if ( in_array( $setting_type, array( 'title', 'sectionend' ), true ) ) {
			continue;
		}

		// Convert setting to field format.
		if ( isset( $setting['id'] ) && $current_group ) {
			$field = $this->transform_setting_to_field( $setting );
			if ( $field ) {
				$current_group['fields'][] = $field;
				// Add field value to the flat values array.
				$raw_value              = get_option( $field['id'], $setting['default'] ?? '' );
				$values[ $field['id'] ] = $this->validate_field_value( $raw_value, $field['type'] );
			}
		}
	}

	// Sort groups by their order if available.
	uasort(
		$groups,
		function ( $a, $b ) {
			$a_order = $a['order'] ?? 999;
			$b_order = $b['order'] ?? 999;
			return $a_order - $b_order;
		}
	);

	return array(
		'id'          => 'products',
		'title'       => __( 'Products', 'woocommerce' ),
		'description' => __( 'Manage product settings including dimensions, weight units, and display options.', 'woocommerce' ),
		'values'      => $values,
		'groups'      => $groups,
	);
}