WC_Product_Variable_Data_Store_CPT::validate_prices_data
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.
Returns
true|false. True if valid, false otherwise.
Usage
// protected - for code of main (parent) or child class $result = $this->validate_prices_data( $prices_array, $deprecated );
- $prices_array(array) (required)
- The prices data.
- $deprecated(string) (required)
- Was the current transient version, unused since 10.3.0.
WC_Product_Variable_Data_Store_CPT::validate_prices_data() WC Product Variable Data Store CPT::validate prices data code WC 10.5.0
protected function validate_prices_data( $prices_array, $deprecated ) {
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;
}
$price_data_is_empty = true;
foreach ( $prices_array 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;
}