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
My Answer:
You can add a check for the _dvs_courier_check meta value in your loop before processing the order. Here's how you can modify your code to skip orders with _dvs_courier_check value = 1:
php
$count = 0;
$skip = 0;
foreach($object_ids as $orderId) {
$getOrder = new WC_Order($orderId);
$getOrder = $getOrder->get_data();
// Check if order has _dvs_courier_check meta value set to 1
$courier_check = get_post_meta($orderId, '_dvs_courier_check', true);
if($courier_check == '1') {
$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'];
// Add your code to upload shipment to courier platform here
}
?>
This code will check if the _dvs_courier_check meta value is set to 1 for each order before processing it. If the meta value is 1, the order will be skipped and not uploaded to the courier platform.
Rate this post
5 of 5 based on 8828 votesComments