admin管理员组文章数量:1026989
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
1 Answer
Reset to default 2This 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.
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
1 Answer
Reset to default 2This 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.
本文标签: phpWoocommerceCheckmark validation not working for proceedtocheckout buttonStack Overflow
版权声明:本文标题:php - Woocommerce - Checkmark validation not working for proceed_to_checkout button - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745668302a2162304.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论