我有一個問題……我們從 Enhancer 獲得了一個用于 WooCommerce 訂閱的腳本。該腳本正在檢查客戶是否已經擁有該產品,如果是,則他不進行試用。
但是我們所有的產品都是一樣的服務,只是包裝內部有點不同。
所以我們要檢查所有產品,而不僅僅是選中的產品。有沒有辦法更改 get_id 部分以檢查所有產品?
這是代碼部分:
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 ;
}
uj5u.com熱心網友回復:
您必須使用所有產品,WP_Query()然后才能迭代回圈以獲取產品。試試下面的代碼。
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 ;
}
uj5u.com熱心網友回復:
不應該只是他正在檢查的那部分代碼,他有什么產品。
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 ;
}
所以也許只是產品 ID 的輸入是錯誤的。我真的可以通過使用產品 ID 來定義它們(我知道它不是動態的,但這沒問題)。我必須如何輸入我需要檢查的產品?
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/337094.html
標籤:php WordPress的 产品
上一篇:WordPressDocent主題如何左對齊h2博客標題
下一篇:動態分配模型屬性型別
