Hannah James
Hannah James

Reputation: 570

How to skip orders having specific meta in admin bulk order action

I am creating Bulk upload shipments in woocommerce. I created a order status name trax-booked and in order admin we can bulk select the order to submit shipments in courier platform.

This is my code to get the orders data:

<?php
    $count = 0;
    $skip = 0;

    foreach($object_ids as $orderId) {
        $getOrder = new WC_Order($orderId);
        $getOrder = $getOrder->get_data();
    
        if($getOrder['status'] == 'trax-booked') {
            $skip++;
        
            if(count($object_ids) == $skip) {
                wp_redirect(wp_get_referer());
                die;
            }
            continue;
        }
    
        $count++;
        $itemDesc = '';
        $itemQty = 0;
    
        //Get Order Info
        $order = new WC_Order($orderId);
        $orderData = $order->get_data();
    
        $first_name = $orderData['shipping']['first_name'];
        $address_1 = $orderData['shipping']['address_1'];
        $phone = $orderData['shipping']['phone'];
        $email = $orderData['shipping']['email'];
        $city = $orderData['shipping']['city'];

?>

Here I want to prevent orders for duplicate booking. So when an order is succesfully booked already added this line

update_post_meta( $order->id, '_dvs_courier_check', '1' );

So I want to add a check that when I select multiple orders and use the bulk edit function so orders that having _dvs_courier_check value = 1 will skip and do not upload to the courier platform. Please help how to do this

Upvotes: 1

Views: 53

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254483

There are some mistakes in your code… To skip orders that have _dvs_courier_check as meta data, try the following:

<?php
    $count = $skip = 0; // Initializing

    foreach($object_ids as $order_id ) {
        $order      = wc_get_order($order_id); // Get WC_Order Object
        $order_data = $order->get_data(); // Get default order meta data as an unprotected array
       
        // Skip orders that have '_dvs_courier_check' meta data with value "1"
        if( $order->get_meta('_dvs_courier_check') != 1 ) {

            if( $order->get_status() === 'trax-booked' ) {
                $skip++;
            
                if( count($object_ids) == $count ) {
                    wp_redirect( wp_get_referer() );
                    die;
                }
                continue;
            }
        
            $count++;
            $itemDesc = '';
            $itemQty  = 0;
        
            $first_name = $order->get_shipping_first_name();
            $address_1  = $order->get_shipping_address_1'];
            $phone      = $order->get_billing_phone(); // <= billing phone (shipping phone doesn't exist by default)
            $email      = $order->get_billing_email(); // <= billing email (shipping email doesn't exist by default)
            $city       = $order->get_shipping_city();

            // ... / ...
        }
    }
?>

It should work.

Upvotes: 2

Related Questions