WooCommerce add to cart validation: prevent add to cart

后端 未结 3 1689
离开以前
离开以前 2021-02-06 16:42

I have a probleme with woocommerce that im trying to fix for few days.

I am creating a website for a guy and he wanted me to add a custom input on the product page, I cou

3条回答
  •  温柔的废话
    2021-02-06 17:01

    I would have said that you should use Product Add-Ons but it doesn't seem to have a date picker. Anyway, I would try modifying your validation function as follows:

    function add_the_date_validation( $passed ) { 
    if ( empty( $_REQUEST['thedate'] )) {
        wc_add_notice( __( 'Please enter a date.', 'woocommerce' ), 'error' );
        $passed = false;
    }
    return $passed;
    }
    add_filter( 'woocommerce_add_to_cart_validation', 'add_the_date_validation', 10, 5 );  
    

    Instead of returning TRUE you want to return the current status of the $passed variable. I can't promise that will work, because I am not going to set up the rest of the code required to test it, but this is similar to what I have done many times.

    On another note, unless you mean to apply this validation to every product in your store, you need to limit this function with some additional conditional logic.

提交回复
热议问题