Skip to content

feat(checkout-form): warn when required field is missing for products#1146

Merged
superdav42 merged 1 commit intomainfrom
checkout-form-validation-notices
May 7, 2026
Merged

feat(checkout-form): warn when required field is missing for products#1146
superdav42 merged 1 commit intomainfrom
checkout-form-validation-notices

Conversation

@superdav42
Copy link
Copy Markdown
Collaborator

@superdav42 superdav42 commented May 7, 2026

Summary

  • Add admin notice in the checkout form editor when products with price variations are configured but no period_selection field is on the form.
  • Add admin notice when products that don't force a specific site template (site_templates limitation mode != MODE_ASSIGN_TEMPLATE) are configured but no template_selection field is on the form.
  • Notices list the offending product names so the admin knows which ones triggered the warning.

Test plan

  • Create a checkout form with a pricing_table field referencing a product that has price variations, and no period_selection field → notice appears.
  • Add a period_selection field → notice disappears.
  • Create a checkout form with a products field referencing a product whose site template limitation mode is not assign_template, and no template_selection field → notice appears.
  • Add a template_selection field, OR set the product's site template mode to assign_template → notice disappears.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Checkout editor now displays contextual warning notices when required configuration fields are missing
    • Alerts for missing period selection fields (when products have price variations) and template selection fields
    • Identifies affected products and provides guidance for adding required fields or adjusting settings

Add admin notices in the checkout form editor when products configured
in pricing_table or products fields require a companion field that the
form does not provide:

- Period Selection notice: products with price variations need a
  period_selection field for customers to switch between billing cycles.
- Template Selection notice: products that don't force a specific site
  template (site_templates limitation mode != MODE_ASSIGN_TEMPLATE)
  need a template_selection field for customers to pick a template.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 7, 2026

Review Change Stack

📝 Walkthrough

Walkthrough

This PR adds conditional admin notices to the checkout form editor. Backend helper methods analyze products to identify which are missing required selection fields (template or period), compute product name lists, pass them to the editor view, and the view renders warnings with actionable instructions when products need selection capability.

Changes

Checkout Form Editor Missing Selection Field Notices

Layer / File(s) Summary
Notice Product Computation
inc/admin-pages/class-checkout-form-edit-admin-page.php
get_template_selection_notice_products() and get_price_variation_notice_products() parse checkout form products, load product objects, validate selection field requirements, and return product names for admin notices.
Template Context Wiring
inc/admin-pages/class-checkout-form-edit-admin-page.php
render_steps() passes checkout form slug and helper method results as template context to the editor view.
Editor UI Notice Rendering
views/base/checkout-forms/steps.php
Two conditional styled warning panels render at editor top when products are missing Period or Template Selection fields, listing affected products and instructions to add the fields.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 Notice me, dear admin, with products so fine,
Your selection fields missing—here's my design!
Two helpers now guard what your checkout shall show,
Yellow warnings aglow, so no templates miss the flow. 🟡

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: adding warning notices in the checkout form editor when required fields are missing for products.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch checkout-form-validation-notices

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 7, 2026

🔨 Build Complete - Ready for Testing!

📦 Download Build Artifact (Recommended)

Download the zip build, upload to WordPress and test:

🌐 Test in WordPress Playground (Very Experimental)

Click the link below to instantly test this PR in your browser - no installation needed!
Playground support for multisite is very limitied, hopefully it will get better in the future.

🚀 Launch in Playground

Login credentials: admin / password

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
views/base/checkout-forms/steps.php (1)

12-12: 💤 Low value

Defensive: guard against the variables not being defined.

$price_variation_notice_products and $template_selection_notice_products are always passed by the current caller in render_steps(), but this view is plain wu_get_template-extracted state, so a missed callsite would surface as an undefined variable warning at the top of the editor. A cheap isset() (or ?? []) keeps the view robust if it's ever rendered from another path.

🛡️ Suggested guard
-	<?php if ( ! empty($price_variation_notice_products)) : ?>
+	<?php if ( ! empty($price_variation_notice_products ?? [])) : ?>
...
-	<?php if ( ! empty($template_selection_notice_products)) : ?>
+	<?php if ( ! empty($template_selection_notice_products ?? [])) : ?>

