Skip to content

fix(woocommerce): persist custom meta on new product first save#4715

Open
dkoo wants to merge 2 commits into
releasefrom
hotfix/nppd-1485-group-subscription-meta-unsaved-product
Open

fix(woocommerce): persist custom meta on new product first save#4715
dkoo wants to merge 2 commits into
releasefrom
hotfix/nppd-1485-group-subscription-meta-unsaved-product

Conversation

@dkoo
Copy link
Copy Markdown
Contributor

@dkoo dkoo commented May 11, 2026

All Submissions:

Changes proposed in this Pull Request:

Fixes a bug where Newspack's custom product options (e.g., Group subscription enabled flag and member limit) were silently dropped on the first save of a brand-new product whose type was being changed via the product-type dropdown (e.g., new product → "Simple subscription"). Subsequent edits to existing products were unaffected.

Root cause: Both WooCommerce_Products::save_custom_product_options and WC core's WC_Meta_Box_Product_Data::save are hooked on woocommerce_process_product_meta at priority 10, so the order depends on registration timing. Newspack registers first, which means at the moment our handler runs, $product->get_type() still returns the auto-draft default (simple) — WC hasn't yet updated the product-type term. Our type-filter at class-woocommerce-products.php:289 then skips options whose product_types list doesn't include simple (Group subscription requires subscription or subscription_variation).

Fix: Read the product type from $_POST['product-type'] as the source of truth, falling back to $product->get_type() when not present (REST, programmatic saves). Mirrors WC core's own pattern in WC_Meta_Box_Product_Data::save().

Closes NPPD-1485.

How to test the changes in this Pull Request:

  1. Set define( 'NEWSPACK_CONTENT_GATES', true ); in wp-config.php so the Group subscription option is registered.
  2. Ensure WooCommerce + WooCommerce Subscriptions are active.
  3. Simple subscription, first save (the bug):
    1. Go to Products → Add New.
    2. Set product title, choose product type "Simple subscription", set a subscription price.
    3. Tick Group subscription and set Group subscription member limit to e.g. 5.
    4. Click Save Draft.
    5. ✅ Reload the product — Group subscription should still be enabled and the limit preserved. Pre-fix: both values were lost.
  4. Existing-product edit (no regression):
    1. Open the product saved in step 3.
    2. Change the limit to a different value, click Update.
    3. ✅ The new value persists.
  5. Variable subscription parent + variations (untouched code path):
    1. Products → Add New, set type to Variable subscription, save draft.
    2. Add an attribute (used for variations), generate variations.
    3. Expand a variation, set subscription price and tick Group subscription, set a limit.
    4. Save changes.
    5. ✅ Variation persists Group subscription meta on its first save (this code path was already correct — verified in review).
  6. Programmatic / REST save (out of scope): REST API (POST /wp-json/wc/v3/products) and the new WC product block editor bypass woocommerce_process_product_meta entirely and are not covered by this PR. To be tracked in a follow-up.

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable? — Deferred: extending the existing WC_Product mock at tests/mocks/wc-mocks.php (no update_meta_data/save/get_status/get_date_*) is out of scope for this hotfix. Verified empirically against a live env. Follow-up ticket recommended.
  • Have you successfully ran tests with your changes locally? — n test-php: 924 tests, 2859 assertions, all passing.

@dkoo dkoo self-assigned this May 11, 2026
@dkoo dkoo added the [Status] Needs Review The issue or pull request needs to be reviewed label May 11, 2026
@dkoo dkoo marked this pull request as ready for review May 11, 2026 21:52
@dkoo dkoo requested a review from a team as a code owner May 11, 2026 21:52
Copilot AI review requested due to automatic review settings May 11, 2026 21:52
@github-actions
Copy link
Copy Markdown

🎉 This PR is included in version 6.39.4-hotfix-nppd-1485-group-subscription-meta-unsaved-product.1 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes WooCommerce admin “first save” behavior where Newspack custom product options could be dropped when creating a new product and changing its type via the product-type dropdown (due to hook ordering causing $product->get_type() to still read as simple during woocommerce_process_product_meta).

Changes:

  • Derive the effective product type from $_POST['product-type'] (sanitized) during save_custom_product_options, with fallback to $product->get_type().
  • Use the derived $product_type for product_types filtering when persisting custom option meta.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Status] Needs Review The issue or pull request needs to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants