Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diegomox/f3df8fa17fe8f330403d7369e6968237 to your computer and use it in GitHub Desktop.
Save diegomox/f3df8fa17fe8f330403d7369e6968237 to your computer and use it in GitHub Desktop.
Allows to save in the Woocommerce order a proof that the terms and conditions were accepted by the customer.
<?php
/**
* @snippet WooCommerce: Save “Terms & Conditions” Acceptance @ Checkout
* @author Diego González - diegomox
* @compatible WooCommerce 8
* @source This code is adapted from: https://www.businessbloomer.com/woocommerce-save-terms-conditions-user-acceptance-checkout
*/
use Automattic\WooCommerce\Utilities\OrderUtil;
// 1. Save Terms and Conditions as Order Meta
add_action('woocommerce_checkout_update_order_meta', 'dgm_save_terms_conditions_acceptance', 10, 2);
function dgm_save_terms_conditions_acceptance( $order_id, $data ) {
$order = $order_id ? wc_get_order( $order_id ) : null;
if ( $order && isset( $data['terms'] ) ) {
if ( class_exists( 'Automattic\WooCommerce\Utilities\OrderUtil' ) && OrderUtil::custom_orders_table_usage_is_enabled() ) {
$order->update_meta_data( '_terms_accepted', (int) $data['terms'] );
$order->save();
} else {
update_post_meta( $order_id, '_terms_accepted', (int) $data['terms'] );
}
}
}
// 2. Display Terms and Conditions @ Single Order Page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'dgm_display_terms_conditions_acceptance' );
function dgm_display_terms_conditions_acceptance( $order ) {
if ( class_exists( 'Automattic\WooCommerce\Utilities\OrderUtil' ) && OrderUtil::custom_orders_table_usage_is_enabled() ) {
$acceptance = $order->get_meta( '_terms_accepted', true );
} else {
$acceptance = get_post_meta( $order->get_id(), '_terms_accepted', true );
}
echo '<p><strong>' . __( 'Terms & Conditions: ', 'your_text_domain' ) . '</strong>' . ( $acceptance? __( 'accepted', 'your_text_domain' ) : __( 'N/A', 'your_text_domain' ) );
}
@diegomox
Copy link
Author

How To Add PHP Code Snippets To WordPress

  1. Directly to your theme – you can place the code snippet directly in a file called functions.php in a child theme

  2. You can use a ready-made plugin of the several that exist and allow you to paste PHP snippets. One of the most popular plugins you can use is Code Snippets.

@justcalljarold
Copy link

Hi there, is this compatible also with WooCommerce with the Subscription extension?

@diegomox
Copy link
Author

diegomox commented Feb 11, 2024

@justcalljarold Yes, it is compatible with WooCommerce, including the Subscription extension.

tempsnip

@justcalljarold
Copy link

@justcalljarold Yes, it is compatible with WooCommerce, including the Subscription extension.

tempsnip

ey thanks for the confirmation!

I've also been looking to the site where you got this snippet. they mostly have everything in woocommerce but not specifically woocommerce with subscription extension added so I just wanted to make sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment