Merge pull request #20483 from woocommerce/fix/20431

Search for matching keys when updating meta data
This commit is contained in:
Claudiu Lodromanean 2018-06-11 10:26:24 -07:00 committed by GitHub
commit b351bcac50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 3 deletions

View File

@ -420,11 +420,34 @@ abstract class WC_Data {
$this->maybe_read_meta_data();
$array_key = $meta_id ? array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id ) : '';
$array_key = false;
if ( $meta_id ) {
$array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id, true );
$array_key = $array_keys ? current( $array_keys ) : false;
} else {
// Find matches by key.
$matches = array();
$ids_to_keys = wp_list_pluck( $this->meta_data, 'key', 'id' );
foreach ( $this->meta_data as $meta_data_array_key => $meta ) {
if ( $meta->key === $key ) {
$matches[] = $meta_data_array_key;
}
}
if ( ! empty( $matches ) ) {
// Set matches to null so only one key gets the new value.
foreach ( $matches as $meta_data_array_key ) {
$this->meta_data[ $meta_data_array_key ]->value = null;
}
$array_key = current( $matches );
}
}
if ( $array_key ) {
$meta = $this->meta_data[ current( $array_key ) ];
$meta->key = $key;
$meta = $this->meta_data[ $array_key ];
$meta->key = $key;
$meta->value = $value;
} else {
$this->add_meta_data( $key, $value, true );