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
Check this
There is a missing function that need to be hooked in
woocommerce_get_query_vars
filter hook, to be able to useis_wc_endpoint_url('edit-password')
conditional function with your custom endpoint.Try the following code replacement (I have renamed your functions):
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):Code goes in functions.php file of your child theme (or in a plugin).