Check if a customer has already purchased a subscription product
<?php
if( class_exists('YITH_WC_Subscription')){
add_filter( 'woocommerce_add_to_cart_validation', 'check_customer_bought_product', 10, 4 );
function check_customer_bought_product( $valid, $product_id, $quantity, $variation_id = 0 ){
$id = ( $variation_id ) ? $variation_id : $product_id;
if ( YITH_WC_Subscription()->is_subscription( $id ) ) {
$current_user = wp_get_current_user();
// determine if customer has bought product
if( $current_user && wc_customer_bought_product( $current_user->email, $current_user->ID, $id ) ){
$message = __( 'This subscription has been removed from your cart. You have already purchased this product', 'yith-woocommerce-subscription' );
wc_add_notice( $message, 'notice' );
return false;
}
}
return $valid;
}
}