WooCommerce add to cart validation: prevent add to cart

后端 未结 3 1688
离开以前
离开以前 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.

    0 讨论(0)
  • 2021-02-06 17:05

    I had this issue and after much trial and error the fix was to adjust the $priority parameter (10 is default) in :

    add_filter( 'woocommerce_add_to_cart_validation', 'add_the_date_validation', 10, 5 );
    

    As per usual, I tried everything else first...changed this to 15 and bingo..! I guess it was running ahead of code ranked lower priority, which then let items into cart.

    0 讨论(0)
  • 2021-02-06 17:11

    Yes, sorry i'am new in this forum. However i have found a solution, i have edit directly class-wc-cart.php at line 900, and i have copy the function before the if:

    if($passed!=false)
    {
        do_action( 'woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data );
    }
    

    Now for me it works, i know that isn't the most correct solution but for the moment it works, i have only to be careful when i will update woocommerce

    0 讨论(0)
提交回复
热议问题