Okay
  Public Ticket #3685200
Checkbox for disclaimer form
Closed

Comments

  •  44
    Anthony O Connell started the conversation

    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,218
    WebWizards replied

    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

  •  44
    Anthony O Connell replied

    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,218
    WebWizards replied

    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.


  •  44
    Anthony O Connell replied

    That's perfect.

    Appreciate that 😊 

  •  44
    Anthony O Connell replied

    Hi guys.

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

    Thanks 

  •  2,218
    WebWizards replied

    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).

  •  44
    Anthony O Connell replied

    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,218
    WebWizards replied

    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
  •  44
    Anthony O Connell replied

    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:  create an account.png

  •  2,218
    WebWizards replied

    Sure, I've added a snippet now to do that! It should now be ticket by default.

  •  44
    Anthony O Connell replied

    That's great.

    Thanks very much 🙏