diff --git a/includes/abstracts/abstract-wc-data.php b/includes/abstracts/abstract-wc-data.php index f48bcbba8b8..5e328d5137d 100644 --- a/includes/abstracts/abstract-wc-data.php +++ b/includes/abstracts/abstract-wc-data.php @@ -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 );