Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
527 views
in Technique[技术] by (71.8m points)

php - European GDPR additional checkout validation checkbox in Woocommerce

Hi I have been trying to add an additional Conditional Check Box to my Woocommerce checkout page, the same as the Terms and Conditions one but with information about the new GDPR (data protection) and a link to my Privacy Policy. They must tick the box before they can check out.

I have been using various snippets of code put together from what I have found here and have managed to add a conditional check box and some text but have not been able to add a link to the Privacy Policy. I have to add it below the Checkout Box. Ideally I would like to have the link alongside the check box like it appears on the Terms and Conditions one.

Any help to get the Privacy page link within the checkout box would be appreciated.

Conditional Checkbox Privacy policy Link screenshot:

Conditional Checkbox Privacy policy Link

Here's what I have so far:

//Here is the function for adding the checkbox:
function cw_custom_checkbox_fields( $checkout ) {

    echo '<div class="cw_custom_class"><h3>'.__('Give Sepration Heading: ').'</h3>';

    woocommerce_form_field( 'custom_checkbox', array(
        'type'          => 'checkbox',
        'label'         => __('Agreegation Policy.'),
        'required'  => true,
    ), $checkout->get_value( 'custom_checkbox' ));

    echo '</div>';
}
add_action('woocommerce_checkout_after_terms_and_conditions', 'checkout_additional_checkboxes');
function checkout_additional_checkboxes( ){
    $checkbox1_text = __( "I have read and accept the Privacy Policy and understand how you manage my Data under GDPR", "woocommerce" );
       ?>
    <p class="form-row custom-checkboxes">
        <label class="woocommerce-form__label checkbox custom-one">
            <input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="custom_one" > <span><?php echo  $checkbox1_text; ?></span> <span class="required">*</span>
        </label>
            </p>
    <?php
}

add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
    // Check if set, if its not set add an error.
    if ( ! $_POST['custom_one'] )
        wc_add_notice( __( 'You must accept "I have read and accept the Privacy Policy and understand how you manage my Data under GDPR".' ), 'error' );
  }




add_filter('comment_form_defaults','isa_comment_reform');
add_action( 'woocommerce_after_checkout_form', 'wnd_checkout_message_bottom', 10 );

function wnd_checkout_message_bottom( ) {
 echo '<div class="wnd-checkout-message"><h3> <a href="http://127.0.0.1/protest/privacy-policy/" target="_blank">Privacy Policy</a><br/></h3>
</div>';
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Update (May 2018 - Code enhanced)

The new version of woocommerce 3.4 handle now GDPR

The following code, will add an additional validation checkbox for the new mandatory european GDPR Privacy Policy, under terms and conditions, in checkout page:

// Add terms and policy check box in checkout page
add_action( 'woocommerce_checkout_after_terms_and_conditions', 'add_terms_and_policy', 20 );
function add_terms_and_policy() {
    $domain = 'woocommerce';

    $gdpr_private_policy_link = sprintf( '<a href="%s" target="_blank">%s</a>',
        home_url("/protest/privacy-policy/"), // The button link to the GDPR privacy policy page
        __( "Privacy Policy", $domain )       // The button text
    );

    woocommerce_form_field( 'gdpr_terms', array(
        'type'          => 'checkbox',
        'class'         => array( 'terms gdpr_terms' ),
        'input_class'   => array('woocommerce-form__input-checkbox'),
        'label_class'   => array('woocommerce-form__label-for-checkbox'),
        'label'         => '<span>' . sprintf(
            __( "I have read and accept the %s and understand how you manage my Data under GDPR", $domain ),
            $gdpr_private_policy_link
        ) . '</span>',
        'required'      => true,
    ), '');
}

// Validate required GDPR private policy checkbox
add_action( 'woocommerce_after_checkout_validation', 'terms_and_policy_validation', 20, 2 );
function terms_and_policy_validation( $data, $errors ) {
    if ( ! isset( $_POST['gdpr_terms'] ) ){
        $domain = 'woocommerce';

        $gdpr_text = sprintf(
            __( "I have read and accept the %s and understand how you manage my Data under GDPR", $domain ),
            __( "Privacy Policy", $domain )
        );

        $errors->add( 'gdpr_terms', sprintf( __( 'You must accept "%s".', $domain ), $gdpr_text ), 'error' );
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and work.

enter image description here

If not checked, the customer will get this error message avoiding checkout:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...