I’m attempting to build a membership system in WordPress that utilizes WooCommerce with Gravity Forms and integrates with SalesForce.
Can somebody give me some direction on how to properly utilize the woocommerce_add_to_cart_validation
filter? I’m simply not sure how to properly utilize this hook.
Not matter what I do, if I try to perform any custom validation, the item is not added to the cart. Below is my code:
add_filter( 'woocommerce_add_to_cart_validation', 'validate_active_subscription', 10, 10 );
function validate_active_subscription( $valid, $product_id, $quantity, $variation_id, $variations ) {
global $woocommerce;
$cart_item_data = (array) apply_filters( 'woocommerce_add_cart_item_data', $cart_item_data, $product_id, $variation_id );
$item_data = $cart_item_data["_gravity_form_lead"];
//CAPTURE THE ID OF THE MEMBERSHIP CATEGORY
$idObj = get_term_by('slug','membership','product_cat');
$membership_id = $idObj->term_id;
$membership_purchased = false;
$terms = woocommerce_get_product_terms($product_id, 'product_cat' );
foreach ($terms as $term) {
if($term->term_id == $membership_id){
$membership_purchased = true;
}
}
if($membership_purchased){
//LET'S CHECK THAT, IF THIS EMAIL EXISTS WITHIN SALESFORCE, IT IS ASSOCATED WITH THE SAME INDIVIDUAL
try {
$mySforceConnection = SalesForce_Access::Connection();
$sf_contact = SalesForce_Access::Search_Contacts_Via_Email($mySforceConnection, $item_data[6]);
if( $sf_contact->FirstName == $item_data[3] && $sf_contact->LastName == $item_data[4]){
//$valid = true;
}else{
//$woocommerce->add_error("The email address you supplied already exists within our system. Please supply a different email");
//$valid = false;
}
}
catch (Exception $e)
{
//$woocommerce->add_error($e->faultstring);
//$valid = false;
}
}
}
Before the user adds the membership item to the cart, I want to check the email address, first name and last name to verify that the information properly matches an existing record (e.g. if the email exists, the supplied first name and last name should also match).
However, how do I properly pass validation (or invalidation) back to WooCommerce so it can go back to standard operation. Everything I do seems to loop back to the single product page and remove my gravity form fields.
Any thoughts would be appreciated.
I was able to discover the problem.
It appears that the Gravity Forms add-on has its own validation which does not check the status of validation prior to perform its own logic. Therefore, any previous validation that had failed would not trip the Gravity Forms validation, therefore allowing Gravity Forms to validate and removing them from the product page.
The solution to this is to add the following code to line 414 of the gravityforms-product-addons.php file:
I have submitted this as a bug, so hopefully it will be resolved in a future update.