├── .gitignore ├── README.md └── buddypress-woocommerce.php /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/* 2 | !.vscode/settings.json 3 | !.vscode/tasks.json 4 | !.vscode/launch.json 5 | !.vscode/extensions.json -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # buddypress-woocommerce 2 | Move WooCommerce My Account area to BuddyPress profile 3 | 4 | KNOWN ISSUES 5 | 6 | - Redirects may not work properly when running The Seo Framework plugin: https://github.com/robertstaddon/buddypress-woocommerce/issues/3 7 | 8 | CHANGELOG 9 | 10 | 1.4 - February 3, 2020 11 | - Use 'buddypress-woocommerce' text domain 12 | - Make Buddypress account URL filterable 13 | 14 | 1.3 - April 18, 2019 15 | - Fix: WooCommerce 3.6 Compatibility - Avoid 404 errors on account pages caused by https://github.com/woocommerce/woocommerce/pull/22631 16 | -------------------------------------------------------------------------------- /buddypress-woocommerce.php: -------------------------------------------------------------------------------- 1 | query_vars['add-payment-method'] ) ) 23 | return true; 24 | 25 | return ( is_page( wc_get_page_id( 'myaccount' ) ) && isset( $wp->query_vars['add-payment-method'] ) ); 26 | } 27 | } 28 | 29 | 30 | class BP_WooCommerce { 31 | 32 | public function __construct() { 33 | // Require WooCommerce to be active 34 | if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { 35 | // Check for BuddyPress and initialize if it is active 36 | add_action( 'bp_include', array( $this, 'init' ) ); 37 | } 38 | } 39 | 40 | /* 41 | * Initialize the plugin hooks if WooCommerce and BuddyPress are active 42 | */ 43 | public function init() { 44 | // Add WooCommerce navigation and subnavigation to BuddyPress 45 | add_action( 'bp_setup_nav', array( $this, 'bp_navigation') ); 46 | 47 | // Re-route WooCommerce Edit Account URL 48 | add_filter( 'woocommerce_customer_edit_account_url', array( $this, 'customer_edit_account_url' ) ); 49 | 50 | // Re-route all WooCommerce URL endpoints to appropriate BuddyPress pages 51 | add_filter( 'woocommerce_get_endpoint_url', array( $this, 'get_endpoint_url' ), 10, 4 ); 52 | } 53 | 54 | 55 | /* 56 | * Use the BuddyPress "Account Settings" page (/members/username/settings/) instead of the WooCommerce "Edit Account" page (/my-account/edit-account) 57 | * The WooCommerce page doesn't have "Display name publicly as..." 58 | */ 59 | public function customer_edit_account_url( $edit_account_url = "" ) { 60 | return bp_loggedin_user_domain() . "settings"; 61 | } 62 | 63 | 64 | /* 65 | * Add WooCommerce "My Account" to BuddyPress profile 66 | * http://xd3v.com/create-a-premium-social-network-with-woocommerce/ 67 | */ 68 | public function bp_navigation() { 69 | global $bp; 70 | 71 | $account_url = trailingslashit( $bp->loggedin_user->domain . 'account' ); 72 | $secure_account_url = str_replace( 'http:', 'https:', $account_url ); 73 | 74 | $wc_account_menu_items = $this->get_wc_account_menu_items(); 75 | 76 | // Add top-level Account menu item 77 | bp_core_new_nav_item( 78 | array( 79 | 'name' => __( 'Account', 'buddypress-woocommerce' ), 80 | 'slug' => 'account', 81 | 'default_subnav_slug' => 'view', 82 | 'show_for_displayed_user' => false, 83 | 'position' => 30, 84 | 'item_css_id' => 'account', 85 | ) 86 | ); 87 | 88 | $position = 0; 89 | foreach ( $wc_account_menu_items as $key => $item_title ) { 90 | $position += 10; 91 | if ( $key == 'dashboard') $key = 'view'; 92 | if ( strpos( $key, 'my-membership-details') !== false ) $key = 'members-area'; // WooCommerce Memberships: Don't link directly to a "My Membership Details" area because it requires unique ID in URL 93 | 94 | bp_core_new_subnav_item( 95 | array( 96 | 'name' => __( $item_title, 'buddypress-woocommerce' ), 97 | 'slug' => $key, 98 | 'parent_url' => $secure_account_url, 99 | 'parent_slug' => 'account', 100 | 'screen_function' => array( $this, 'account_screens' ), 101 | 'show_for_displayed_user' => false, 102 | 'position' => $position, 103 | 'item_css_id' => 'account-' . $key, 104 | ) 105 | ); 106 | } 107 | 108 | // Remove "Settings > Delete Account" 109 | bp_core_remove_subnav_item( 'settings', 'delete-account' ); 110 | } 111 | 112 | /** 113 | * Get $key => $value array of WooCommerce Account menu items for BuddyPress Account menu 114 | */ 115 | public function get_wc_account_menu_items() { 116 | // Start with the WooCommerce Account menu items 117 | $wc_account_menu_items = wc_get_account_menu_items(); 118 | 119 | // Add new items 120 | $wc_account_menu_items['add-payment-method'] = "Add Payment Method"; 121 | 122 | // Remove items that are on other BuddyPress menus 123 | unset( $wc_account_menu_items['customer-logout'] ); 124 | unset( $wc_account_menu_items['edit-account'] ); 125 | 126 | return $wc_account_menu_items; 127 | } 128 | 129 | 130 | /** 131 | * These are the screen_functions used by our custom BuddyPress navigation items 132 | */ 133 | function account_screens() { 134 | // Avoid 404 error in WooCommerce 3.6+ 135 | add_filter( 'woocommerce_is_account_page', '__return_true' ); 136 | 137 | //add_action( 'bp_template_title', array( $this, 'account_screen_title' ) ); 138 | add_action( 'bp_template_content', array( $this, 'account_content' ) ); 139 | bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) ); 140 | } 141 | function account_screen_title() { 142 | echo 'My Account'; 143 | } 144 | function account_content() { 145 | wc_print_notices(); 146 | do_action( 'woocommerce_account_content' ); 147 | } 148 | 149 | 150 | /** 151 | * Point WooCommerce endpoints to BuddyPress My Account pages 152 | */ 153 | public function get_endpoint_url( $url, $endpoint, $value, $permalink ) { 154 | $base_path = bp_loggedin_user_domain() . "account/"; 155 | $endpoint_path = $base_path . $endpoint . "/"; 156 | $endpoint_value_path = $endpoint_path . $value; 157 | 158 | $wc_account_menu_items = $this->get_wc_account_menu_items(); 159 | $wc_account_menu_items["delete-payment-method"] = "Delete Payment Method"; 160 | $wc_account_menu_items["set-default-payment-method"] = "Set Default Payment Method"; 161 | 162 | if( $endpoint == "edit-account" ) { 163 | return $this->customer_edit_account_url(); 164 | } 165 | elseif ( strpos( $endpoint, 'my-membership-details') !== false ) { // WooCommerce Memberships: Can't display "My Membership Details" area because of unique ID in URL 166 | return $url; 167 | } 168 | elseif ( array_key_exists( $endpoint, $wc_account_menu_items ) ) { 169 | if( $value ) 170 | return $endpoint_value_path; 171 | else 172 | return $endpoint_path; 173 | } 174 | else { 175 | return $url; 176 | } 177 | 178 | // if("/edit-address" == substr( $url, 0, 13 )) { 179 | // return "/" . basename( get_permalink( get_option('woocommerce_myaccount_page_id') ) ) . $url; 180 | // } 181 | } 182 | } 183 | new BP_WooCommerce; 184 | --------------------------------------------------------------------------------