检测 WooCommerce "my account" 页面的仪表板
Detect dashboard of WooCommerce "my account" pages
如何检测仪表板上是否使用了 "myaccount/my-account.php" 模板。
目前我使用:
<?php
global $wp;
if ( !isset($wp->query_vars['page']) ) {
?>
<a href="/mein-konto/">Back to my Account</a>
<?php } ?>
<div class="myaccount_content">
<?php
do_action( 'woocommerce_account_content' );
?>
</div>
但这感觉有点老套。没有类似is_myaccount_dashboard()
的功能吗?
更新:专门检测我的帐户"Dashboard"页面
<?php
global $wp;
$request = explode( '/', $wp->request );
// If NOT in My account dashboard page
if( ! ( end($request) == 'my-account' && is_account_page() ) ){
?>
<a href="<?php echo get_permalink( get_option('woocommerce_myaccount_page_id')); ?>">Back to my Account Dashboard</a>
<?php
}
?>
<div class="myaccount_content">
<?php
do_action( 'woocommerce_account_content' );
?>
</div>
已测试并有效。
原回答:
是的,当然有 is_account_page()
本机 WooCommerce 条件是 returns 在客户的帐户页面上为真。
这是一个使用 is_account_page()
和 is_user_logged_in()
的示例。要获取我的帐户 link url,您可以使用:get_permalink( get_option('woocommerce_myaccount_page_id') )
.
if ( !is_account_page() ) { // User is NOT on my account pages
if ( is_user_logged_in() ) { // Logged in user
// Link to "My Account pages dashboard".
?>
<a href="<?php echo get_permalink( get_option('woocommerce_myaccount_page_id') ); ?>" title="<?php _e('My Account', 'woocommerce'); ?>"><?php _e( 'My Account', 'woocommerce' ); ?></a>
<?php }
else { // User is NOT logged in
// Link to "Login / register page".
?>
<a href="<?php echo get_permalink( get_option('woocommerce_myaccount_page_id') ); ?>" title="<?php _e( 'Login / Register','woocommerce' ); ?>"><?php _e( 'Login / Register', 'woocommerce' ); ?></a>
<?php
}
}
?>
参考:
之后,您可以 Override WooCommerce Templates via a Theme 使用我的帐户模板来微调更多 WooCommerce 行为……
要在“我的帐户”区域中检测您所在的确切页面(以允许您确定正在使用哪个模板),我认为 Woocommerce 没有提供一种方法。
我认为您必须获取当前 URL 和原版 PHP,并将其与设置为 [=] 的页面的 URL 进行比较20=] 帐户主页。
例如
$current_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$dashboard_url = get_permalink( get_option('woocommerce_myaccount_page_id'));
if($dashboard_url == $current_url){
// do your stuff here
}
Woocommerce 的 is_account_page()
条件函数将 return 对所有“我的帐户”子页面为真,因此不能用于确定您是否专门在仪表板页面上。
我有同样的问题(多年后,哈哈)。对于查看答案并想知道为什么它没有帮助的人来说,woocommerce 中提供了端点检测功能,可以完全满足您的需求。您可以阅读可用函数列表 here.
这直接取自 woocommerce 文档。我只是复制它以防 link 将来被破坏
is_account_page() =>
Returns 在客户的帐户页面上是正确的。
is_wc_endpoint_url() =>
Returns 在查看任何 WooCommerce 端点时为真
is_wc_endpoint_url('order-pay')=>
当显示订单支付的端点页面时。
is_wc_endpoint_url('order-received')=>
当显示收到订单的端点页面时。
is_wc_endpoint_url('view-order')=>
当显示查看顺序的端点页面时。
is_wc_endpoint_url('edit-account')=>
当显示编辑帐户的端点页面时。
is_wc_endpoint_url('edit-address')=>
当显示编辑地址的端点页面时。
is_wc_endpoint_url('lost-password')=>
当显示丢失密码的端点页面时。
is_wc_endpoint_url('customer-logout')=>
当显示客户注销端点页面时。
is_wc_endpoint_url('add-payment-method')=>
当显示添加支付方式的端点页面时。
<?php if(is_page("account") && !is_wc_endpoint_url()) { ?>
假设您的帐户页面位于 /account/,这将检测您的信息中心。
如果您只执行 is_page("account")
,条件将触发所有帐户页面。但是,因为仪表板不像 'view-order' 或 'last-password' 那样被视为 WC 端点,所以这个简单的检查就可以完成工作。
实际上,我发现这种情况似乎可以正常工作,以便仅使用本机 WC 代码检测 WC 仪表板页面:
if (is_user_logged_in() && is_account_page() && !is_wc_endpoint_url()) {
echo 'WC Dashboard';
} else {
echo 'no WC Dashboard';
}
如何检测仪表板上是否使用了 "myaccount/my-account.php" 模板。
目前我使用:
<?php
global $wp;
if ( !isset($wp->query_vars['page']) ) {
?>
<a href="/mein-konto/">Back to my Account</a>
<?php } ?>
<div class="myaccount_content">
<?php
do_action( 'woocommerce_account_content' );
?>
</div>
但这感觉有点老套。没有类似is_myaccount_dashboard()
的功能吗?
更新:专门检测我的帐户"Dashboard"页面
<?php
global $wp;
$request = explode( '/', $wp->request );
// If NOT in My account dashboard page
if( ! ( end($request) == 'my-account' && is_account_page() ) ){
?>
<a href="<?php echo get_permalink( get_option('woocommerce_myaccount_page_id')); ?>">Back to my Account Dashboard</a>
<?php
}
?>
<div class="myaccount_content">
<?php
do_action( 'woocommerce_account_content' );
?>
</div>
已测试并有效。
原回答:
是的,当然有 is_account_page()
本机 WooCommerce 条件是 returns 在客户的帐户页面上为真。
这是一个使用 is_account_page()
和 is_user_logged_in()
的示例。要获取我的帐户 link url,您可以使用:get_permalink( get_option('woocommerce_myaccount_page_id') )
.
if ( !is_account_page() ) { // User is NOT on my account pages
if ( is_user_logged_in() ) { // Logged in user
// Link to "My Account pages dashboard".
?>
<a href="<?php echo get_permalink( get_option('woocommerce_myaccount_page_id') ); ?>" title="<?php _e('My Account', 'woocommerce'); ?>"><?php _e( 'My Account', 'woocommerce' ); ?></a>
<?php }
else { // User is NOT logged in
// Link to "Login / register page".
?>
<a href="<?php echo get_permalink( get_option('woocommerce_myaccount_page_id') ); ?>" title="<?php _e( 'Login / Register','woocommerce' ); ?>"><?php _e( 'Login / Register', 'woocommerce' ); ?></a>
<?php
}
}
?>
参考:
之后,您可以 Override WooCommerce Templates via a Theme 使用我的帐户模板来微调更多 WooCommerce 行为……
要在“我的帐户”区域中检测您所在的确切页面(以允许您确定正在使用哪个模板),我认为 Woocommerce 没有提供一种方法。
我认为您必须获取当前 URL 和原版 PHP,并将其与设置为 [=] 的页面的 URL 进行比较20=] 帐户主页。
例如
$current_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$dashboard_url = get_permalink( get_option('woocommerce_myaccount_page_id'));
if($dashboard_url == $current_url){
// do your stuff here
}
Woocommerce 的 is_account_page()
条件函数将 return 对所有“我的帐户”子页面为真,因此不能用于确定您是否专门在仪表板页面上。
我有同样的问题(多年后,哈哈)。对于查看答案并想知道为什么它没有帮助的人来说,woocommerce 中提供了端点检测功能,可以完全满足您的需求。您可以阅读可用函数列表 here.
这直接取自 woocommerce 文档。我只是复制它以防 link 将来被破坏
is_account_page() => Returns 在客户的帐户页面上是正确的。
is_wc_endpoint_url() => Returns 在查看任何 WooCommerce 端点时为真
is_wc_endpoint_url('order-pay')=> 当显示订单支付的端点页面时。
is_wc_endpoint_url('order-received')=> 当显示收到订单的端点页面时。
is_wc_endpoint_url('view-order')=> 当显示查看顺序的端点页面时。
is_wc_endpoint_url('edit-account')=> 当显示编辑帐户的端点页面时。
is_wc_endpoint_url('edit-address')=> 当显示编辑地址的端点页面时。
is_wc_endpoint_url('lost-password')=> 当显示丢失密码的端点页面时。
is_wc_endpoint_url('customer-logout')=> 当显示客户注销端点页面时。
is_wc_endpoint_url('add-payment-method')=> 当显示添加支付方式的端点页面时。
<?php if(is_page("account") && !is_wc_endpoint_url()) { ?>
假设您的帐户页面位于 /account/,这将检测您的信息中心。
如果您只执行 is_page("account")
,条件将触发所有帐户页面。但是,因为仪表板不像 'view-order' 或 'last-password' 那样被视为 WC 端点,所以这个简单的检查就可以完成工作。
实际上,我发现这种情况似乎可以正常工作,以便仅使用本机 WC 代码检测 WC 仪表板页面:
if (is_user_logged_in() && is_account_page() && !is_wc_endpoint_url()) {
echo 'WC Dashboard';
} else {
echo 'no WC Dashboard';
}