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 ;
}
CodePudding user response:
You have to get all products using WP_Query()
then you can iterate the loop to get the product. try the below code.
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 ;
}
$params = array(
'posts_per_page' => -1,
'post_type' => 'product'
);
$products = new WP_Query( $params );
if ( $products->have_posts() ) { while ( $products->have_posts() ) { $products->the_post();
if ( isset( self::$onetime_trial_cache[ $user_id ][ get_the_ID() ] ) ) {
return self::$onetime_trial_cache[ $user_id ][ get_the_ID() ] ? 0 : $trial_length ;
}
if ( $product->is_type( 'variation' ) ) {
$parent_product = wc_get_product( $product->get_parent_id() ) ;
} else {
$parent_product = wc_get_product( get_the_ID() ) ;
}
if ( 'no' !== self::get_product_limitation( $parent_product ) ) {
self::$onetime_trial_cache[ $user_id ][ get_the_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 ][ get_the_ID() ] = false ;
return $trial_length ;
}
$subscriptions = wcs_get_users_subscriptions( $user_id ) ;
foreach ( $subscriptions as $subscription ) {
if ( $subscription->has_product( get_the_ID() ) && ( '' !== $subscription->get_trial_period() || 0 !== $subscription->get_time( 'trial_end' ) ) ) {
self::$onetime_trial_cache[ $user_id ][ get_the_ID() ] = true ;
return 0 ;
}
}
self::$onetime_trial_cache[ $user_id ][ get_the_ID() ] = false ;
} wp_reset_postdata(); }
return $trial_length ;
}
CodePudding user response:
Shouldn't it just be that part of the code where he is checking, what product he had.
foreach ( $subscriptions as $subscription ) {
if ( $subscription->has_product('295587','295585','295578') && ( '' !== $subscription->get_trial_period() || 0 !== $subscription->get_time( 'trial_end' ) ) ) {
self::$onetime_trial_cache[ $user_id ][ $product->get_id() ] = true ;
return 0 ;
}
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?