Public Ticket #3685200

Checkbox for disclaimer form

Closed
Comments
56 Tickets 59
Anthony O Connell started the conversation 2 years ago on July 12, 2024 at 10:51pm

Hi guys.

I want to be able to add a disclaimer to certain products. Is it possible to add a checkbox at checkout for a disclaimer that customers need to check before they can checkout. This would be very useful for some of my vendors.

Thanks.

2,762
KingsPlugins replied 2 years ago on July 13, 2024 at 10:35pm

Hi Anthony,

In general, we can add a code snippet that adds a disclaimer checkbox to the checkout, making it impossible to checkout without accepting that.

You can add this code snippet (And edit the text / titles):

// Add custom field to checkout
add_action('woocommerce_after_order_notes', 'disclaimer_checkbox');
function disclaimer_checkbox($checkout) {
    echo '<div id="disclaimer_checkbox"><h2>' . __('Important Disclaimer') . '</h2>';
    woocommerce_form_field('disclaimer', array(
        'type'          => 'checkbox',
        'required'        => 'required',
        'class'         => array('form-row-wide'),
        'label'         => __('Do you acept the terms and conditions?'),
    ), $checkout->get_value('disclaimer'));
    echo '</div>';
}
// Validate custom field
add_action('woocommerce_checkout_process', 'disclaimer_checkbox_process');
function disclaimer_checkbox_process() {
    // Check if set, if not then display notice.
    if (!$_POST['disclaimer']){
        wc_add_notice(__('Please let us know if you accept the disclaimer.'), 'error');
    }
}
// Update the order meta with custom field value
add_action('woocommerce_checkout_update_order_meta', 'disclaimer_checkbox_update_order_meta');
function disclaimer_checkbox_update_order_meta($order_id) {
    if ($_POST['disclaimer']) {
        update_post_meta($order_id, 'Disclaimer Accepted', esc_attr($_POST['disclaimer']));
    }
}

It would result in a checkbox added to checkout as follows:

https://www.loom.com/share/f5ab3adbeff4406fb170c5c4b9b9044f?sid=a28a7893-3593-46e3-8bd1-6546edea06c5


I do not really have a way though to only apply it to certain products - it would just apply in general to all checkouts.


Kind regards,

Stefan

1
56 Tickets 59
Anthony O Connell replied 2 years ago on July 14, 2024 at 8:54pm

Hi Stefan.

That's excellent. 

That works brilliantly. 

Can I ask where exactly in the snippet can I edit it?

Thanks so much.

This will be a great little feature.

2,762
KingsPlugins replied 2 years ago on July 15, 2024 at 9:04pm

Surely, there are basically 3 areas in that snippet that can be changed:

1762557756.png

Based on the display here:

9596185931.png

-> The first area, saying "Important Disclaimer" will modify that title

-> The 2nd, saying "Do you accept the terms and conditions?" will modify the text near the checkbox

-> The last one controls the text displayed in case the user did not check the checkbox.


1
56 Tickets 59
Anthony O Connell replied 2 years ago on August 4, 2024 at 2:17pm

Hi guys.

Is their a way I can make the below disclaimer only visible to a certain product via the product ID?

Thanks 

2,762
KingsPlugins replied 2 years ago on August 5, 2024 at 2:48pm

Hi again,

Would that mean to only show the disclaimer if a specific product is in the cart or not? (or do you mean something else e.g. to show the disclaimer on the product page).

1
56 Tickets 59
Anthony O Connell replied 2 years ago on August 5, 2024 at 3:16pm

Yes. To only show it on a specific product when it is in the cart.

I am able to add the disclaimer in the product info so that is fine and doesn't need attention.

Thanks

2,762
KingsPlugins replied 2 years ago on August 5, 2024 at 11:02pm

Got it, I think you can do it with this updated snippet:

// Add custom field to checkout
add_action('woocommerce_after_order_notes', 'disclaimer_checkbox');
function disclaimer_checkbox($checkout) {
    $product_ids_to_check = array(123, 456, 789); // Replace with your product IDs
    if (is_product_in_cart($product_ids_to_check)) {
        echo '<div id="disclaimer_checkbox"><h2>' . __('Important Disclaimer') . '</h2>';
        woocommerce_form_field('disclaimer', array(
            'type'          => 'checkbox',
            'required'      => 'required',
            'class'         => array('form-row-wide'),
            'label'         => __('Do you accept the terms and conditions?'),
        ), $checkout->get_value('disclaimer'));
        echo '</div>';
    }
}
function is_product_in_cart($product_ids) {
    $cart = WC()->cart->get_cart();
    foreach ($cart as $cart_item) {
        if (in_array($cart_item['product_id'], $product_ids)) {
            return true;
        }
    }
    return false;
}
// Validate custom field
add_action('woocommerce_checkout_process', 'disclaimer_checkbox_process');
function disclaimer_checkbox_process() {
    $product_ids_to_check = array(123, 456, 789); // Replace with your product IDs
    if (is_product_in_cart($product_ids_to_check)) {
        // Check if set, if not then display notice.
        if (!isset($_POST['disclaimer'])) {
            wc_add_notice(__('Please let us know if you accept the disclaimer.'), 'error');
        }
    }
}
// Update the order meta with custom field value
add_action('woocommerce_checkout_update_order_meta', 'disclaimer_checkbox_update_order_meta');
function disclaimer_checkbox_update_order_meta($order_id) {
    if (isset($_POST['disclaimer'])) {
        update_post_meta($order_id, 'Disclaimer Accepted', esc_attr($_POST['disclaimer']));
    }
}

It's needed to change this in 2 places with the product ID or IDs:

$product_ids_to_check = array(123, 456, 789); // Replace with your product IDs
1
56 Tickets 59
Anthony O Connell replied 2 years ago on August 6, 2024 at 12:11pm

That works perfect.

This will work great for vendors who need to input a disclaimer for certain events they publish. 

Thank you.

One quick one.

Please see checkout page attached.

Is it possible to have the 'Create an account' already ticked like the newsletter.

I can't seem to see how to do it.

Thanks.

Attached files:

1

Sign In