Reputation: 69
I want to make a checkbox that needs to be clicked before a product can be added to the cart.
add_action( 'woocommerce_before_add_to_cart_button', 'add_privacy_policy', 9 );
function add_privacy_policy() {
woocommerce_form_field( 'privacy_policy', array(
'type' => 'checkbox',
'class' => array('form-row privacy'),
'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,
'label' => 'I\'ve read and accept the <a href="#">Privacy Policy</a>',
));
}
This is what I have so far. I now need to create an action that fires after I clicked the add to cart button but before the product is actually placed into the cart. If the box is not ticked I want to make it so that it outputs an error and the product is not added to the cart.
As I don't really know woocommerce I wanted to know whether there's such an action and if not, how to create the same effect.
Upvotes: 3
Views: 1133
Reputation: 2770
Just add the follows code snippet -
function add_privacy_policy_validation( $passed ) {
if ( !isset( $_REQUEST['privacy_policy'] ) ) {
wc_add_notice( __( 'Please accept privacy policy', 'woocommerce' ), 'error' );
$passed = false;
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'add_privacy_policy_validation', 99 );
Upvotes: 2