skip to Main Content

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.

2

Answers


  1. Check this

    function custom_wc_add_notice( $message ){
    if( is_wc_endpoint_url( 'edit-password' ) ){
        // Add your custom error message here
        $message .= ' Your custom message';
    }
    return $message;
    }
    
    // Hook the custom error message function to the appropriate hooks
    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 );
    
    Login or Signup to reply.
  2. 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;
    }
    
    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() {
        // 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 account pages
        if( is_wc_endpoint_url( 'edit-account' ) ){
            extract($_POST);
            $message .= ' ' . __('"Edit Account" custom message');
        }
        // My Account | Edit password page
        elseif( is_wc_endpoint_url( 'edit-password' ) ){
            $message .= ' ' . __('"Edit password" custom message');
        }
        // All My Account pages
        elseif( is_account_page() ){
            $message .= ' ' . __('"My account section" custom message');
        }
        return $message;
    }
    

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

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search