Automattic\WooCommerce\StoreApi\Utilities

CartController::sanitize_variation_data()protectedWC 1.0

Format and sanitize variation data posted to the API.

Labels are converted to names (e.g. Size to pa_size), and values are cleaned.

Method of the class: CartController{}

No Hooks.

Return

Array.

Usage

// protected - for code of main (parent) or child class
$result = $this->sanitize_variation_data( $variation_data, $variable_product_attributes );
$variation_data(array) (required)
Key value pairs of attributes and values.
$variable_product_attributes(array) (required)
Product attributes we're expecting.

CartController::sanitize_variation_data() code WC 9.3.1

protected function sanitize_variation_data( $variation_data, $variable_product_attributes ) {
	$return = [];

	foreach ( $variable_product_attributes as $attribute ) {
		if ( ! $attribute['is_variation'] ) {
			continue;
		}
		$attribute_label          = wc_attribute_label( $attribute['name'] );
		$variation_attribute_name = wc_variation_attribute_name( $attribute['name'] );

		// Attribute labels e.g. Size.
		if ( isset( $variation_data[ $attribute_label ] ) ) {
			$return[ $variation_attribute_name ] =
				$attribute['is_taxonomy']
					?
					sanitize_title( $variation_data[ $attribute_label ] )
					:
					html_entity_decode(
						wc_clean( $variation_data[ $attribute_label ] ),
						ENT_QUOTES,
						get_bloginfo( 'charset' )
					);
			continue;
		}

		// Attribute slugs e.g. pa_size.
		if ( isset( $variation_data[ $attribute['name'] ] ) ) {
			$return[ $variation_attribute_name ] =
				$attribute['is_taxonomy']
					?
					sanitize_title( $variation_data[ $attribute['name'] ] )
					:
					html_entity_decode(
						wc_clean( $variation_data[ $attribute['name'] ] ),
						ENT_QUOTES,
						get_bloginfo( 'charset' )
					);
		}
	}
	return $return;
}