我正在尝试使url.com/my-account或简码[woocommerce_my_account]显示订单,而不是显示“ Hello User(非用户)?”的仪表板。
我唯一需要的是登录后重定向到订单而不是仪表板,但是随后我进入/ my-account仍然显示我不想要的仪表板。
我发现最能满足我需求的代码是...
function woocommerce_orders() {
$user_id = get_current_user_id();
if ($user_id == 0) {
return do_shortcode('[woocommerce_my_account]');
}else{
ob_start();
wc_get_template( 'myaccount/my-orders.php', array(
'current_user' => get_user_by( 'id', $user_id),
'order_count' => $order_count
) );
return ob_get_clean();
}
}
add_shortcode('woocommerce_orders', 'woocommerce_orders');
但是,如果没有下订单,那么它会变成空白(不显示“没有订单已完成。”和“商店”按钮),并且我的帐户导航侧栏也不会显示。我是否需要为此创建一个自定义页面模板才能将其添加到woocommerce帐户nav-sidebar中?
编辑:如果我使用orders.php而不是my-orders.php,那么我可以得到“尚未完成订单”。但是仍然没有侧边栏导航
答案 0 :(得分:2)
您可以尝试以下代码(由于它删除了对仪表板的访问权限,因此并不完美):
add_action( 'woocommerce_account_content', 'remove_dashboard_account_default', 5 );
function remove_dashboard_account_default() {
remove_action( 'woocommerce_account_content', 'woocommerce_account_content', 10 );
add_action( 'woocommerce_account_content', 'custom_account_orders', 10 );
}
function custom_account_orders( $current_page ) {
global $wp;
if ( ! empty( $wp->query_vars ) ) {
foreach ( $wp->query_vars as $key => $value ) {
// Ignore pagename param.
if ( 'pagename' === $key ) {
continue;
}
if ( has_action( 'woocommerce_account_' . $key . '_endpoint' ) ) {
do_action( 'woocommerce_account_' . $key . '_endpoint', $value );
return;
}
}
}
$current_page = empty( $current_page ) ? 1 : absint( $current_page );
$customer_orders = wc_get_orders( apply_filters( 'woocommerce_my_account_my_orders_query', array(
'customer' => get_current_user_id(),
'page' => $current_page,
'paginate' => true,
) ) );
wc_get_template(
'myaccount/orders.php',
array(
'current_page' => absint( $current_page ),
'customer_orders' => $customer_orders,
'has_orders' => 0 < $customer_orders->total,
)
);
}
代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试和工作。
答案 1 :(得分:1)
有一种更简单的方法:只需捕获WordPress的parse_request
,检查request
是否用于my-account
(或任何您的帐户页面上的子弹),然后执行重定向:
function vnm_wc_redirect_account_dashboard( $wp ) {
if ( !is_admin() ) {
// Uncomment the following line if you want to see what the current request is
//die( $wp->request );
// The following will only match if it's the root Account page; all other endpoints will be left alone
if ( $wp->request === 'my-account' ) {
wp_redirect( site_url( '/my-account/orders/' ) );
exit;
}
}
}
add_action( 'parse_request', 'vnm_wc_redirect_account_dashboard', 10, 1 );
答案 2 :(得分:0)
我使用了LoicTheAztec的代码和另一个补充代码段,该代码段删除了仪表板选项卡:
var request = require("request");
reqOptions.qs = {
"pageToken": options.meta.pageToken,
"fields": "*",
"includeRemoved": true
}
reqOptions.url = "https://www.googleapis.com/drive/v3/changes";
reqOptions.headers = {
Authorization: "Bearer " + input.auth.access_token
};
request(reqOptions, function(err, res, body) {
try {
if (typeof(body) == "string") {
body = JSON.parse(body);
}
} catch (e) {
return output(e);
}
for (var i = 0; i < body.changes.length; i++) {
//console.log(body.changes[i].file)
// if (body.changes[i].file.trashed ) {
if (body && body.changes[i] && body.changes[i].file && body.changes[i].file.trashed) {
//add to result
}
}
}):
我还创建了一个代码段,该代码段默认使“订单”标签突出显示。通过添加活动类,然后添加CSS透明度来完成:1突出显示它。脚本将仅在帐户部分显示,以避免不必要的地方出现膨胀:
// Remove or rename my account page navigation links (removes downloads and dashboard).
add_filter ( 'woocommerce_account_menu_items', 'my_account_menu_order' );
function my_account_menu_order() {
$menuOrder = array(
'orders' => __( 'Orders', 'woocommerce' ),
// 'downloads' => __( 'Download', 'woocommerce' ),
'edit-address' => __( 'Addresses', 'woocommerce' ),
'edit-account' => __( 'Account details', 'woocommerce' ),
'customer-logout' => __( 'Logout', 'woocommerce' ),
// 'dashboard' => __( 'Dashboard', 'woocommerce' )
);
return $menuOrder;
}
答案 3 :(得分:0)
使用parse_request
操作:
add_action( 'parse_request', function ( $wp ) {
// Prevent the redirection, in the case,
// the user is not logged in (no login, no orders)
if (!is_user_logged_in()) return false;
if ( $wp->request === 'my-account' ) {
wp_redirect( home_url( '/my-account/orders/' ) );
exit;
}
}, 10, 1 );