From 151b5b41a40e6a5c3afef23e40731f9b1b16865b Mon Sep 17 00:00:00 2001 From: YvesCesar Date: Tue, 29 Sep 2026 17:45:45 -0400 Subject: [PATCH 1/2] fix: require the policy consent on the block checkout Signed-off-by: YvesCesar --- inc/checkout.php | 53 ++++++++++++++++---------- tests/Integration/Inc/CheckoutTest.php | 40 +++++++++++-------- 2 files changed, 57 insertions(+), 36 deletions(-) diff --git a/inc/checkout.php b/inc/checkout.php index 309bba5..46679cf 100644 --- a/inc/checkout.php +++ b/inc/checkout.php @@ -1,6 +1,6 @@ 'libresign/policy-consent', + 'label' => __( 'I agree to the terms and privacy policy before placing the order.', 'libresign' ), + 'location' => 'order', + 'type' => 'checkbox', + 'required' => true, + 'error_message' => __( 'You must agree to the policies before completing the purchase.', 'libresign' ), + ) + ); +} +add_action( 'woocommerce_init', 'libresign_theme_register_policy_consent_field' ); - return sprintf( +/** + * Link the checkout terms text to the policy. + */ +function libresign_theme_link_checkout_terms_to_policy( $block_content ) { + $text = sprintf( /* translators: %s: policy page link */ - __( 'I agree to the %s before placing the order.', 'libresign' ), + __( 'Read the %s.', 'libresign' ), sprintf( '%s', - esc_url( $policy_url ), + esc_url( libresign_theme_get_policy_url() ), esc_html__( 'terms and privacy policy', 'libresign' ) ) ); -} -add_filter( 'woocommerce_get_terms_and_conditions_checkbox_text', 'libresign_theme_checkout_policy_checkbox_text' ); -/** - * Prevent checkout submission without policy terms acceptance. - */ -function libresign_theme_validate_checkout_policy_consent( $data, $errors ) { - if ( empty( $data['terms'] ) ) { - $errors->add( - 'libresign_policy_consent', - __( 'You must agree to the policies before completing the purchase.', 'libresign' ) - ); - } + return (string) preg_replace( + '/]*\bwp-block-woocommerce-checkout-terms-block\b)/', + '
get( CheckoutFields::class )->get_additional_fields()['libresign/policy-consent']; + + $this->assertSame( 'checkbox', $field['type'] ); + $this->assertSame( 'order', $field['location'] ); + $this->assertTrue( $field['required'] ); + $this->assertSame( 'I agree to the terms and privacy policy before placing the order.', $field['label'] ); + $this->assertSame( 'You must agree to the policies before completing the purchase.', $field['errorMessage'] ); } - public function test_the_terms_checkbox_links_to_the_policy_in_a_new_tab() { + /** + * @dataProvider provide_terms_blocks + */ + public function test_links_the_checkout_terms_to_the_policy( $block_content ) { $this->assertSame( - 'I agree to the terms and privacy policy before placing the order.', - apply_filters( 'woocommerce_get_terms_and_conditions_checkbox_text', '' ) + '
checkout_errors( array( 'terms' => 0 ) ); - - $this->assertContains( 'libresign_policy_consent', $errors->get_error_codes() ); + public static function provide_terms_blocks() { + yield 'as saved' => array( '
' ); + yield 'as woocommerce renders it' => array( '
' ); } - public function test_accepts_an_order_with_the_policy_consent() { - $errors = $this->checkout_errors( array( 'terms' => 1 ) ); - - $this->assertNotContains( 'libresign_policy_consent', $errors->get_error_codes() ); + public function test_leaves_other_markup_alone() { + $this->assertSame( '
', libresign_theme_link_checkout_terms_to_policy( '
' ) ); } } From a8119f6e2631ccb40b6b8b0b1c9336e4d560f911 Mon Sep 17 00:00:00 2001 From: YvesCesar Date: Tue, 29 Sep 2026 18:07:35 -0400 Subject: [PATCH 2/2] fix: keep refusing classic checkout orders without the policy consent Signed-off-by: YvesCesar --- inc/checkout.php | 13 +++++++++++++ tests/Integration/Inc/CheckoutTest.php | 17 +++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/inc/checkout.php b/inc/checkout.php index 46679cf..e8c2bfe 100644 --- a/inc/checkout.php +++ b/inc/checkout.php @@ -28,6 +28,19 @@ function libresign_theme_register_policy_consent_field() { } add_action( 'woocommerce_init', 'libresign_theme_register_policy_consent_field' ); +/** + * Refuse an order placed through the classic checkout without the policy consent. + */ +function libresign_theme_validate_checkout_policy_consent( $data, $errors ) { + if ( empty( $data['terms'] ) ) { + $errors->add( + 'libresign_policy_consent', + __( 'You must agree to the policies before completing the purchase.', 'libresign' ) + ); + } +} +add_action( 'woocommerce_after_checkout_validation', 'libresign_theme_validate_checkout_policy_consent', 10, 2 ); + /** * Link the checkout terms text to the policy. */ diff --git a/tests/Integration/Inc/CheckoutTest.php b/tests/Integration/Inc/CheckoutTest.php index 7b78ba8..e09e1f1 100644 --- a/tests/Integration/Inc/CheckoutTest.php +++ b/tests/Integration/Inc/CheckoutTest.php @@ -4,6 +4,7 @@ use Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFields; use Automattic\WooCommerce\Blocks\Package; +use WP_Error; use WP_UnitTestCase; final class CheckoutTest extends WP_UnitTestCase { @@ -22,6 +23,22 @@ public function test_requires_the_policy_consent_to_place_an_order() { $this->assertSame( 'You must agree to the policies before completing the purchase.', $field['errorMessage'] ); } + public function test_the_classic_checkout_refuses_an_order_without_the_consent() { + $errors = new WP_Error(); + + do_action( 'woocommerce_after_checkout_validation', array( 'terms' => 0 ), $errors ); + + $this->assertContains( 'libresign_policy_consent', $errors->get_error_codes() ); + } + + public function test_the_classic_checkout_accepts_an_order_with_the_consent() { + $errors = new WP_Error(); + + do_action( 'woocommerce_after_checkout_validation', array( 'terms' => 1 ), $errors ); + + $this->assertNotContains( 'libresign_policy_consent', $errors->get_error_codes() ); + } + /** * @dataProvider provide_terms_blocks */