Ad

Conditionally Display Woocommerce Completed Orders

currently, on my website, there is a custom user profile field (example midwife_practice, users fill this during registration) that has the same dropdown value as another field during checkout (example midwife_practice_checkout).

what I want to do is display all orders in the frontend but when a user visits this page, only completed orders that the field value 'midwife_practice_checkout' matches the 'midwife_practice' field value of the current user should be displayed.

so far, I got most of the job done with the code below. the last thing is how to implement the condition 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'); 

thanks

Ad

Answer

I got it all figured out using meta_key and meta_value thanks to @7uc1f3r

  
    $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'); 
Ad
source: stackoverflow.com
Ad