搜索

首页  >  问答  >  正文

如果购买总金额为0,则隐藏WooCommerce我的账户订单

<p>在woocommerce中,我试图隐藏订单,如果订单总额为0。 以下是我的代码,但它不起作用。对于问题有什么想法吗?</p> <pre class="brush:php;toolbar:false;">add_filter( 'woocommerce_my_account_my_orders_query', 'hide_zero_total_orders_from_my_account', 10, 1 ); function hide_zero_total_orders_from_my_account( $args ) { $args['meta_query'] = array( array( 'key' => '_order_total', 'value' => 0, 'compare' => '>', 'type' => 'NUMERIC', ), ); return $args; }</pre> <p><br /></p>
P粉545956597P粉545956597468 天前532

全部回复(1)我来回复

  • P粉037450467

    P粉0374504672023-08-18 00:21:58

    您可以使用轻量级的SQL查询来获取客户的总购买金额,并使用该查询来隐藏“我的账户订单”部分,如果总购买金额等于0(零):

    // 获取客户购买总金额
    function get_customer_purchases_total_amount(){
        global $wpdb;
    
        return (float) $$wpdb->get_var( $wpdb->prepare( "
            SELECT SUM(pm.meta_value)
            FROM {$wpdb->prefix}posts as p
            INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
            INNER JOIN {$wpdb->prefix}postmeta as pm2 ON p.ID = pm2.post_id
            WHERE p.post_type = 'shop_order'
            AND p.post_status IN ('wc-processing','wc-completed')
            AND pm.meta_key = '_order_total'
            AND pm2.meta_key = '_customer_user'
            AND pm2.meta_value = %d
        ", get_current_user_id() ) );
    }
    
    // 有条件地隐藏“我的账户订单”部分
    add_filter( 'woocommerce_account_menu_items', 'hide_customer_account_orders_conditionally', 100, 1 );
    function hide_customer_account_orders_conditionally( $items ) {
        if ( get_customer_purchases_total_amount() == 0 ) {
            unset( $items['orders'] );
        }
        return $items;
    }
    

    将代码放在您的子主题的functions.php文件中(或插件中)。已测试并可正常运行。

    回复
    0
  • 取消回复