WC_API_XML_Handler::format_product_data()publicWC 2.1

Adjust the product data to handle options for attributes without a named child element and other fields that have no named child elements (e.g. categories = array( 'cat1', 'cat2' ) )

Note that the parent product data for variations is also adjusted in the same manner as needed

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

Хуков нет.

Возвращает

Массив.

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

$WC_API_XML_Handler = new WC_API_XML_Handler();
$WC_API_XML_Handler->format_product_data( $data );
$data(массив) (обязательный)
-

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

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

Код WC_API_XML_Handler::format_product_data() WC 8.7.0

public function format_product_data( $data ) {

	// handle attribute values
	if ( ! empty( $data['attributes'] ) ) {

		foreach ( $data['attributes'] as $attribute_key => $attribute ) {

			if ( ! empty( $attribute['options'] ) && is_array( $attribute['options'] ) ) {

				foreach ( $attribute['options'] as $option_key => $option ) {

					unset( $data['attributes'][ $attribute_key ]['options'][ $option_key ] );

					$data['attributes'][ $attribute_key ]['options']['option'][] = array( $option );
				}
			}
		}
	}

	// simple arrays are fine for JSON, but XML requires a child element name, so this adjusts the data
	// array to define a child element name for each field
	$fields_to_fix = array(
		'related_ids'    => 'related_id',
		'upsell_ids'     => 'upsell_id',
		'cross_sell_ids' => 'cross_sell_id',
		'categories'     => 'category',
		'tags'           => 'tag',
	);

	foreach ( $fields_to_fix as $parent_field_name => $child_field_name ) {

		if ( ! empty( $data[ $parent_field_name ] ) ) {

			foreach ( $data[ $parent_field_name ] as $field_key => $field ) {

				unset( $data[ $parent_field_name ][ $field_key ] );

				$data[ $parent_field_name ][ $child_field_name ][] = array( $field );
			}
		}
	}

	// handle adjusting the parent product for variations
	if ( ! empty( $data['parent'] ) ) {

		// attributes
		if ( ! empty( $data['parent']['attributes'] ) ) {

			foreach ( $data['parent']['attributes'] as $attribute_key => $attribute ) {

				if ( ! empty( $attribute['options'] ) && is_array( $attribute['options'] ) ) {

					foreach ( $attribute['options'] as $option_key => $option ) {

						unset( $data['parent']['attributes'][ $attribute_key ]['options'][ $option_key ] );

						$data['parent']['attributes'][ $attribute_key ]['options']['option'][] = array( $option );
					}
				}
			}
		}

		// fields
		foreach ( $fields_to_fix as $parent_field_name => $child_field_name ) {

			if ( ! empty( $data['parent'][ $parent_field_name ] ) ) {

				foreach ( $data['parent'][ $parent_field_name ] as $field_key => $field ) {

					unset( $data['parent'][ $parent_field_name ][ $field_key ] );

					$data['parent'][ $parent_field_name ][ $child_field_name ][] = array( $field );
				}
			}
		}
	}

	return $data;
}