Also applies to: 32-32

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@views/base/checkout-forms/steps.php` at line 12, The view uses
$price_variation_notice_products and $template_selection_notice_products
directly which can trigger "undefined variable" warnings if this template is
rendered outside render_steps(); before any conditional checks (e.g., the
if(...) that references these variables) ensure each variable is
defined/defaulted (use isset() or the null coalescing operator to set them to an
empty array) so the existing conditionals work safely even when the caller
forgets to pass them.
inc/admin-pages/class-checkout-form-edit-admin-page.php (1)

1116-1265: ⚡ Quick win

Extract shared product-ID collection to remove duplication.

get_template_selection_notice_products() and get_price_variation_notice_products() share ~30 lines of identical logic for parsing pricing_table/products field IDs and turning them into a deduped product-ID set. Only the per-product predicate differs. Extracting a shared helper keeps the two notice methods focused on their predicates and avoids future drift between them (e.g., if a new product-ID-bearing field type is added, only one place needs updating).

♻️ Suggested refactor
+	/**
+	 * Collects product IDs referenced by `pricing_table` / `products` fields on the form.
+	 *
+	 * `@since` 2.4.13
+	 *
+	 * `@param` \WP_Ultimo\Models\Checkout_Form $form The checkout form.
+	 * `@return` int[] Deduped, positive product IDs.
+	 */
+	protected function collect_product_ids_from_form($form): array {
+
+		$product_id_fields = $form->get_all_fields_by_type(['pricing_table', 'products']);
+
+		if (empty($product_id_fields)) {
+			return [];
+		}
+
+		$product_ids = [];
+
+		foreach ($product_id_fields as $field) {
+			$type = $field['type'] ?? '';
+
+			if ('pricing_table' === $type) {
+				$ids_string = (string) ($field['pricing_table_products'] ?? '');
+			} elseif ('products' === $type) {
+				$ids_string = (string) ($field['products'] ?? '');
+			} else {
+				continue;
+			}
+
+			if ('' === $ids_string) {
+				continue;
+			}
+
+			foreach (explode(',', $ids_string) as $id) {
+				$id = absint(trim($id));
+
+				if ($id) {
+					$product_ids[ $id ] = $id;
+				}
+			}
+		}
+
+		return $product_ids;
+	}

Then both methods reduce to:

-		$product_id_fields = $form->get_all_fields_by_type(['pricing_table', 'products']);
-
-		if (empty($product_id_fields)) {
-			return [];
-		}
-
-		$product_ids = [];
-
-		foreach ($product_id_fields as $field) {
-			...
-		}
-
-		if (empty($product_ids)) {
-			return [];
-		}
+		$product_ids = $this->collect_product_ids_from_form($form);
+
+		if (empty($product_ids)) {
+			return [];
+		}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@inc/admin-pages/class-checkout-form-edit-admin-page.php` around lines 1116 -
1265, Both notice methods duplicate the logic that parses
'pricing_table'/'products' fields into a deduplicated list of product IDs;
extract that logic into a single helper (e.g., protected function
collect_product_ids_from_form($form): array) and call it from
get_template_selection_notice_products() and
get_price_variation_notice_products(). The helper should accept the
Checkout_Form instance (or the form object returned by $this->get_object()),
iterate the fields returned by
get_all_fields_by_type(['pricing_table','products']), build the same deduped
array of absint(trim($id)) product IDs, skip empty strings, and return [] when
none found so existing per-product predicates (template/limitations or price
variations) remain unchanged. Update both notice methods to remove the
duplicated loops and use the new helper while preserving all original behavior
and return values.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@inc/admin-pages/class-checkout-form-edit-admin-page.php`:
- Around line 1116-1265: Both notice methods duplicate the logic that parses
'pricing_table'/'products' fields into a deduplicated list of product IDs;
extract that logic into a single helper (e.g., protected function
collect_product_ids_from_form($form): array) and call it from
get_template_selection_notice_products() and
get_price_variation_notice_products(). The helper should accept the
Checkout_Form instance (or the form object returned by $this->get_object()),
iterate the fields returned by
get_all_fields_by_type(['pricing_table','products']), build the same deduped
array of absint(trim($id)) product IDs, skip empty strings, and return [] when
none found so existing per-product predicates (template/limitations or price
variations) remain unchanged. Update both notice methods to remove the
duplicated loops and use the new helper while preserving all original behavior
and return values.

In `@views/base/checkout-forms/steps.php`:
- Line 12: The view uses $price_variation_notice_products and
$template_selection_notice_products directly which can trigger "undefined
variable" warnings if this template is rendered outside render_steps(); before
any conditional checks (e.g., the if(...) that references these variables)
ensure each variable is defined/defaulted (use isset() or the null coalescing
operator to set them to an empty array) so the existing conditionals work safely
even when the caller forgets to pass them.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d28a2e53-2f4b-4ce6-9ff2-446feb107c7d

📥 Commits

Reviewing files that changed from the base of the PR and between 2e974cc and abdea57.

📒 Files selected for processing (2)
  • inc/admin-pages/class-checkout-form-edit-admin-page.php
  • views/base/checkout-forms/steps.php

@superdav42 superdav42 merged commit f0f8837 into main May 7, 2026
11 checks passed
@superdav42
Copy link
Copy Markdown
Collaborator Author

Summary

  • Add admin notice in the checkout form editor when products with price variations are configured but no period_selection field is on the form.
  • Add admin notice when products that don't force a specific site template (site_templates limitation mode != MODE_ASSIGN_TEMPLATE) are configured but no template_selection field is on the form.
  • Notices list the offending product names so the admin knows which ones triggered the warning.

Test plan

  • Create a checkout form with a pricing_table field referencing a product that has price variations, and no period_selection field → notice appears.
  • Add a period_selection field → notice disappears.
  • Create a checkout form with a products field referencing a product whose site template limitation mode is not assign_template, and no template_selection field → notice appears.
  • Add a template_selection field, OR set the product's site template mode to assign_template → notice disappears.
    🤖 Generated with Claude Code

Merged via PR #1146 to main.
Merged by deterministic merge pass (pulse-wrapper.sh).


aidevops.sh v3.14.83 spent 32s on this as a headless bash routine.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 7, 2026

Performance Test Results

Performance test results for 7722bef are in 🛎️!

Note: the numbers in parentheses show the difference to the previous (baseline) test run. Differences below 2% or 0.5 in absolute values are not shown.

URL: /

Run DB Queries Memory Before Template Template WP Total LCP TTFB LCP - TTFB
0 40 (-1 / -3% ) 37.78 MB 903.00 ms 162.50 ms (-4.50 ms / -3% ) 1084.50 ms 2036.00 ms 1943.00 ms 92.95 ms (+7.20 ms / +8% )
1 56 49.11 MB 900.50 ms (-30.00 ms / -3% ) 139.50 ms (-4.50 ms / -3% ) 1037.50 ms (-35.00 ms / -3% ) 1976.00 ms 1897.65 ms 78.35 ms

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant