I'm getting all the processing orders and then I need to iterate through each order to perform certain actions, the only way I have found to do this is to use a nested loop:
foreach ( $processing_orders as $order ) {
foreach( $order->get_items() as $item ) {
//Do stuff
}
}
Our woocommerce store has grown incredibly, and now this $processing_orders
array has thousand of processing orders, we have a performance bottleneck here, and I'm worried about the scalability of this in the future, I wonder if there's a better way to do this without the need to use a nested loop.
UPDATE As requested Im adding the full code:
global $woocommerce;
// Create a mailer
$mailer = $woocommerce->mailer();
$processing_orders = (array) wc_get_orders( array(
'limit' => -1,
'status' => ['wc-processing','wc-follow-up','wc-follow-up-2','wc-follow-up-3','wc-follow-up-4'],
'type' => 'shop_order',
'date_created' => '>'. $from
) );
if ( sizeof($processing_orders) > 0 ) {
$reminder_text = __("Notificación: fecha de envío enviada al proveedor $today", "woocommerce");
foreach ( $processing_orders as $order ) {
foreach( $order->get_items() as $item ) {
$product_id = $item->get_product_id();
$current_shipping_days = get_post_meta( $product_id, '_shipping_days_field', true );
$current_shipping_days = (int)$current_shipping_days;
$product_shipping_date = convertDateTimeImmutable($order->get_date_created())->modify( "+ $current_shipping_days weekdays" )->format( 'd/m/y' );
$days_until_shipping = $date2->diff($date1)->format("%a"); //find difference
$days = intval($diff);
$product_post_object = get_post( $product_id );
$vendor_id = $product_post_object->post_author;
$vendor_info = get_userdata( $vendor_id );
$vendor_email = $vendor_info->user_email;
$order_url = esc_url( add_query_arg( array(
'order_id' => $order->get_order_number(),
'_view_mode' => 'email',
'permission' => '1',
), dokan_get_navigation_url( 'orders' ) ) );
switch ($days) {
case 3:
$message_body = '<p>'.sprintf( __("Faltan 3 días para que se cumpla la fecha de entrega de tu pedido: %s"),
'<a href="'.esc_url( $order_url ).'">#'.$order->get_order_number().'</a>'
) .'</p>';
$message = $mailer->wrap_message(
// Message head and message body.
sprintf( '<h2>'. __( 'Faltan 3 días para que se cumpla la fecha de entrega del pedido %s' ) . '</h2>', $order->get_order_number() ), $message_body );
// Cliente email, email subject and message.
$mailer->send( $vendor_email, sprintf( __( 'Aviso: pedido #%s, faltan 3 días para que se cumpla la fecha de entrega' ), $order->get_order_number() ), $message );
break;
case 2:
$message_body = '<p>'.sprintf( __("Faltan 2 días para que se cumpla la fecha de entrega de tu pedido: %s"),
'<a href="'.esc_url( $order_url ).'">#'.$order->get_order_number().'</a>'
) .'</p>';
$message = $mailer->wrap_message(
// Message head and message body.
sprintf( '<h2>'. __( 'Faltan 2 días para que se cumpla la fecha de entrega del pedido %s' ) . '</h2>', $order->get_order_number() ), $message_body );
// Cliente email, email subject and message.
$mailer->send( $vendor_email, sprintf( __( 'Aviso: pedido #%s, faltan 2 días para que se cumpla la fecha de entrega' ), $order->get_order_number() ), $message );
break;
case 1:
$message_body = '<p>'.sprintf( __("Falta 1 día para que se cumpla la fecha de entrega del pedido: %s"),
'<a href="'.esc_url( $order_url ).'">#'.$order->get_order_number().'</a>'
) .'</p>';
$message = $mailer->wrap_message(
// Message head and message body.
sprintf( '<h2>'. __( 'Falta 1 día para que se cumpla la fecha de entrega del pedido %s' ) . '</h2>', $order->get_order_number() ), $message_body );
// Cliente email, email subject and message.
$mailer->send( $vendor_email, sprintf( __( 'Aviso: pedido #%s, falta 1 día para que se cumpla la fecha de entrega' ), $order->get_order_number() ), $message );
break;
case 0:
$message_body = '<p>'.sprintf( __("Hoy se cumple la fecha de entrega del pedido: %s"),
'<a href="'.esc_url( $order_url ).'">#'.$order->get_order_number().'</a>'
) .'</p>';
$message = $mailer->wrap_message(
// Message head and message body.
sprintf( '<h2>'. __( 'Aún no has enviado el pedido %s Hoy se cumple la fecha de entrega. Enviar los pedidos tarde tiene consecuencias.' ) . '</h2>', $order->get_order_number() ), $message_body );
// Cliente email, email subject and message.
$mailer->send( $vendor_email, sprintf( __( 'Aviso: pedido #%s, hoy se cumple la fecha para la entrega' ), $order->get_order_number() ), $message );
break;
case -1:
$message_body = '<p>'.sprintf( __("Ya se cumplió la fecha de entrega del pedido: %s"),
'<a href="'.esc_url( $order_url ).'">#'.$order->get_order_number().'</a>'
) .'</p>';
$message = $mailer->wrap_message(
// Message head and message body.
sprintf( '<h2>'. __( 'Aún no has enviado el pedido %s y ya se venció fecha de entrega. Enviar los pedidos tarde tiene consecuencias.' ) . '</h2>', $order->get_order_number() ), $message_body );
// Cliente email, email subject and message.
$mailer->send( $vendor_email, sprintf( __( 'Aviso: pedido #%s, se ha vencido el tiempo de entrega' ), $order->get_order_number() ), $message );
}
}
}
}
It's a bit long but I'm sending a remainder through email to the vendors when there are 3, 2, 1, and 0 days left to the shipping date of their orders.
An order has many sub-orders so I get all the "parent" processing orders and then I loop through each of their product items, then I get the "post_author" of the product to get the vendor and send him an email.
The above code runs daily.
Im using Dokan Multi-vendor plugin.