1 回答

TA貢獻1829條經驗 獲得超7個贊
template_redirect
- 在確定要加載的模板之前觸發。wp_get_current_user
- 檢索當前用戶對象。wc_customer_bought_product
- 檢查用戶(通過電子郵件或 ID 或兩者)是否購買了商品。
function action_template_redirect() {
? ? global $wp;
? ??
? ? // Retrieve the current user object.
? ? $current_user = wp_get_current_user();
? ? if ( $current_user->ID == 0 ) return;
? ??
? ? // DEBUG PURPOSES, UNCOMMENT IF NECESSARY
? ? //echo $wp->request;
? ? // On my-account page
? ? if( $wp->request == 'my-account' ) {
? ? ? ? // Set product ID
? ? ? ? $product_id = 11344;
? ? ? ??
? ? ? ? // If true
? ? ? ? if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product_id ) ) {
? ? ? ? ? ? // Redirect
? ? ? ? ? ? wp_safe_redirect('/my-account/my-listings/');
? ? ? ? ? ? exit;
? ? ? ? }? ?
? ? }
}
add_action( 'template_redirect', 'action_template_redirect' );
- 1 回答
- 0 關注
- 142 瀏覽
添加回答
舉報