首页 > 解决方案 > WooCommerce 我的帐户仪表板:编辑帐单地址的直接链接

问题描述

当我销售虚拟产品时,我不需要 WooCommerce 送货地址字段。我已经从结帐页面中删除了它们,并且还想从“我的帐户”页面中删除它们。

在“编辑地址”选项卡下的仪表板中,用户应该被直接引导到他可以编辑帐单地址的页面。不再需要显示帐单/送货地址以及用户必须单击“编辑地址”的页面。

有没有办法做到这一点?任何帮助表示赞赏。

标签: phpwordpresswoocommerce

解决方案


您可以取消设置Address端点并为Billing. 例如,在您functions.php添加以下代码。

//1
function add_d_endpoint() {
    add_rewrite_endpoint( 'billing', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'add_d_endpoint' );
//2  
function d_query_vars( $vars ) {
    $vars[] = 'billing';
    return $vars;
}
add_filter( 'query_vars', 'd_query_vars', 0 );
//3
function add_d_link_my_account( $items ) {
    $items[ 'billing' ] = 'Billing Address'; //The title of new endpoint
    return $items;
}
add_filter( 'woocommerce_account_menu_items', 'add_d_link_my_account' );
//4
function d_content() {
    echo WC_Shortcode_My_Account::edit_address( 'billing' ); //The content of new endpoint
}
//5
add_action( 'woocommerce_account_billing_endpoint', 'd_content' );
// Note: add_action must follow 'woocommerce_account_{your-endpoint-slug}_endpoint' format

//6
/** Remove Address from My Account Menu **/
add_filter( 'woocommerce_account_menu_items', 'dsx_remove_my_account_dashboard' );
function dsx_remove_my_account_dashboard( $menu_links ) {
    unset( $menu_links[ 'edit-address' ] ); //remove the address from endpoint
    return $menu_links;
}

然后转到您的Dashboard> Settings> Permalinks,然后单击按钮Save,应该这样做。



或者,您可以覆盖my-address.php位于的woocommerce/templates/myaccount,只需使用WC_Shortcode_My_Account::edit_address( 'billing' );.

或者您可以重定向到Billing用户尝试访问Edit-Address端点时,尝试在您的functions.php.

function redirect_to_billing( $wp ) {
    $current_url = home_url(add_query_arg(array(),$wp->request));
    $billing = home_url('/account/edit-address/billing');
    /** If user is accessing edit-address endpoint and it's not the billing address**/
    if(is_wc_endpoint_url('edit-address') && $current_url !== $billing){ 
        wp_redirect($billing);
        exit();
    }
    
}
add_action( 'parse_request', 'redirect_to_billing' , 10);

推荐阅读