在 Woocommerce 3 中显示自定义帐户菜单项的自定义内容 [英] Display custom content for a custom account menu item in Woocommerce 3

查看:30
本文介绍了在 Woocommerce 3 中显示自定义帐户菜单项的自定义内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据用户在 Woocommerce 中的总购买量,根据此答案代码在我的自定义帐户菜单元素中显示自定义通知:

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:

Custom cart notice based on user total purchased amount in 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. 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". 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). Tested and works.

这篇关于在 Woocommerce 3 中显示自定义帐户菜单项的自定义内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