I've got a client who needs a checkbox for "accepting the terms and conditions etc." The tricky thing is that he will be adding in-person purchases on the admin panel and needs the checkbox there as well. I got the checkbox on both the front and back-end but I'm having some trouble getting them to talk to each other. When placing an order on the front-end, it shows as admin-panel-pic
But when you click into edit the order, the checkbox in not checked and if you update the order at all the value will disappear. I'm sure I'm missing something obvious but this is the first time I've had to modify Woocommerce fields like this.
Here's the code pertaining to the checkbox I have currently in my functions.php:
<?php
add_action('woocommerce_before_order_notes', 'wps_add_select_checkout_field');
function wps_add_select_checkout_field( $checkout ) {
woocommerce_form_field( 'rem_terms', array(
'type' => 'checkbox',
'class' => array('form-row mycheckbox'),
'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox
checkbox'),
'input_class' => array('woocommerce-form__input woocommerce-form__input-checkbox input-
checkbox'),
'required' => true,
'label' => 'I agree with the terms and conditions described in the <a
href="/privacy-policy" target="_blank" rel="noopener">Privacy Policy</a> and the <a
target="_blank" rel="noopener" href="/liability-release-and-express-assumption-of-risk">
Liability Release </a>',) // Label and Link
);
$checkout->get_value( 'rem_terms' );
}
add_action('woocommerce_checkout_update_order_meta',
'wps_select_checkout_field_update_order_meta');
add_action( 'woocommerce_checkout_process', 'bt_add_checkout_checkbox_warning' );
/**
* Alert if checkbox not checked
*/
function bt_add_checkout_checkbox_warning() {
if ( ! (int) isset( $_POST['rem_terms'] ) ) {
wc_add_notice( __( 'Please agree to our terms and conditions before placing your order' ),
'error' );
}
}
function wps_select_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['rem_terms'] ) ) {
update_post_meta( $order_id, 'rem_terms', sanitize_text_field( $_POST['rem_terms'] ) );
}
}
add_action( 'woocommerce_admin_order_data_after_order_details',
'misha_editable_order_meta_general' );
function misha_editable_order_meta_general( $order ){ ?>
<br class="clear" />
<h4>Checkbox</h4>
<?php
/*
* get all the meta data values we need
*/
$rem_terms = get_post_meta( $order->get_id(), 'rem_terms', true );
?>
<div class="address">
<p><strong>Customer accepts REM's terms and conditions described in the
privacy policy</strong><?php echo $rem_terms?></p>
</div>
<div class="edit_address"><?php
woocommerce_wp_checkbox( array(
'id' => 'rem_terms',
'label' => 'Customer Agrees to terms and conditions',
'value' => $rem_terms,
'wrapper_class' => 'form-field-wide'
) );
?></div>
<?php }
add_action( 'woocommerce_process_shop_order_meta', 'save_new_fields' );
function save_new_fields( $ord_id ){
update_post_meta( $ord_id, 'rem_terms', wc_clean( $_POST[ 'rem_terms' ] ) );
}
function example_custom_order_fields( $fields, $order ) {
$new_fields = array();
if( get_post_meta( $order->id, 'rem_terms', true ) ) {
$new_fields['rem_terms'] = array(
'label' => 'Cust Accepts Terms',
'value' => get_post_meta( $order->id, 'rem_terms', true )
);
}
return array_merge( $fields, $new_fields );
}
add_filter( 'wcdn_order_info_fields', 'example_custom_order_fields', 10, 2 );