Is there a way to block logged in user from buy any product after the first purchase of any product?
I want to limit each subscription to buy only one product and then no more shops on any product, only with new subscription.
I found a filter that do almost what i need but limit only one per product,
add_filter('woocommerce_add_to_cart_validation','rei_woocommerce_add_to_cart_validation',20, 2);
function rei_woocommerce_add_to_cart_validation($valid, $product_id){
$current_user = wp_get_current_user();
if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product_id)) {
wc_add_notice( __( 'Purchased', 'woocommerce' ), 'error' );
$valid = false;
}
return $valid;
}
but i need to lock every other product after first purchase of a subscription.
Maybe the best way would be when user click to pay for the product at check out page (at this time he need to be logged in) and check if he already purchased any product before.. if yes, he cant pay, only with new subscription…
im ugly on coding.. could somebody help me with this???
Here is a solution for one subscription per cart, or membership if you are using subscriptions as product. Wish I knew who to give credit to but I have had this one in my magic kit for a while.
This works on WC 3.0+ and uses instances of the WCS_ class.
For those who are looking for the same answer, here is what i did:
I’m reading OP’s requirement slightly different. That is, that each user can only have one subscription at any one time. For that, this is my solution:
}