WC_Product_Variable_Data_Store_CPT::validate_prices_data()protectedWC 1.0

Validate the prices data by checking the structure and type of the data.

Method of the class: WC_Product_Variable_Data_Store_CPT{}

No Hooks.

Return

true|false. True if valid, false otherwise.

Usage

// protected - for code of main (parent) or child class
$result = $this->validate_prices_data( $prices_array, $current_version );
$prices_array(array) (required)
The prices data.
$current_version(string) (required)
The current version of the data.

WC_Product_Variable_Data_Store_CPT::validate_prices_data() code WC 9.7.1

protected function validate_prices_data( $prices_array, $current_version ) {
	if ( ! is_array( $prices_array ) ) {
		return false;
	}

	// Fail if array is empty - we want to rebuild in this case.
	if ( empty( $prices_array ) ) {
		return false;
	}

	if ( isset( $prices_array['version'] ) && $prices_array['version'] !== $current_version ) {
		return false;
	}

	$data_without_version = array_diff_key( $prices_array, array( 'version' => '' ) );
	$price_data_is_empty  = true;

	foreach ( $data_without_version as $price_data ) {
		if ( ! is_array( $price_data ) ) {
			return false;
		}

		$required_types = array( 'price', 'regular_price', 'sale_price' );

		foreach ( $required_types as $type ) {
			// If all 'price' fields are empty, we want to track that so we can rebuild the data.
			if ( 'price' === $type && ! empty( $price_data[ $type ] ) && $price_data_is_empty ) {
				$price_data_is_empty = false;
			}

			if ( ! isset( $price_data[ $type ] ) || ! is_array( $price_data[ $type ] ) ) {
				return false;
			}
		}

		$variation_ids = array_keys( $price_data['price'] );

		foreach ( $variation_ids as $variation_id ) {
			if ( ! is_numeric( $variation_id ) ) {
				return false;
			}

			foreach ( $required_types as $type ) {
				if ( ! array_key_exists( $variation_id, $price_data[ $type ] ) ) {
					return false;
				}

				$type_price = $price_data[ $type ][ $variation_id ];
				if ( ! is_numeric( $type_price ) && '' !== $type_price ) {
					return false;
				}
			}
		}
	}

	// If price is empty, we want to rebuild the data.
	if ( $price_data_is_empty ) {
		return false;
	}

	return true;
}