自定义通知在 Woocommerce 我的帐户自定义端点页面上不起作用

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

我一直在努力创建适用于 WooCommerce“我的帐户”页面的错误代码。我还成功地将 WooCommerce 端点添加到此自定义页面,但是,当我尝试使用复合钩子

"woocommerce_add_{$notice_type}"
时,它无法在自定义页面上工作,但可以在基本 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 endpoint
2个回答
0
投票

缺少能够将

is_wc_endpoint_url()
条件函数与自定义端点“编辑密码”一起使用的函数。

需要满足以下条件:

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

现在,如果您使用

is_wc_endpoint_url('edit-password')
条件,它就会起作用。

尝试以下(评论)

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_wc_add_notice( $message ) {
    // My Account 'edit-account' endpoint page
    if( is_wc_endpoint_url( 'edit-account' ) ){
        extract($_POST);
        $message .= ' ' . __('"Edit Account" custom message');
    }
    // My Account 'edit-password' endpoint 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;
}

此外,您应该更明确地重命名其他函数,例如:

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' )
    + array_slice( $menu_links, 5, NULL, true );
     
    return $menu_links;
}

add_action( 'woocommerce_account_edit-password_endpoint', 'add_account_edit_password_endpoint_content' );
function add_account_edit_password_endpoint_content() {
    #some code for separation

    // Just 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 );
}

不要忘记刷新 WordPress 重写规则,方法是转到常规设置 > 永久链接并单击“保存更改”。


-2
投票

检查这个

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.