我正试图在WordPress中建立一个会员系统,利用WooCommerce 具有Gravity Forms 并与集成SalesForce.
有人能告诉我如何正确使用woocommerce_add_to_cart_validation
滤器我只是不知道如何正确使用这个钩子。
无论我做什么,如果我尝试执行任何自定义验证,该项目都不会添加到购物车中。下面是我的代码:
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;
}
}
}
在用户将会员项目添加到购物车之前,我想检查电子邮件地址、名字和姓氏,以验证信息是否与现有记录正确匹配(例如,如果电子邮件存在,则提供的名字和姓氏也应匹配)。
然而,我如何正确地将验证(或无效)传递回WooCommerce,以便它能够返回到标准操作。我所做的一切似乎都回到了单一产品页面,并删除了重力表单字段。
如有任何想法,将不胜感激。