【问题标题】:Display custom content for a custom account menu item in Woocommerce 3在 Woocommerce 3 中显示自定义帐户菜单项的自定义内容
【发布时间】:2018-11-02 15:52:09
【问题描述】:

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

Custom cart notice based on user total purchased amount in Woocommerce

它不像我想的那样工作。我做错了什么?

这是我使用的代码:

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

感谢任何帮助。

【问题讨论】:

    标签: php wordpress woocommerce account items


    【解决方案1】:

    请在 StackOverFlow 中询问时,在函数名称、变量和文本中使用真正的英语,因为这是针对以英语为语言的大型社区。尽量给出明确的名字。

    要使自定义菜单项的内容显示,您需要刷新重写规则

    为此,请转到 WordPress 设置 > 永久链接...并单击“保存更改”。 现在您的内容将会出现。

    这是您重新访问的代码(格式清晰),并添加了一些删除谢谢页面上的会话值:

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

    此代码位于您的活动子主题(或主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-17
      • 2017-01-27
      • 2021-07-15
      • 1970-01-01
      • 2019-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多