0

Working with WooCommerce, I have some custom code that moves custom data from cart items to line items. This is what I'm doing:

add_action('woocommerce_checkout_create_order_line_item', 'tls_checkout_create_order_line_item', 10, 4 );
function tls_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
    if (isset($values['custom_data']['tls_associated_line_item'])) {
        $item->update_meta_data('_tls_associated_order_item', $values['custom_data']['tls_associated_line_item']);
    }
    else{
        $item->update_meta_data('_tls_item_id', $cart_item_key);
    }
}

The order items are updated appropriately, but this code prevents the cart from clearing when the order is completed (maybe because the hash is changing?). Is there any way to update order item meta without causing this interruption?

dubloons
  • 1,136
  • 2
  • 12
  • 25
  • I would use woocommerce_checkout_update_order_meta instead :) You want to update meta not add line item - read more about it here - https://stackoverflow.com/questions/48277513/difference-between-order-line-item-and-order-meta-in-wc-checkout-hooks . With your hook i would limit to only add_meta_data if need to add new meta but not use for update. – Snuffy Oct 27 '22 at 06:45
  • @MartinMirchev - I don't think I have what I need from that hook. I need access to the cart items to copy over the custom data. That hook only has order information. The hook I'm using is working, and I'm only adding meta. For some reason, I haven't been able to figure out, it's preventing the cart from clearing after completing an order. – dubloons Oct 27 '22 at 15:50
  • Like i said woocommerce_checkout_create_order_line_item is for creating items and you should avoid for updating. " I don't think I have what I need from that hook" ? What do you mean you dont have ? Returns order from which you can edit items. Ofc you can always run WC()->cart->empty_cart( true ); at the end of your function but that is not the way it should be :) – Snuffy Oct 31 '22 at 10:44

0 Answers0