Save WooCommerce Terms and Conditions Acceptance in the Database

On May 25, 2018, the General Data Protection Regulation (GDPR) came into effect. This law aims to provide greater security and control for individuals over their personal information, as well as establish common data protection rules across Europe. Among many other things, anyone managing private data must be able to demonstrate that the customer/user has given permission to handle their personal data.

What does WooCommerce do by default?

WooCommerce makes it easy to make sure that users cannot make a purchase unless they accept the terms and conditions. You simply need to create a page with your store’s terms and conditions and add it to WooCommerce > Settings > Advanced > Page setup.

And with this, just before making a payment, you will see this on the checkout page:

Great! But that only forces the user to check the box. Legally, you won’t be able to prove that the user actually checked the box because WooCommerce does not store this acceptance anywhere.

How to save the Terms and Conditions agreement in the databaseDatabase A database is a structured system for storing and managing information. In the context of WordPress, the database stores all site data, such as content, settings, and users. WordPress typically uses MySQL or MariaDB.?

What we’re going to do is add a small code snippet so that we can store this acceptance. To do this, open your functions.php file in WordPress, located in wp-content/themes/your_theme/functions.php.

Once inside the file, add the following code snippet:

// Store terms and conditions in the database
add_action('woocommerce_checkout_update_order_meta', 'woo_save_terms_and_conditions_status');
function woo_save_terms_and_conditions_status( $order_id ) {
  if ($_POST['terms']) update_post_meta( $order_id, '_terms', esc_attr($_POST['terms']));
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'woo_display_terms_and_conditions_status', 10, 1 );
function woo_display_terms_and_conditions_status($order){
  $terms = get_post_meta( $order->id, '_terms', true );
  $terms_status = ( $terms == 'on' ? __('Accepted') : __('undefined') );
  echo '<p><strong>'.__('Terms and Conditions').':</strong> ' . $terms_status . '</p>';
}

And that’s it! Now, every time someone makes a purchase, we will see the following in the order details, as the acceptance is now stored in our database:


Source: Remi’s blog

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

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