Search code examples
phpwordpresswoocommercehook-woocommerceendpoint

Custom notice is not working on Woocommerce My-account custom endpoint page


I have been working on creating an error code that is for WooCommerce "My account" page. I have also successfully added a WooCommerce end point to this custom page, however, when I try using the composite hook "woocommerce_add_{$notice_type}" it wouldn't work on the custom page, but would work on the base WooCommerce endpoints like my-account.

Custom my-account page Registration (named edit-password):

add_filter ( 'woocommerce_account_menu_items', 'silva_log_history_link', 40 );
function silva_log_history_link( $menu_links ){
    $menu_links = array_slice( $menu_links, 0, 5, true ) 
    + array( 'edit-password' => 'Edit Password' )
    + array_slice( $menu_links, 5, NULL, true );
     
    return $menu_links;
}

add_action( 'init', 'silva_add_endpoint' );
function silva_add_endpoint() {
    // WP_Rewrite is my Achilles' heel, so please do not ask me for detailed explanation
    add_rewrite_endpoint( 'edit-password', EP_PAGES );
}
add_action( 'woocommerce_account_edit-password_endpoint', 'silva_my_account_endpoint_content' );
function silva_my_account_endpoint_content() {
    #some code for separation
}

Custom Error Code:

function custom_wc_add_notice( $message ){
    if( is_wc_endpoint_url( 'edit-account' ) ){
        global $woocommerce;
        extract( $_POST );
            
        $message .= ' Your custom message';
    }
    return $message;
}

add_filter( 'woocommerce_add_notice', 'custom_wc_add_notice', 10, 1 );
add_filter( 'woocommerce_add_error', 'custom_wc_add_notice', 10, 1 );
add_filter( 'woocommerce_add_success', 'custom_wc_add_notice', 10, 1 );

Another thing is that when i change the error code to edit-password, it doesn't seem to work but when i use the my-account page the changes seem to work. Thanks, hoping to get some help.


Solution

  • There is a missing function that need to be hooked in woocommerce_get_query_vars filter hook, to be able to use is_wc_endpoint_url('edit-password') conditional function with your custom endpoint.

    Try the following code replacement (I have renamed your functions):

    add_filter ( 'woocommerce_account_menu_items', 'add_my_account_edit_password_menu_item', 40 );
    function add_my_account_edit_password_menu_item( $menu_links ){
        $menu_links = array_slice( $menu_links, 0, 5, true ) 
        + array( 'edit-password' => __('Edit Password', 'woocommerce') )
        + array_slice( $menu_links, 5, NULL, true );
         
        return $menu_links;
    }
    
    // HERE is the missing hooked function
    add_filter( 'woocommerce_get_query_vars', 'define_edit_password_query_var' );
    function define_edit_password_query_var( $query_vars ) {
        $query_vars['edit-password']  = 'edit-password';
        return $query_vars;
    }
    
    add_action( 'woocommerce_account_edit-password_endpoint', 'add_edit_password_endpoint_content' );
    function add_edit_password_endpoint_content() {
        ## The Edit pasword page content
    
        // For testing we print directly an error message
        wc_print_notice('This is an error message…', 'error');
    }
    
    add_action( 'init', 'add_edit_password_rewrite_endpoint' );
    function add_edit_password_rewrite_endpoint() {
        // WP_Rewrite is my Achilles' heel, so please do not ask me for detailed explanation
        add_rewrite_endpoint( 'edit-password', EP_PAGES );
    }
    

    Don't forget to Flush WordPress rewrite rules, by going to general Settings > Permalinks and click on "Save Changes".

    Now, if you use is_wc_endpoint_url('edit-password') conditional, it will work in the following (commented):

    add_filter( 'woocommerce_add_message', 'custom_woocommerce_notice' );
    add_filter( 'woocommerce_add_success', 'custom_woocommerce_notice' );
    add_filter( 'woocommerce_add_notice',  'custom_woocommerce_notice' );
    add_filter( 'woocommerce_add_error',   'custom_woocommerce_notice' );
    function custom_woocommerce_notice( $message ) {
        // My Account | Edit password pages
        if( is_wc_endpoint_url( 'edit-password' ) ){
            $message .= ' ' . __('"Edit password" custom message');
        }
        return $message;
    }
    

    Code goes in functions.php file of your child theme (or in a plugin).

    enter image description here