简体   繁体   English

在Woocommerce 3中显示自定义帐户菜单项的自定义内容

[英]Display custom content for a custom account menu item in Woocommerce 3

I am trying to display a custom notice in my custom account menu element based on user total purchased amount in Woocommerce, based on this answer code: 我试图根据以下答案代码,根据用户在Woocommerce中购买的总金额在自定义帐户菜单元素中显示自定义通知:

Custom cart notice based on user total purchased amount in Woocommerce 基于用户在Woocommerce中购买的总金额的自定义购物车通知

It does not work as I would like. 它没有我想要的工作。 What I am doing wrong? 我做错了什么?

This is the code that I use: 这是我使用的代码:

add_filter ( 'woocommerce_account_menu_items', 'xu', 40 );
function xu( $menu_links ){

$menu_links = array_slice( $menu_links, 0,3 , true ) 
+ array( 'xu' => 'Xu của bạn' )
+ array_slice( $menu_links, 3, NULL, true );

return $menu_links;

 }

add_action( 'init', 'add_endpoint' );
 function add_endpoint() {

add_rewrite_endpoint( 'xu', EP_PAGES );

}

add_action( 'woocommerce_account_xu_endpoint', 'xuxu' );
 function xuxu() {

 if( ! WC()->session->get( 'purchases_sum' ) ){
        WC()->session->set('purchases_sum', 
  get_customer_total_purchases_sum());
    }

    $total_purchases  = WC()->session->get( 'purchases_sum' );

    if ( $total_purchases == 0 ) return; // We exit (no purchases or non logged users)

    if ( ( 10000 - $total_purchases ) > 0 )
    {

        echo 'You need an extra ' . wc_price( 10000 - $total_purchases ) . ' at all to get a... ';

    }
    else
    {
        echo '... ';
    }
}

Any help is appreciated. 任何帮助表示赞赏。

Please when asking in StackOverFlow, use real english in your function names, variables and text as this is for a large community where english is the language. 在StackOverFlow中询问时,请在函数名称,变量和文本中使用纯英文,因为这是针对以英语为母语的大型社区的。 Try always to give explicit names. 尝试始终给出明确的名称。

To make the content appear for your custom menu item, you need to refresh rewrite rules . 要使内容显示在自定义菜单项中, 您需要刷新重写规则

For that go to WordPress Settings > Permalinks… And click on "Save changes". 为此,请转到WordPress设置>永久链接…,然后单击“保存更改”。 Now your content will appear. 现在,您的内容将会出现。

Here is your revisited code (clean formatted) with some additions to remove the session value on thankyou page: 这是您重新访问的代码(格式干净),其中有一些附加内容可以删除感谢页面上的会话值:

// Utililty function to get customer's total purchases sum
function get_customer_total_purchases_sum() {
    $current_user_id = get_current_user_id(); // Current user ID

    if( $current_user_id == 0 ) return 0; // we return zero if customer is not logged in

    global $wpdb;

    // return the SQL query (paid orders sum)
    return $wpdb->get_var("SELECT SUM(pm.meta_value) FROM {$wpdb->prefix}postmeta as pm
    INNER JOIN {$wpdb->prefix}postmeta as pm2 ON pm.post_id = pm2.post_id
    INNER JOIN {$wpdb->prefix}posts as p ON pm.post_id = p.ID
    WHERE p.post_status LIKE 'wc-completed' AND p.post_type LIKE 'shop_order'
    AND pm.meta_key LIKE '_order_total' AND pm2.meta_key LIKE '_customer_user'
    AND pm2.meta_value LIKE '$current_user_id'");
}

add_filter ( 'woocommerce_account_menu_items', 'custom_account_menu_items', 10 );
function custom_account_menu_items( $menu_links ){

    $menu_links = array_slice( $menu_links, 0,3 , true )
    + array( 'rewards' => 'Rewards' )
    + array_slice( $menu_links, 3, NULL, true );

    return $menu_links;

}

add_action( 'init', 'add_rewards_account_endpoint' );
function add_rewards_account_endpoint() {
    add_rewrite_endpoint( 'rewards', EP_PAGES );
}

add_action( 'woocommerce_account_rewards_endpoint', 'rewards_account_endpoint_content' );
function rewards_account_endpoint_content() {

    if( ! WC()->session->get( 'purchases_sum' ) ){
        WC()->session->set('purchases_sum', get_customer_total_purchases_sum());
    }

    $total_purchases  = WC()->session->get( 'purchases_sum' );

    if ( $total_purchases == 0 ) return; // We exit (no purchases or non logged users)

    if ( ( 10000 - $total_purchases ) > 0 )
    {

        echo 'You need an extra ' . wc_price( 10000 - $total_purchases ) . ' at all to get a... ';

    }
    else
    {
        echo '... ';
    }
}

// Removing the purchase_sum session value on thankyou page.
add_action( 'template_redirect', 'removing_purchases_sum_session' );
function removing_purchases_sum_session( ) {

    if ( is_wc_endpoint_url('order-received') && WC()->session->get( 'purchases_sum' ) ) {
        // We remove this session variable in thankyou page (if it still exist)
        WC()->session->__unset( 'purchases_sum' );
    }
}

This code goes on function.php file of your active child theme (or theme). 这段代码将放在您活动的子主题(或主题)的function.php文件中。 Tested and works. 经过测试和工作。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在WooCommerce中定义用户变量我的账户自定义菜单项内容 - Define user variable in WooCommerce My account custom menu item content WooCommerce 我的帐户自定义端点菜单项 - WooCommerce My Account custom endpoint menu item WooCommerce 我的帐户页面自定义菜单项与自定义链接 - WooCommerce My Account page custom menu item with custom link 在 Woocommerce 我的帐户菜单中的注销项上方添加自定义项 - Add a custom item above logout item in Woocommerce my account menu 在 WooCommerce 中添加基于用户角色的自定义我的帐户菜单项 3+ - Add custom my account menu item based on user role in WooCommerce 3+ 在 WooCommerce 中显示自定义计算的购物车商品价格 - Display a custom calculated cart item price in WooCommerce 在Woocommerce中获取作为“自定义我的帐户”页面内容分页的未购买商品 - Get unpurchased items paginated as Custom My account page content in Woocommerce 仅在 WooCommerce 我的帐户仪表板中添加自定义 html 内容 - Add custom html content in WooCommerce My Account Dashboard only 如何在woocommerce的帐户详细信息页面中显示自定义用户注册详细信息 - How to Display custom user registration details in Account Details page in woocommerce 在Woocommerce中每个商品名称旁边的购物车中显示产品自定义字段 - Display product custom fields in cart next to each item name in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM