有条件地显示 woocommerce 完成的订单

conditionally display woocommerce completed orders

目前,在我的网站上,有一个自定义用户配置文件字段(示例 midwife_practice,用户在注册时填写)与结帐时另一个字段(示例 midwife_practice 具有相同的下拉值_checkout).

我想做的是在前端显示所有订单,但是当用户访问此页面时,只完成字段值 'midwife_practice_checkout' 与当前用户的 'midwife_practice' 字段值匹配的订单应该显示。

到目前为止,我使用下面的代码完成了大部分工作。最后就是如何实现条件if(midwife_practice == midwife_practice_checkout)

        
        
         $args = array(
                    'status'       => 'completed', // Accepts a string: one of 'pending', 'processing', 'on-hold', 'completed', 'refunded, 'failed', 'cancelled', or a custom order status.
  );

        
        $orders = wc_get_orders( $args );
        
        
        
?>      <table id="past_myTable" class="shop_table shop_table_responsive my_account_orders my_account_appointments past">           
            <thead>
                <tr>
                    <th scope="col" class="appointment-id">
                        <span class="nobr"><?php esc_html_e( 'Appointment ID', 'woocommerce-appointments' ); ?></span>
                    </th>
                    <th scope="col" class="billing-name"><span class="nobr"><?php esc_html_e( 'Client Name', 'woocommerce-appointments' ); ?></span></th>
                    <th scope="col" class="appointment-when">
                        <span class="nobr"><?php esc_html_e( 'When', 'woocommerce-appointments' ); ?></span>
                    </th>
                    <th scope="col" class="scheduled-product">
                        <span class="nobr"><?php esc_html_e( 'Scheduled For', 'woocommerce-appointments' ); ?></span>
                    </th>
                    <th scope="col" class="appointment-status">
                        <span class="nobr"><?php esc_html_e( 'Status', 'woocommerce-appointments' ); ?></span>
                    </th>
                    <th scope="col" class="appointment-actions">
                    <span class="nobr"><?php esc_html_e( 'Action', 'woocommerce-appointments' ); ?></span>
                    </th>
                </tr>
            </thead>
                <tbody>
<?php               foreach ( $orders as $order ) {
        // Loop through all appointments of an order
    
    $order_id = $order->get_id();
    $billing_first_name = $order->get_billing_first_name();
    $billing_last_name  = $order->get_billing_last_name();
    $midwife_practice = get_post_meta( $order->get_id(), 'billing_midwifepractice', true ) . PHP_EOL;
    $current_user = wp_get_current_user();
    $current_user_midwife = $current_user->user_registration_select_1643878958;
    
            
        
                $appointments_ids = WC_Appointment_Data_Store::get_appointment_ids_from_order_id($order_id);

                    foreach($appointments_ids as $appointment_id) {
                                $appointment = get_wc_appointment($appointment_id);
                                $reschedule_url = $appointment->get_reschedule_url();
                                $duration = $appointment->get_duration();
                                $start_date = $appointment->get_start_date();
                                $cancel_btn = $appointment->get_cancel_url();
                                $status = $appointment->get_status();
                        if ( $appointment->get_product() && $appointment->get_product()->is_type( 'appointment' ) ) {
                                    $prod_name = $appointment->get_product_name();
                                }
                                
                    }
            
?>      <tr>
                    <td class="appointment-id anowrap" data-title="<?php esc_html_e( 'Appointment_id', 'woocommerce-appointments' ); ?>">
                        <?php
                            printf(
                                '#%d',
                                esc_attr( $order_id )
                            );
                                ?>
                    </td>
                    <td class="appointment-id anowrap" data-title="<?php esc_html_e( 'Client_name', 'woocommerce-appointments' ); ?>">
                            <span class="adesc"><?php esc_attr_e( $billing_first_name.' '.$billing_last_name ); ?></span>
                    </td>
                    <td class="appointment-id anowrap" data-title="<?php esc_html_e( 'When', 'woocommerce-appointments' ); ?>">
                            <?php esc_attr_e( $start_date ); ?>
                            <span class="adesc"><?php esc_attr_e( $duration ); ?></span>
                    </td>
                    <td class="appointment-id anowrap" data-title="<?php esc_html_e( 'Schedule', 'woocommerce-appointments' ); ?>">
                            <?php ?>
                                <a href="<?php echo esc_url( get_permalink( $appointment->get_product_id() ) ); ?>">
                                    <?php esc_html_e( $prod_name ); ?>
                                </a>
                            <?php ?>
                    </td>
                    <td class="appointment-id anowrap" data-title="<?php esc_html_e( 'Status', 'woocommerce-appointments' ); ?>">
                            <?php esc_html_e( wc_appointments_get_status_label( $status ) ); ?>
                    </td>
                    <td class="appointment-id anowrap" data-title="<?php esc_html_e( 'Status', 'woocommerce-appointments' ); ?>">
                            <?php if ( 'cancelled' !== $appointment->get_status() && 'completed' !== $appointment->get_status() && ! $appointment->passed_cancel_day() ) : ?>
                                <a href="<?php echo esc_url( $appointment->get_cancel_url() ); ?>" class="woocommerce-button button anowrap cancel">
                                    <?php esc_html_e( 'Cancel', 'woocommerce-appointments' ); ?>
                                </a>
                            <?php endif ?>
                            <?php if ( 'cancelled' !== $appointment->get_status() && 'completed' !== $appointment->get_status() && ! $appointment->passed_reschedule_day() ) : ?>
                                <a href="<?php echo esc_url( $appointment->get_reschedule_url() ); ?>" class="woocommerce-button button anowrap reschedule">
                                    <?php esc_html_e( 'Reschedule', 'woocommerce-appointments' ); ?>
                                </a>
                            <?php endif ?>
                    </td>

            
        </tr>   
                    <?php
}   
?>              
    </tbody>
