4ck4
4ck4

Reputation: 19

WooCommerce My Account Dashboard: Direct link to edit billing address

As I sell virtual products I don’t need the WooCommerce shipping address fields. I've already removed them from the checkout page and also want to remove them from the “My Account” page.

In the dashboard under the „Edit address“ tab the user should be directed directly to the page where he can edit the billing address. The page that shows the billing / shipping address and where the user has to click on "Edit address" is not longer needed.

Is there a way to achieve this? Any help is appreciated.

Upvotes: 1

Views: 2229

Answers (1)

Polar
Polar

Reputation: 3547

You can unset the Address endpoint and create a new one for Billing. For example, in your functions.php add the following code.

//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;
}

Then go to your Dashboard > Settings > Permalinks, then click the button Save, that should do it.



Alternatively, you can override the my-address.php located on woocommerce/templates/myaccount, just use WC_Shortcode_My_Account::edit_address( 'billing' );.

Or you can redirect into Billing when user(s) is trying to access the Edit-Address endpoint, try using the below code in your 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);

Upvotes: 1

Related Questions