How I can validate whenever a product is added to cart ?
-
I using wordpress 4.3.1, woocommerce 2.4.6 and pmpro 1.8.5.6 .
I have saved the membership level as metadata, code below is what I am using to validate the product but it is not working
.If someone will try to use this url to add the product , the product will be added to cart, this is what I am trying to restrict . https://localhost/membership/cart/?add-to-cart=99<?php function wcmemebrshipproductnotincart( $valid, $productid, $quantity ) { if(is_user_logged_in() && function_exists('pmpro_hasMembershipLevel') && pmpro_hasMembershipLevel()){ global $current_user; $current_user->membership_level = pmpro_getMembershipLevelForUser($current_user->ID); } $level_id = get_post_meta( get_the_ID(), '_membership_product_level', true ); $level = pmpro_getLevel($level_id); $name = $level->name; if( $level_id ){ if( $current_user->membership_level->name != $name || !is_user_logged_in() ){ $valid = false; wc_add_notice( __( 'You can not do that', 'textdomain' ), 'error' ); } else { $valid = true; } } else { $valid = true; } return $valid; } addfilter( 'woocommerceaddtocartvalidation', 'wcmemebrshipproductnotincart', 10, 3 );
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘How I can validate whenever a product is added to cart ?’ is closed to new replies.