Commit aca44fada68 for woocommerce
commit aca44fada6813c1bed1e7b8c22f87af2b6b92583
Author: Jorge A. Torres <jorge.torres@automattic.com>
Date: Fri Nov 27 07:34:16 2026 -0500
Fix fatal error saving an order with a removed download permission (#71513)
diff --git a/plugins/woocommerce/changelog/fix-order-downloads-invalid-permission-fatal b/plugins/woocommerce/changelog/fix-order-downloads-invalid-permission-fatal
new file mode 100644
index 00000000000..298a348ca47
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-order-downloads-invalid-permission-fatal
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fix a fatal error when saving an order after one of its download permissions was removed.
diff --git a/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-order-downloads.php b/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-order-downloads.php
index bc99197dbbc..9f92d031bb2 100644
--- a/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-order-downloads.php
+++ b/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-order-downloads.php
@@ -103,7 +103,22 @@ class WC_Meta_Box_Order_Downloads {
if ( ! isset( $permission_ids[ $i ] ) ) {
continue;
}
- $download = new WC_Customer_Download( $permission_ids[ $i ] );
+
+ try {
+ $download = new WC_Customer_Download( $permission_ids[ $i ] );
+ } catch ( Exception $e ) {
+ // Permission row no longer exists, e.g. revoked between page load and save, so skip it.
+ wc_get_logger()->notice(
+ 'Skipped a download permission that no longer exists while saving an order.',
+ array(
+ 'error' => $e->getMessage(),
+ 'permission_id' => $permission_ids[ $i ],
+ 'order_id' => $post_id,
+ )
+ );
+ continue;
+ }
+
$download->set_downloads_remaining( wc_clean( $downloads_remaining[ $i ] ) );
$download->set_access_expires( array_key_exists( $i, $access_expires ) && '' !== $access_expires[ $i ] ? strtotime( $access_expires[ $i ] ) : '' );
$download->save();
diff --git a/plugins/woocommerce/tests/php/includes/admin/meta-boxes/class-wc-meta-box-order-downloads-test.php b/plugins/woocommerce/tests/php/includes/admin/meta-boxes/class-wc-meta-box-order-downloads-test.php
new file mode 100644
index 00000000000..b0cdfb249d0
--- /dev/null
+++ b/plugins/woocommerce/tests/php/includes/admin/meta-boxes/class-wc-meta-box-order-downloads-test.php
@@ -0,0 +1,41 @@
+<?php
+declare( strict_types = 1 );
+
+/**
+ * Tests for WC_Meta_Box_Order_Downloads::save().
+ *
+ * @package WooCommerce\Tests\Admin
+ */
+class WC_Meta_Box_Order_Downloads_Test extends WC_Unit_Test_Case {
+
+ /**
+ * @testdox Should skip a download permission row deleted after the order-edit page loaded, and still save the rest.
+ */
+ public function test_skips_a_permission_row_deleted_before_save(): void {
+ $valid_permission = new WC_Customer_Download();
+ $valid_permission->set_order_id( 1 );
+ $valid_permission->set_user_email( 'customer@example.com' );
+ $valid_permission->save();
+
+ $stale_permission = new WC_Customer_Download();
+ $stale_permission->set_order_id( 1 );
+ $stale_permission->set_user_email( 'customer@example.com' );
+ $stale_permission->save();
+ $stale_permission_id = $stale_permission->get_id();
+
+ // Simulate the row being revoked/cleaned up after the order-edit page rendered but before the form posted.
+ WC_Data_Store::load( 'customer-download' )->delete_by_id( $stale_permission_id );
+
+ $_POST['permission_id'] = array(
+ $valid_permission->get_id(),
+ $stale_permission_id,
+ );
+ $_POST['downloads_remaining'] = array( '5', '5' );
+ $_POST['access_expires'] = array( '', '' );
+
+ WC_Meta_Box_Order_Downloads::save( 1, null );
+
+ $reloaded = new WC_Customer_Download( $valid_permission->get_id() );
+ $this->assertSame( 5, $reloaded->get_downloads_remaining(), 'The valid permission row should still be updated even though a sibling row was stale.' );
+ }
+}