Automattic / woocommerce-subscriptions-core

Subscriptions core package for WooCommerce
Other
80 stars 29 forks source link

Unable to delete custom order item meta data from WC_Subscription object #585

Open Johnny99211 opened 3 months ago

Johnny99211 commented 3 months ago

Describe the bug

I am facing a massive issue which is only facing inside a subscription order. When reproducing this issue inside a normal WooCommerce order, everything works as expected.

As a plugin developer, I'm always trying to make good compatibility. In my case, my plugin sets a custom meta to a WooCommerce order or subscription, depending on if it's a normal order or subscription:

add_action( 'woocommerce_checkout_create_order_line_item', array( $this, 'woocommerce_checkout_create_order_line_item_action' ), 10, 4 );
public function woocommerce_checkout_create_order_line_item_action( WC_Order_Item_Product $order_item, string $cart_item_key, array $values, WC_Order $order ): void {
    if ( ! empty( $values['elm_license_id'] ) ) {
        $order_item->add_meta_data( '_elm_license_id', $values['elm_license_id'] );
    }
}

This works as expected and documented inside the WooCommerce developer documentation:

image

When I now press the “Assign license” button (which just triggers the form submit with an additional hidden field), a script runs within the following hook which does some things. At the end, the custom meta should be deleted. This works fine inside any normal order, but inside a subscription, nothing happens. The meta is still there (normal posts table and HPOS). It was working before the version 6.0.0 but since the update, I'm no longer able to delete any custom meta:

add_action( 'woocommerce_update_order', array( $this, 'woocommerce_update_order_action' ), 99, 3 );
public function woocommerce_update_order_action( int $order_id, WC_Order $order ): void {
    foreach ( $order->get_items() as $order_item ) {
        $order_item->delete_meta_data( '_elm_license_id' );
        $order_item->save();
    }
}

To Reproduce

  1. Use the above hooks to set a custom meta when creating an order / subscription
  2. Use the 2nd snippet to delete the meta again when pressing the “Update” button inside a subscription

Expected behavior

The custom meta should be gone.

Actual behavior

The custom meta is still there.

Product impact