</table>

<?php       
        
        }
add_shortcode('conditionalorder', 'Customorder'); 

谢谢

感谢@7uc1f3r

,我使用 meta_keymeta_value 解决了所有问题
  
    $current_user = wp_get_current_user();
        $current_user_midwife = $current_user->user_registration_select_1643878958;


        
         $args = array(
                    'status'       => 'completed',
                    'meta_key' => '_billing_midwifepractice',
                    'meta_value' => $current_user_midwife,
                    'compare' => '='
  );

        
        $orders = wc_get_orders( $args );
        
        
        
?>      <table id="past_myTable" class="shop_table shop_table_responsive my_account_orders my_account_appointments past">           
            <thead>
                <tr>
                    <th scope="col" class="appointment-id">
                        <span class="nobr"><?php esc_html_e( 'Appointment ID', 'woocommerce-appointments' ); ?></span>
                    </th>
                    <th scope="col" class="billing-name"><span class="nobr"><?php esc_html_e( 'Client Name', 'woocommerce-appointments' ); ?></span></th>
                    <th scope="col" class="appointment-when">
                        <span class="nobr"><?php esc_html_e( 'When', 'woocommerce-appointments' ); ?></span>
                    </th>
                    <th scope="col" class="scheduled-product">
                        <span class="nobr"><?php esc_html_e( 'Scheduled For', 'woocommerce-appointments' ); ?></span>
                    </th>
                    <th scope="col" class="appointment-status">
                        <span class="nobr"><?php esc_html_e( 'Status', 'woocommerce-appointments' ); ?></span>
                    </th>
                    <th scope="col" class="appointment-actions">
                    <span class="nobr"><?php esc_html_e( 'Action', 'woocommerce-appointments' ); ?></span>
                    </th>
                </tr>
            </thead>
                <tbody>
<?php               foreach ( $orders as $order ) {
        // Loop through all appointments of an order
    
    $order_id = $order->get_id();
    $billing_first_name = $order->get_billing_first_name();
    $billing_last_name  = $order->get_billing_last_name();
    $midwife_practice = get_post_meta( $order->get_id(), 'billing_midwifepractice', true ) . PHP_EOL;
    $current_user = wp_get_current_user();
    $current_user_midwife = $current_user->user_registration_select_1643878958;
    
            
        
                $appointments_ids = WC_Appointment_Data_Store::get_appointment_ids_from_order_id($order_id);

                    foreach($appointments_ids as $appointment_id) {
                                $appointment = get_wc_appointment($appointment_id);
                                $reschedule_url = $appointment->get_reschedule_url();
                                $duration = $appointment->get_duration();
                                $start_date = $appointment->get_start_date();
                                $cancel_btn = $appointment->get_cancel_url();
                                $status = $appointment->get_status();
                        if ( $appointment->get_product() && $appointment->get_product()->is_type( 'appointment' ) ) {
                                    $prod_name = $appointment->get_product_name();
                                }
                                
                    }
            
?>      <tr>
                    <td class="appointment-id anowrap" data-title="<?php esc_html_e( 'Appointment_id', 'woocommerce-appointments' ); ?>">
                        <?php
                            printf(
                                '#%d',
                                esc_attr( $order_id )
                            );
                                ?>
                    </td>
                    <td class="appointment-id anowrap" data-title="<?php esc_html_e( 'Client_name', 'woocommerce-appointments' ); ?>">
                            <span class="adesc"><?php esc_attr_e( $billing_first_name.' '.$billing_last_name ); ?></span>
                    </td>
                    <td class="appointment-id anowrap" data-title="<?php esc_html_e( 'When', 'woocommerce-appointments' ); ?>">
                            <?php esc_attr_e( $start_date ); ?>
                            <span class="adesc"><?php esc_attr_e( $duration ); ?></span>
                    </td>
                    <td class="appointment-id anowrap" data-title="<?php esc_html_e( 'Schedule', 'woocommerce-appointments' ); ?>">
                            <?php ?>
                                <a href="<?php echo esc_url( get_permalink( $appointment->get_product_id() ) ); ?>">
                                    <?php esc_html_e( $prod_name ); ?>
                                </a>
                            <?php ?>
                    </td>
                    <td class="appointment-id anowrap" data-title="<?php esc_html_e( 'Status', 'woocommerce-appointments' ); ?>">
                            <?php esc_html_e( wc_appointments_get_status_label( $status ) ); ?>
                    </td>
                    <td class="appointment-id anowrap" data-title="<?php esc_html_e( 'Status', 'woocommerce-appointments' ); ?>">
                            <?php if ( 'cancelled' !== $appointment->get_status() && 'completed' !== $appointment->get_status() && ! $appointment->passed_cancel_day() ) : ?>
                                <a href="<?php echo esc_url( $appointment->get_cancel_url() ); ?>" class="woocommerce-button button anowrap cancel">
                                    <?php esc_html_e( 'Cancel', 'woocommerce-appointments' ); ?>
                                </a>
                            <?php endif ?>
                            <?php if ( 'cancelled' !== $appointment->get_status() && 'completed' !== $appointment->get_status() && ! $appointment->passed_reschedule_day() ) : ?>
                                <a href="<?php echo esc_url( $appointment->get_reschedule_url() ); ?>" class="woocommerce-button button anowrap reschedule">
                                    <?php esc_html_e( 'Reschedule', 'woocommerce-appointments' ); ?>
                                </a>
                            <?php endif ?>
                    </td>

            
        </tr>   
                    <?php
}   
?>              
    </tbody>
</table>

<?php       
        
        }
add_shortcode('conditionalorder', 'Customorder');