Sjoerd
Sjoerd

Reputation: 87

Contact Form 7 check quantity of fields

In wordpress I'm using Contact Form 7. I want to create a order form. But the order quantity has to have a minimum of 2 products. When people order less then two products they have to get a message that it's not possible.

There are a total of 6 different products people can order. Each product can ben orderd as many as they like.

Any ideas how I can make this work?

Upvotes: 0

Views: 677

Answers (1)

Sjoerd
Sjoerd

Reputation: 87

I found a way. I've created a new filter for the Contact Form

Code below:

add_filter( 'wpcf7_validate_number*', 'custom_number_confirmation_validation_filter', 20, 2 );

function custom_number_confirmation_validation_filter( $result, $tag ) {
$tag = new WPCF7_Shortcode( $tag );

    $var1 = isset( $_POST['your_number1'] ) ? trim( $_POST['your_number1'] ) : '';
    $var2 = isset( $_POST['your_number2'] ) ? trim( $_POST['your_number2'] ) : '';
    $var3 = isset( $_POST['your_number3'] ) ? trim( $_POST['your_number3'] ) : '';
    $var4 = isset( $_POST['your_number4'] ) ? trim( $_POST['your_number4'] ) : '';
    $var5 = isset( $_POST['your_number5'] ) ? trim( $_POST['your_number5'] ) : '';
    $var6 = isset( $_POST['your_number6'] ) ? trim( $_POST['your_number6'] ) : '';

    $varNumber = '2';
    if ( $var1 + $var2 + $var3 + $var4 + $var5 + $var6  <  $varNumber) {
        $result->invalidate( $tag, "Order a minimum of 2!" );
            }


return $result;}

Replace your_number# with the name of the field in Contact Form 7

Upvotes: 0

Related Questions