Jussi
Jussi

Reputation: 65

woocommerce_process_shop_order_meta not firing when bulk editing orders?

I've made a simple function to be ran when a Woocommerce order is edited by the admins:

add_action( 'woocommerce_process_shop_order_meta', 'myfunction' );

myfunction works just fine when I edit a single order. It checks if an order included a specific product, and if product is found in order, some data is added to MailChimp.

When I bulk edit my orders, specifically change order status to "Completed", the hook seems to not be running. Orders are changed to status "completed", but no data goes to MailChimp.

Here is myfunction just in case, but I suspect this is an issue with woocommerce_process_shop_order_meta not running in bulk edit mode.

function myfunction( $order_id ){
    $order = wc_get_order( $order_id );
    $items = $order->get_items(); 
    foreach ( $items as $item_id => $item ) {
        $product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
        if ( $product_id === 4472 ) {
            $email = $order->get_billing_email();
            $first_name = $order->get_billing_first_name();
            $last_name = $order->get_billing_last_name();
            sample_order_mailchimp($email, $first_name, $last_name);
            break;
        }
    }
}

Am I missing something obvious here?

Upvotes: 1

Views: 4088

Answers (1)

Jussi
Jussi

Reputation: 65

So the way the hooks work is: woocommerce_process_shop_order_meta does not fire when bulk editing orders.

What you can use instead, on bulk editing of Woocommerce orders, is the hook called handle_bulk_actions-edit-shop_order.

I think Woocommerce really should do a far better job of documenting all of this. I simply had to figure it out by trial and error. Here's the final solution:

add_filter( 'handle_bulk_actions-edit-shop_order', 'myfunction', 10, 3 ); // the last two parameters are critically important. They specify: 10 for priority, 3 for how many arguments you pass to your function! Without this, you can't process the order ids
function myfunction( $redirect_to, $action, $order_ids ) {
    if ( $action === 'mark_completed' ) {
       // Do your magics here. This is only fired if the bulk edit action was to mark the orders completed. You can change that as you need.
    }
}

Upvotes: 3

Related Questions