Skip to content

Commit

Permalink
Fixed WooPay custom terms and terms checkbox rules
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardola committed Jan 10, 2025
1 parent 8f992f1 commit 4613b4d
Showing 1 changed file with 56 additions and 34 deletions.
90 changes: 56 additions & 34 deletions includes/woopay/class-woopay-session.php
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ private static function create_woopay_nonce( int $uid ) {
*
* @return string The custom message with the placeholders replaced.
*/
private static function get_formatted_custom_message() {
private static function get_formatted_custom_terms() {
$custom_message = WC_Payments::get_gateway()->get_option( 'platform_checkout_custom_message' );

$terms_value = wc_terms_and_conditions_page_id() ?
Expand All @@ -898,72 +898,94 @@ private static function get_formatted_custom_message() {
*/
private static function get_option_fields_status() {
// Shortcode checkout options.
$company = get_option( 'woocommerce_checkout_company_field', 'optional' );
$address_2 = get_option( 'woocommerce_checkout_address_2_field', 'optional' );
$phone = get_option( 'woocommerce_checkout_phone_field', 'required' );
$terms_checkbox = ! empty( get_option( 'woocommerce_terms_page_id', null ) );
$terms_text = wp_kses_post( wc_replace_policy_page_link_placeholders( wc_get_terms_and_conditions_checkbox_text() ) );
$company = get_option( 'woocommerce_checkout_company_field', 'optional' );
$address_2 = get_option( 'woocommerce_checkout_address_2_field', 'optional' );
$phone = get_option( 'woocommerce_checkout_phone_field', 'required' );
$has_terms_and_condition_page = ! empty( get_option( 'woocommerce_terms_page_id', null ) );
$terms_and_conditions = get_option( 'woocommerce_checkout_terms_and_conditions_checkbox_text', '' );
$has_privacy_policy_page = ! empty( get_option( 'wp_page_for_privacy_policy', null ) );
$custom_below_place_order_button_text = self::get_formatted_custom_terms();
$below_place_order_button_text = $custom_below_place_order_button_text;
$show_terms_checkbox = false;

// Blocks checkout options. To get the blocks checkout options, we need
// to parse the checkout page content because the options are stored
// in the blocks HTML as a JSON.
$checkout_page_id = get_option( 'woocommerce_checkout_page_id' );
$checkout_page = get_post( $checkout_page_id );

/*
* Will show the terms checkbox if the terms page is set.
* Will show the checkbox even when the text is loaded from the custom field or the policy page field.
*/
if ( $has_terms_and_condition_page && $terms_and_conditions ) {
$show_terms_checkbox = true;
if ( ! $below_place_order_button_text ) {
$below_place_order_button_text = $terms_and_conditions;
}
}

if ( ! $below_place_order_button_text && $has_privacy_policy_page ) {
$show_terms_checkbox = false;
$below_place_order_button_text = get_option( 'woocommerce_checkout_privacy_policy_text', '' );
}

if ( empty( $checkout_page ) ) {
return [
'company' => $company,
'address_2' => $address_2,
'phone' => $phone,
'terms_checkbox' => $terms_checkbox,
'terms_text' => $terms_text,
'terms_checkbox' => $show_terms_checkbox,
'custom_terms' => $below_place_order_button_text,
];
}

$checkout_page_blocks = parse_blocks( $checkout_page->post_content );
$checkout_block_index = array_search( 'woocommerce/checkout', array_column( $checkout_page_blocks, 'blockName' ), true );

// If we can find the index, it means the merchant checkout page is using blocks checkout.
if ( false !== $checkout_block_index && ! empty( $checkout_page_blocks[ $checkout_block_index ]['attrs'] ) ) {
$checkout_block_attrs = $checkout_page_blocks[ $checkout_block_index ]['attrs'];
if ( false !== $checkout_block_index ) {
$below_place_order_button_text = $custom_below_place_order_button_text;
$company = 'optional';
$address_2 = 'optional';
$phone = 'optional';

$company = 'optional';
$address_2 = 'optional';
$phone = 'optional';
if ( ! empty( $checkout_page_blocks[ $checkout_block_index ]['attrs'] ) ) {
$checkout_block_attrs = $checkout_page_blocks[ $checkout_block_index ]['attrs'];

if ( ! empty( $checkout_block_attrs['requireCompanyField'] ) ) {
$company = 'required';
}
if ( ! empty( $checkout_block_attrs['requireCompanyField'] ) ) {
$company = 'required';
}

if ( ! empty( $checkout_block_attrs['requirePhoneField'] ) ) {
$phone = 'required';
}
if ( ! empty( $checkout_block_attrs['requirePhoneField'] ) ) {
$phone = 'required';
}

// showCompanyField is undefined by default.
if ( empty( $checkout_block_attrs['showCompanyField'] ) ) {
$company = 'hidden';
}
// showCompanyField is undefined by default.
if ( empty( $checkout_block_attrs['showCompanyField'] ) ) {
$company = 'hidden';
}

if ( isset( $checkout_block_attrs['showApartmentField'] ) && false === $checkout_block_attrs['showApartmentField'] ) {
$address_2 = 'hidden';
}
if ( isset( $checkout_block_attrs['showApartmentField'] ) && false === $checkout_block_attrs['showApartmentField'] ) {
$address_2 = 'hidden';
}

if ( isset( $checkout_block_attrs['showPhoneField'] ) && false === $checkout_block_attrs['showPhoneField'] ) {
$phone = 'hidden';
if ( isset( $checkout_block_attrs['showPhoneField'] ) && false === $checkout_block_attrs['showPhoneField'] ) {
$phone = 'hidden';
}
}

$fields_block = self::get_inner_block( $checkout_page_blocks[ $checkout_block_index ], 'woocommerce/checkout-fields-block' );
$terms_block = self::get_inner_block( $fields_block, 'woocommerce/checkout-terms-block' );
$terms_checkbox = isset( $terms_block['attrs']['checkbox'] ) && $terms_block['attrs']['checkbox'];
$terms_text = $terms_block['attrs']['text'] ?? null;
$fields_block = self::get_inner_block( $checkout_page_blocks[ $checkout_block_index ], 'woocommerce/checkout-fields-block' );
$terms_block = self::get_inner_block( $fields_block, 'woocommerce/checkout-terms-block' );
$show_terms_checkbox = isset( $terms_block['attrs']['checkbox'] ) && $terms_block['attrs']['checkbox'];
}

return [
'company' => $company,
'address_2' => $address_2,
'phone' => $phone,
'terms_checkbox' => $terms_checkbox,
'terms_text' => $terms_text,
'terms_checkbox' => $show_terms_checkbox,
'custom_terms' => $below_place_order_button_text,
];
}

Expand Down

0 comments on commit 4613b4d

Please sign in to comment.