Invalidate and Abort Contact Form 7 on a condition.

Invalidate and Abort Contact Form 7 on a condition.

Invalidating a contact form 7 address

/*Add hidden field as cookie id*/
add_filter( 'shortcode_atts_wpcf7', 'custom_shortcode_atts_wpcf7_filter', 10, 3 );
function custom_shortcode_atts_wpcf7_filter( $out, $pairs, $atts ) {
    
    $cookieid = 'cookieid';
    if ( isset( $atts[$cookieid] ) ) {
        $out[$cookieid] = $atts[$cookieid];
    }


    $cookiename = 'cookiename';
    if ( isset( $atts[$cookiename] ) ) {
        $out[$cookiename] = $atts[$cookiename];
    }

    return $out;
}
/*End add hidden field as cookie id*/



add_action("wpcf7_before_send_mail", "wpcf7_do_something_else",90,3);  
function wpcf7_do_something_else($cf7, &$abort, $submission) {

  
    $submission 	= WPCF7_Submission::get_instance();
    $posted_data 	= $submission->get_posted_data(); 
    global $wpdb;

	if( 151 == $cf7->id ){


    	 
    	$stock_qty     = get_post_meta ( $posted_data['cookieid'], 'stock_quantity', true );
    	$cookie_status = get_post_meta ( $posted_data['cookieid'], 'cookie_status', true );


    	/*Sold Out Fast*/
		if($stock_qty == 0){

			$errMsg = 'Sold out! Sorry, those went fast. Better luck next time.';
			$abort = true; 
			$submission->set_status( 'validation_failed' );
			$submission->set_response( $cf7->message( 'validation_error' ) ); //msg from admin settings;
			$submission->set_response( $cf7->filter_message($errMsg) ); //custom msg;
			
		}
		/*Sold Out Fast*/
    	
    	if($stock_qty >0 && $cookie_status=='live'){

    		$stock_qty-=1;
    		update_post_meta( $posted_data['cookieid'], 'stock_quantity', $stock_qty );

		    $tablename  	= $wpdb->prefix . "cookie_address";
			$wpdb->insert( 
							$tablename, 
				   array( 
		 		   			'first_name'=> $posted_data['First_Name'],
				   			'last_name' => $posted_data['Last_Name'],
				   			'email' 	=> $posted_data['Email'], 
				   			'cookieid'  => $posted_data['cookieid'], 
							'address1'  => $posted_data['Address_Line_1'],
							'address2' 	=> $posted_data['Address_Line_2'],
							'city'    	=> $posted_data['City'],
							'state'   	=> $posted_data['State'][0],
							'zipcode' 	=> $posted_data['Zip_Code']
						), 
				   array( 
				   			'%s', 
				   			'%s', 
				   			'%s', 
				   			'%s', 
				   			'%s', 
				   			'%s',
				   			'%s',
				   			'%s', 
				   			'%s'
				   		) 
			);
    	}
    	
 	}
     return $cf7;
}

screen103

screen104

screen105

Leave a Reply

Your email address will not be published. Required fields are marked *