2
votes

I need to update the order item meta in a woocommerce oder on checkout page or while woocommerce creates the order. I'm using the plugin visual product configurator and it is not passing the right quantity of some items of the order to woocommerce order meta, especially when I use multiple variations on the same product.

Is there a hook for me to use to update the item quantity for a certain order item and how can I use it? The plugin returns me an array with all the cart information and I can only check if an item of the order appears multiple times - if yes I need to change the quantity of that item to that number in the woocommerce order/database.

I was thinking of adding the following hook to my functions.php

add_action('woocommerce_checkout_create_order', 'change_qty', 1,1);


function change_qty($item_qty){

  foreach($item_qty as $qty) {
    $qty['product_id'] = $id;
    $qty['qty'] = $new_qty
    $order->update_meta_data('quantity', $new_qty, $id)
  }
}

Whereas $item_qty is be an multi-dimensional array containing the item_ids and adjusted quantities.

Another problem I'm facing is that I dont know when I need to call that function because I get the array from the plugin on the checkout page, but I think WooCommerce has not yet created an order at that moment?

The result should be an adjusted item quantity in the woocommerce order summary in the backend.

2

2 Answers

1
votes

To update the order item quantity, you can use WC_Order_Item_Product set_quantity() method.

The correct hook to update order items (line items) is woocommerce_checkout_create_order_line_item action hook, that is triggered during order creation, before data is saved to databased.

add_action('woocommerce_checkout_create_order_line_item', 'change_order_line_item_quantity', 10, 4 );
function change_order_line_item_quantity( $item, $cart_item_key, $cart_item, $order ) {
    // Your code goes below

    // Get order item quantity
    $quantity = $item->get_quantity();

    $new_qty = $quantity + 2;

    // Update order item quantity
    $item->set_quantity( $new_qty );
}

The function arguments (variables) are defined and usable:

  • $item is the WC_Order_Item_Product Object (not saved yet to database)
  • $cart_item_key is the related cart item key
  • $cart_item is the related cart item data
  • $order is the WC_Order Object (not saved yet to database)

Related:

-2
votes

This can help you (we hook into payment completed notification from the payment provider). If you want to update the _qty just after the order was created, I can change my function. But for now I would update it only when the payment was successful.:

/**
 * Update order item qty after payment successful
 */
add_filter( 'woocommerce_payment_complete_order_status', 'update_order_item_qty', 10, 2 );
function update_order_item_qty( $order_status, $order_id ) {

    //Get the order and items
    $order = new WC_Order( $order_id );
    $items = $order->get_items();

    //New qty
    $new_qty = 0;

    foreach ( $items as $item_id => $item_data ) {
        update_meta_data( '_qty', $new_qty, $item_id );
    }
}

Please try if this is what you'r looking for.