I’ve got a question.. we got a script from Enhancer for WooCommerce Subscription. This script is checking if the customer already had the product, if yes, he don’t give a trial.
But all our products are the same service, just a little bit diverent points inside the packages.
So we want to check all products, not only the chosen one. Is there a way to change the get_id part to check all products?
This is the code part:
public static function limit_trial( $trial_length, $product ) {
if ( $trial_length <= 0 ) {
return $trial_length ;
}
$user_id = get_current_user_id() ;
if ( ! $user_id ) {
return $trial_length ;
}
if ( isset( self::$onetime_trial_cache[ $user_id ][ $product->get_id() ] ) ) {
return self::$onetime_trial_cache[ $user_id ][ $product->get_id() ] ? 0 : $trial_length ;
}
if ( $product->is_type( 'variation' ) ) {
$parent_product = wc_get_product( $product->get_parent_id() ) ;
} else {
$parent_product = $product ;
}
if ( 'no' !== self::get_product_limitation( $parent_product ) ) {
self::$onetime_trial_cache[ $user_id ][ $product->get_id() ] = false ;
return $trial_length ;
}
if ( 'yes' !== get_post_meta( $parent_product->get_id(), '_enr_limit_trial_to_one', true ) ) {
self::$onetime_trial_cache[ $user_id ][ $product->get_id() ] = false ;
return $trial_length ;
}
$subscriptions = wcs_get_users_subscriptions( $user_id ) ;
foreach ( $subscriptions as $subscription ) {
if ( $subscription->has_product( $product->get_id() ) && ( '' !== $subscription->get_trial_period() || 0 !== $subscription->get_time( 'trial_end' ) ) ) {
self::$onetime_trial_cache[ $user_id ][ $product->get_id() ] = true ;
return 0 ;
}
}
self::$onetime_trial_cache[ $user_id ][ $product->get_id() ] = false ;
return $trial_length ;
}
2
Answers
Shouldn't it just be that part of the code where he is checking, what product he had.
So maybe just the input with the product ID's is wrong. I really can define them by using the product IDs (I know its not dynamic, but that would be no problem). How I have to enter the products I need to check?
You have to get all products using
WP_Query()
then you can iterate the loop to get the product. try the below code.