最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Woocommerce - Checkmark validation not working for proceed_to_checkout button - Stack Overflow

programmeradmin1浏览0评论

I am new to developing with Woocommerce. Currently, I have a customer disclaimer message with a checkbox that if not clicked, will display a popup to check the box before proceeding to cart. However, if I click on the proceed_to_checkout button, it goes onto the next page, regardless of whether or not if someone clicked on the checkbox. Any help would be greatly appreciated:

add_action( 'woocommerce_proceed_to_checkout', 'ICG70_Rover', 10 );

function ICG70_Rover() {

    //Check if wooCommerce is activated
    if ( class_exists( 'WooCommerce' ) ) {
  
        //Define SKUs you want to check for
        $checkSKUs = ['3'];

        //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_checkboxTEST', 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 have read and understood ICG70 Rover disclaimer',  // Label and Link
            ));
        } 

    }   
}

My hook that is supposed to display error if checkbox not clicked when clicking procced_to_checkout button:

// Show notice if customer does not tick
add_action( 'woocommerce_proceed_to_checkout', 'verify_form' );
   
function verify_form() {
    if ( ! (int) isset( $_POST['checkout_checkboxTEST'] ) ) {
        wc_add_notice( __( 'Please acknowledge the disclaimer' ), 'error' );
    }
}

I am new to developing with Woocommerce. Currently, I have a customer disclaimer message with a checkbox that if not clicked, will display a popup to check the box before proceeding to cart. However, if I click on the proceed_to_checkout button, it goes onto the next page, regardless of whether or not if someone clicked on the checkbox. Any help would be greatly appreciated:

add_action( 'woocommerce_proceed_to_checkout', 'ICG70_Rover', 10 );

function ICG70_Rover() {

    //Check if wooCommerce is activated
    if ( class_exists( 'WooCommerce' ) ) {
  
        //Define SKUs you want to check for
        $checkSKUs = ['3'];

        //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_checkboxTEST', 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 have read and understood ICG70 Rover disclaimer',  // Label and Link
            ));
        } 

    }   
}

My hook that is supposed to display error if checkbox not clicked when clicking procced_to_checkout button:

// Show notice if customer does not tick
add_action( 'woocommerce_proceed_to_checkout', 'verify_form' );
   
function verify_form() {
    if ( ! (int) isset( $_POST['checkout_checkboxTEST'] ) ) {
        wc_add_notice( __( 'Please acknowledge the disclaimer' ), 'error' );
    }
}
Share Improve this question edited Nov 16, 2024 at 11:42 LoicTheAztec 255k24 gold badges399 silver badges446 bronze badges asked Nov 16, 2024 at 2:25 JohnLyonsJohnLyons 331 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

This requires a bit of JavaScript/jQuery code to make it work as you are expecting.

Note that if ( class_exists( 'WooCommerce' ) ) { is not useful inside a WooCommerce hooked function, and should always be outside.

Try the following:

// Check if wooCommerce is activated
if ( class_exists( 'WooCommerce' ) ) {
    // Utility function: Get cart items SKUs array
    function cart_items_skus( $skus = array() ) {
        // Loop through cart items
        foreach( WC()->cart->get_cart() as $item ) {
            $skus[] = $item['data']->get_sku();
        }
        return $skus;
    }

    // Display a disclaimer checkbox on Cart Page, if any of the defined products is in cart
    add_action('woocommerce_proceed_to_checkout', 'cart_disclaimer1_test', 10);
    function cart_disclaimer1_test() {
        // Here, define the targeted product IDs
        $targeted_SKUs = array('1254xxc', '136cfbf', 'woo-beanie');

        // Check if any of the targeted products SKUs is in cart
        if ( array_intersect( $targeted_SKUs, cart_items_skus() ) ) {
            // Field attribute name (and ID)
            $field_slug = 'cart_disclaimer_cb'; 

            // Enqueue jQuery code
            wc_enqueue_js("const strPTC = '.checkout-button', btnPTC = $(strPTC), checkoutURL = btnPTC.prop('href');
            btnPTC.removeAttr('href').addClass('disabled');
            $(document.body).on('change', 'input[name={$field_slug}]', function(){
                $(this).prop('checked') ? 
                btnPTC.prop('href', checkoutURL).removeClass('disabled') :
                btnPTC.removeAttr('href').addClass('disabled');
            });");

            // Display the checkbox
            woocommerce_form_field( $field_slug, array(
                'type'          => 'checkbox',
                'class'         => array('form-row disclaimer-checkbox'), // 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 have read and understood ICG70 Rover disclaimer.',  // Label and Link
            ));
        }
    }
}

Code goes in functions.php file of your child theme (or in a plugin).

If a targeted SKU is in cart, the disclaimer checkbox will be displayed and the button "Proceed to checkout" will be disabled, until the customer check this disclaimer checkbox.

发布评论

评论列表(0)

  1. 暂无评论