Last active
February 12, 2024 09:22
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ) ); | |
} |
Hi there, is this compatible also with WooCommerce with the Subscription extension?
@justcalljarold Yes, it is compatible with WooCommerce, including the Subscription extension.
@justcalljarold Yes, it is compatible with WooCommerce, including the Subscription extension.
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
How To Add PHP Code Snippets To WordPress
Directly to your theme – you can place the code snippet directly in a file called functions.php in a child theme
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.