Woocommerce 添加错误和通知在自定义 Woocommerce 我的帐户页面上不起作用

问题描述 投票:0回答:1

我一直在为我的 woocommerce myaccount 页面创建一个错误代码,我还成功地将 woocommerce 端点添加到所述自定义页面,但是,当我尝试使用命令 woocommerce_add 时,它在自定义页面上不起作用,但可以在基本 woocommerce 端点(例如 my-account)上工作。谢谢!

自定义我的帐户页面注册:(名为编辑密码)

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
}

自定义错误代码:

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 );

另一件事是,当我将错误代码更改为编辑密码时,它似乎不起作用,但当我使用我的帐户页面时,更改似乎起作用。谢谢,希望能得到一些帮助。

php wordpress woocommerce hook-woocommerce custom-wordpress-pages
1个回答
0
投票

检查这个

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 );
© www.soinside.com 2019 - 2024. All rights reserved.