Stamp the order attribution HTML element (only once) on a wider set of checkout form actions (#46834)

* Inject the fields (only once) on a large set of checkout form actions

* Changelog

* Better doc format

* Add _once to the new method to clarify functionality

* Filter to modify the default list of actions
This commit is contained in:
Justin Palmer 2024-05-07 15:54:32 +02:00 committed by GitHub
parent 40486a0151
commit f401a98301
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 51 additions and 3 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: update
Inject order attribution checkout fields (only once) on a wider set of checkout form actions.

View File

@ -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 `<wc-order-attribution-inputs>` element that contributes the order attribution values to the enclosing form.
* Used for checkout & customer register forms.
* Handles the `<wc-order-attribution-inputs>` 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 `<wc-order-attribution-inputs>` 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( '<wc-order-attribution-inputs></wc-order-attribution-inputs>' );