diff --git a/plugins/woocommerce/changelog/tweak-more-robust-oa-field-injection b/plugins/woocommerce/changelog/tweak-more-robust-oa-field-injection new file mode 100644 index 00000000000..1c9e5a0b79b --- /dev/null +++ b/plugins/woocommerce/changelog/tweak-more-robust-oa-field-injection @@ -0,0 +1,4 @@ +Significance: patch +Type: update + +Inject order attribution checkout fields (only once) on a wider set of checkout form actions. diff --git a/plugins/woocommerce/src/Internal/Orders/OrderAttributionController.php b/plugins/woocommerce/src/Internal/Orders/OrderAttributionController.php index c359696fe1f..9aa0f137e7a 100644 --- a/plugins/woocommerce/src/Internal/Orders/OrderAttributionController.php +++ b/plugins/woocommerce/src/Internal/Orders/OrderAttributionController.php @@ -58,6 +58,13 @@ class OrderAttributionController implements RegisterHooksInterface { */ private $proxy; + /** + * Whether the `stamp_checkout_html_element` method has been called. + * + * @var bool + */ + private static $is_stamp_checkout_html_called = false; + /** * Initialization method. * @@ -111,7 +118,27 @@ class OrderAttributionController implements RegisterHooksInterface { } ); - add_action( 'woocommerce_checkout_after_customer_details', array( $this, 'stamp_html_element' ) ); + /** + * Filter set of actions used to stamp the unique checkout order attribution HTML container element. + * + * @since 9.0.0 + * + * @param array $stamp_checkout_html_actions The set of actions used to stamp the unique checkout order attribution HTML container element. + */ + $stamp_checkout_html_actions = apply_filters( + 'wc_order_attribution_stamp_checkout_html_actions', + array( + 'woocommerce_checkout_billing', + 'woocommerce_after_checkout_billing_form', + 'woocommerce_checkout_shipping', + 'woocommerce_after_order_notes', + 'woocommerce_checkout_after_customer_details', + ) + ); + foreach ( $stamp_checkout_html_actions as $action ) { + add_action( $action, array( $this, 'stamp_checkout_html_element_once' ) ); + } + add_action( 'woocommerce_register_form', array( $this, 'stamp_html_element' ) ); // Update order based on submitted fields. @@ -339,8 +366,25 @@ class OrderAttributionController implements RegisterHooksInterface { } /** - * Add `` element that contributes the order attribution values to the enclosing form. - * Used for checkout & customer register forms. + * Handles the `` element for checkout forms, ensuring that the field is only output once. + * + * @since 9.0.0 + * + * @return void + */ + public function stamp_checkout_html_element_once() { + if ( self::$is_stamp_checkout_html_called ) { + return; + } + $this->stamp_html_element(); + self::$is_stamp_checkout_html_called = true; + } + + /** + * Output `` element that contributes the order attribution values to the enclosing form. + * Used customer register forms, and for checkout forms through `stamp_checkout_html_element()`. + * + * @return void */ public function stamp_html_element() { printf( '' );