Alert for a conditional checkbox in check out
-
Hi
I’m trying to create a checkbox in the check out which is displayed based on one or more sku’s being present in the basket. Basically I need the user to verify they have read a guide, before ordering custom size products.I came across this code which seems to work, however there is a problem with the alert part as the customer is not able to proceed to payment, if the checkbox is not visible (none of the defined sku’s are in the basket)
So how do I make the alert optional in terms of the checkbox being visible or not? I only want it shown, if the check box is visible and unchecked.
Appreciate any help ??
add_action( 'woocommerce_review_order_before_submit', 'bt_add_checkout_checkbox', 10 ); /** * Add WooCommerce additional Checkbox checkout field */ function bt_add_checkout_checkbox() { //Check if wooCommerce is activated if ( class_exists( 'WooCommerce' ) ) { //Define SKUs you want to check for $checkSKUs = ['sku1', 'sku2', 'sku3']; //Grab all the SKUs in cart $skus = array(); foreach( WC()->cart->get_cart() as $cart_item ) { array_push($skus, $cart_item['data']->get_sku()); } //Check if anything matches in both $matchingResult = array_intersect($checkSKUs,$skus); if (count($matchingResult) > 0) { //If at least 1 SKU matches then generate checkout field woocommerce_form_field( 'checkout_checkbox', array( // CSS ID 'type' => 'checkbox', 'class' => array('form-row mycheckbox'), // CSS Class '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, // Mandatory or Optional 'label' => 'I acknowledge that my product can take up to 24 hours to be delivered. <a href="/checkout-checkbox" target="_blank" rel="noopener">(Unless the description says otherwise)</a>', // Label and Link )); } } } 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['checkout_checkbox'] ) ) { wc_add_notice( __( 'Please acknowledge the Checkbox' ), 'error' ); } }
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- You must be logged in to reply to this topic.