enabled = isset( $settings['replace_myaccount'] ) ? (bool) $settings['replace_myaccount'] : true; if ( ! $this->enabled ) { return; } // Hook into WooCommerce My Account - use woocommerce_account_content action. add_action( 'template_redirect', array( $this, 'setup_account_hooks' ) ); // Add body class. add_filter( 'body_class', array( $this, 'add_body_class' ) ); // Auto-apply pending coupon when customer adds an item to cart. add_action( 'woocommerce_add_to_cart', array( $this, 'apply_pending_coupon_on_add_to_cart' ) ); } /** * Automatically apply pending coupon when customer adds an item to cart. */ public function apply_pending_coupon_on_add_to_cart() { if ( ! function_exists( 'WC' ) || null === WC()->session || null === WC()->cart ) { return; } $pending_coupon = WC()->session->get( 'aca_pending_coupon' ); if ( ! empty( $pending_coupon ) && ! WC()->cart->has_discount( $pending_coupon ) ) { WC()->cart->apply_coupon( $pending_coupon ); WC()->session->__unset( 'aca_pending_coupon' ); } } /** * Setup hooks when on the account page. */ public function setup_account_hooks() { if ( ! is_account_page() || ! is_user_logged_in() ) { return; } // Check if on a native endpoint that should not be replaced. if ( $this->is_native_endpoint() ) { return; } // Remove default WooCommerce navigation. remove_action( 'woocommerce_account_navigation', 'woocommerce_account_navigation' ); // Remove all default content actions. remove_action( 'woocommerce_account_content', 'woocommerce_account_content' ); // Remove dashboard content. remove_action( 'woocommerce_account_dashboard', 'woocommerce_account_dashboard' ); // Add our content hook with high priority. add_action( 'woocommerce_account_content', array( $this, 'render_aca_app' ), 1 ); } /** * Check if current page is a native WooCommerce endpoint. * * @return bool */ /** * Check if current page is an endpoint that should bypass ACA rendering entirely. * * @return bool */ private function is_native_endpoint() { $bypass_endpoints = array( 'customer-logout', 'lost-password', ); global $wp; foreach ( $bypass_endpoints as $endpoint ) { if ( isset( $wp->query_vars[ $endpoint ] ) ) { return true; } } return false; } /** * Render the ACA React app. */ public function render_aca_app() { if ( ! is_user_logged_in() ) { return; } // Enqueue our assets. do_action( 'aca_enqueue_frontend_assets' ); // Determine current route based on WooCommerce endpoint. $current_route = $this->get_current_route(); $settings = get_option( 'aca_settings', array() ); $is_dark = ! empty( $settings['enable_dark_mode'] ); $classes = 'aca-container' . ( $is_dark ? ' aca-dark-mode' : '' ); // Render the container. echo '
'; } /** * Get current route based on WooCommerce endpoint. * * @return string */ private function get_current_route() { global $wp; // Handle view-order with order ID. if ( isset( $wp->query_vars['view-order'] ) && ! empty( $wp->query_vars['view-order'] ) ) { return 'orders/' . intval( $wp->query_vars['view-order'] ); } $endpoint_map = array( 'orders' => 'orders', 'edit-address' => 'addresses', 'edit-account' => 'profile', 'downloads' => 'downloads', 'coupons' => 'coupons', ); foreach ( $endpoint_map as $endpoint => $route ) { if ( isset( $wp->query_vars[ $endpoint ] ) ) { return $route; } } // Check any other registered WooCommerce query_vars (e.g. 3rd-party plugins or custom endpoints). $wc_items = function_exists( 'wc_get_account_menu_items' ) ? wc_get_account_menu_items() : array(); foreach ( array_keys( $wc_items ) as $endpoint ) { if ( isset( $wp->query_vars[ $endpoint ] ) ) { return sanitize_key( $endpoint ); } } // Default to dashboard. return 'dashboard'; } /** * Add body class for styling. * * @param array $classes Body classes. * @return array */ public function add_body_class( $classes ) { if ( is_account_page() && is_user_logged_in() && ! $this->is_native_endpoint() ) { $classes[] = 'aca-myaccount-active'; } return $classes; } }