Last updated on October 18, 2023

WooCommerce GDPR compliance

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Ensure GDPR compliance for your WooCommerce store.

To achieve WooCommerce GDPR compliance, you need to ensure that your website is handling customer data in a way that aligns with the General Data Protection Regulation (GDPR) requirements. This includes obtaining explicit consent for data collection, providing data access and deletion options, and implementing data protection measures.

One important aspect of GDPR compliance is obtaining explicit consent from users before collecting their personal data. You can achieve this by adding a checkbox to your WooCommerce registration and checkout forms. Here’s an example of how you can add a consent checkbox to the WooCommerce registration form:

/**
 * Add GDPR consent checkbox to WooCommerce registration form.
 */
function wpsnippets_add_gdpr_consent_checkbox() {
    ?>
    <p class="form-row">
        <label for="gdpr_consent" class="woocommerce-form__label woocommerce-form__label-for-checkbox">
            <?php esc_html_e( 'I consent to the collection and storage of my personal data.', 'your-text-domain' ); ?>
            <span class="required">*</span>
        </label>
        <input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox" name="gdpr_consent" id="gdpr_consent" required>
    </p>
    <?php
}
add_action( 'woocommerce_register_form', 'wpsnippets_add_gdpr_consent_checkbox', 10 );

/**
 * Validate GDPR consent checkbox on WooCommerce registration form submission.
 */
function wpsnippets_validate_gdpr_consent_checkbox( $errors ) {
    if ( ! isset( $_POST['gdpr_consent'] ) ) {
        $errors->add( 'gdpr_consent_error', __( 'Please give your consent to proceed.', 'your-text-domain' ) );
    }
    return $errors;
}
add_filter( 'woocommerce_registration_errors', 'wpsnippets_validate_gdpr_consent_checkbox', 10, 1 );

In the code snippet above, we first add a consent checkbox to the WooCommerce registration form using the woocommerce_register_form action hook. The checkbox is required and labeled with a message indicating the consent requirement.

Next, we validate the consent checkbox on form submission using the woocommerce_registration_errors filter hook. If the checkbox is not checked, an error message is added to the form submission errors.

By implementing this code snippet, you ensure that users explicitly consent to the collection and storage of their personal data during the registration process, thus complying with GDPR requirements.

Examples

Example 1: Adding a GDPR checkbox to the WooCommerce registration form

This example demonstrates how to add a GDPR checkbox to the WooCommerce registration form, ensuring compliance with GDPR regulations. The code snippet below adds a checkbox to the registration form and saves the user’s consent in the user meta data.

/**
 * Add GDPR checkbox to WooCommerce registration form.
 */
function wpsnippets_add_gdpr_checkbox() {
    woocommerce_form_field( 'gdpr_consent', array(
        'type'          => 'checkbox',
        'class'         => array('form-row privacy'),
        'label_class'   => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
        'input_class'   => array('woocommerce-form__input woocommerce-form__input-checkbox'),
        'required'      => true,
        'label'         => 'I consent to the storage and handling of my data by this website.',
    ), get_user_meta( get_current_user_id(), 'gdpr_consent', true ) );
}
add_action( 'woocommerce_register_form', 'wpsnippets_add_gdpr_checkbox', 10 );

/**
 * Save GDPR consent on WooCommerce registration.
 */
function wpsnippets_save_gdpr_consent( $customer_id ) {
    if ( isset( $_POST['gdpr_consent'] ) ) {
        update_user_meta( $customer_id, 'gdpr_consent', 'yes' );
    } else {
        update_user_meta( $customer_id, 'gdpr_consent', 'no' );
    }
}
add_action( 'woocommerce_created_customer', 'wpsnippets_save_gdpr_consent' );

The wpsnippets_add_gdpr_checkbox function adds a checkbox field to the WooCommerce registration form, with a label and required attribute. It also retrieves the user’s consent from the user meta data.

The wpsnippets_save_gdpr_consent function saves the user’s consent as a user meta data when a new customer is registered. It checks if the checkbox is checked and updates the user meta accordingly.

Example 2: Displaying GDPR consent on WooCommerce order details page

This example demonstrates how to display the user’s GDPR consent on the WooCommerce order details page. The code snippet below retrieves the consent value from the user meta data and displays it on the order details page.

/**
 * Display GDPR consent on WooCommerce order details page.
 */
function wpsnippets_display_gdpr_consent_on_order_details( $order_id ) {
    $user_id = get_post_meta( $order_id, '_customer_user', true );
    $gdpr_consent = get_user_meta( $user_id, 'gdpr_consent', true );

    if ( $gdpr_consent === 'yes' ) {
        echo '<p><strong>GDPR Consent:</strong> User has consented to data storage and handling.</p>';
    } else {
        echo '<p><strong>GDPR Consent:</strong> User has not consented to data storage and handling.</p>';
    }
}
add_action( 'woocommerce_order_details_after_order_table', 'wpsnippets_display_gdpr_consent_on_order_details' );

The wpsnippets_display_gdpr_consent_on_order_details function retrieves the user ID associated with the order, then retrieves the GDPR consent value from the user meta data. Based on the consent value, it displays a message indicating whether the user has consented to data storage and handling.

Example 3: Exporting WooCommerce customer data with GDPR compliance

This example demonstrates how to export customer data from WooCommerce while ensuring GDPR compliance. The code snippet below adds customer data fields to the exported CSV file, including the GDPR consent status.

/**
 * Add GDPR consent column to WooCommerce customer export.
 */
function wpsnippets_add_gdpr_consent_column( $columns ) {
    $columns['gdpr_consent'] = 'GDPR Consent';
    return $columns;
}
add_filter( 'woocommerce_customer_export_csv_columns', 'wpsnippets_add_gdpr_consent_column' );

/**
 * Populate GDPR consent column in WooCommerce customer export.
 */
function wpsnippets_populate_gdpr_consent_column( $column_name, $customer ) {
    if ( 'gdpr_consent' === $column_name ) {
        $user_id = $customer->get_user_id();
        $gdpr_consent = get_user_meta( $user_id, 'gdpr_consent', true );
        echo $gdpr_consent === 'yes' ? 'Consented' : 'Not Consented';
    }
}
add_action( 'woocommerce_customer_export_csv_column_gdpr_consent', 'wpsnippets_populate_gdpr_consent_column', 10, 2 );

The wpsnippets_add_gdpr_consent_column function adds a new column for GDPR consent to the WooCommerce customer export CSV file.

The wpsnippets_populate_gdpr_consent_column function populates the GDPR consent column with the consent status for each customer. It retrieves the user ID associated with the customer, retrieves the GDPR consent value from the user meta data, and displays the corresponding status in the CSV file.

Last updated on October 18, 2023. Originally posted on November 28, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *