Support deleting metadata just by meta id.

This commit is contained in:
Vedanshu Jain 2023-09-07 13:18:30 +05:30
parent 7860a08f80
commit 955fc37a90
1 changed files with 10 additions and 1 deletions

View File

@ -2791,10 +2791,19 @@ CREATE TABLE $meta_table (
* @return bool * @return bool
*/ */
public function delete_meta( &$object, $meta ) { public function delete_meta( &$object, $meta ) {
if ( $this->should_backfill_post_record() && isset( $meta->id ) && ! isset( $meta->key ) ) {
// Let's get the actual meta key before its deleted for backfilling. We cannot delete just by ID because meta IDs are different in HPOS and posts tables.
$db_meta = $this->data_store_meta->get_metadata_by_id( $meta->id );
if ( $db_meta ) {
$meta->key = $db_meta->meta_key;
$meta->value = $db_meta->meta_value;
}
}
$delete_meta = $this->data_store_meta->delete_meta( $object, $meta ); $delete_meta = $this->data_store_meta->delete_meta( $object, $meta );
$this->after_meta_change( $object, $meta ); $this->after_meta_change( $object, $meta );
if ( $object instanceof WC_Abstract_Order && $this->should_backfill_post_record() ) { if ( $object instanceof WC_Abstract_Order && $this->should_backfill_post_record() && isset( $meta->key ) ) {
self::$backfilling_order_ids[] = $object->get_id(); self::$backfilling_order_ids[] = $object->get_id();
delete_post_meta( $object->get_id(), $meta->key, $meta->value ); delete_post_meta( $object->get_id(), $meta->key, $meta->value );
self::$backfilling_order_ids = array_diff( self::$backfilling_order_ids, array( $object->get_id() ) ); self::$backfilling_order_ids = array_diff( self::$backfilling_order_ids, array( $object->get_id() ) );