3

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
digitalsuite.net
  • 339
  • 6
  • 20

2 Answers2

1

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:

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thank you, I'm not quite understanding all variables in the function cahnge_order_line_item_quantity though, where do I have to declare them or are they already declared when I create an order? – digitalsuite.net Apr 10 '19 at 14:32
  • 1
    Function arguments (following variables) as `$item`, `$cart_item_key`, `$cart_item` and `$order` are defined and usable. – LoicTheAztec Apr 10 '19 at 14:37
  • Okay, I just tested it, it works. Do you have any reference or documentation where I can read about those Variables? I want to check if a product_id is the same as the one which is looping through in the function? I $item referring to the SKU, ProductID or something else? – digitalsuite.net Apr 10 '19 at 14:41
  • 1
    @erik7 I have added some links and explanation – LoicTheAztec Apr 10 '19 at 14:50
  • Thank you, I'm checking – digitalsuite.net Apr 10 '19 at 14:52
-2

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.

Mr. Jo
  • 4,946
  • 6
  • 41
  • 100
  • This doesnt work because I dont have the order id afterwards - I have the quantity information only during the checkout process, so I have to somehow change it during this process – digitalsuite.net Apr 10 '19 at 13:32
  • You have it afterwards. Every order get's through the payment completed status and at the point you know the order id. Or do you want to change the qty during the payment so that the customer get's more items than he see in the cart (maybe with a higher pricing)? – Mr. Jo Apr 10 '19 at 13:35
  • No its just for backend purposes so that we see the correct item quantity in the backend – digitalsuite.net Apr 10 '19 at 14:14