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檔案中(或外掛程式中)。已測試並可正常運作。