Woocommerce update Order line items subtotals

Viewed 65

I am trying to update the price of the items in a Bundled product as follows:

add_action('woocommerce_checkout_create_order_line_item', array($this, 'misha_add_order_item_meta'), 10, 4);
 public function misha_add_order_item_meta($item, $cart_item_key, $values, $order)
{ if (isset($values['cartbundles'])) {
        foreach ($values['cartbundles'] as $key => $value) {
            $item->update_meta_data('_line_subtotal', $value['line_subtotal']);
        }
    }}

The Reason i am trying to update the "_line_subtotal" is because Woocommerce only update the Bundle but not the line items in the Bundle.

The order looks as follows: enter image description here

SO, far nothing happens, the Cost and Total column is still Zero.

when i try:

foreach ($order->get_items() as $item_id => $item) {}

The item_id and item dont give me any response even empty array or something.

I would like to store the price of the item as part of the order.

The table looks as follows: enter image description here

1 Answers

I found the Solution:

use the woocommerce_new_order_item hook as follows:

If you create the hook within a class construct use the hook as follows:

add_action('woocommerce_new_order_item', array($this, 'custom_woocommerce_new_order_item'), 10, 3);

If you create the hook outside a class construct use the hook as follows:

add_action('woocommerce_new_order_item', 'custom_woocommerce_new_order_item', 10, 3);

Then respectively create a function within class or in your preferred theme functions.php file (or other) as follows:

function custom_woocommerce_new_order_item($item_id, $item, $order_id){
// Get Order
$order = !empty($order) ? $order : wc_get_order($order_id);
if (!$order || !is_a($order, '\WC_Order')) {
    return;
}
// DO YOUR STUFF FROM HERE...
// Does my Order have Bundled items? (depends with plugin you use for bundles)
$is_product_bundle = false;
foreach ($order->get_items() as $item_id => $item_values) {//go through the  order
    $product_id = (!empty($item_values['variation_id']) ? $item_values['variation_id'] : $item_values['product_id']);//get product id
    if (get_post_meta($product_id, '_wcpb_bundles_product', true)) { //depends with plugin or method you get to know if a product is a bundle
        $is_product_bundle = true;
    }
}

if ($is_product_bundle) { //my Order has Bundled items

    // Go through Order items and manipulate to your liking
    foreach ($order->get_items() as $item_id => $item) {

        //Get product Id of the item
        $product_id = (!empty($item['variation_id']) ? $item['variation_id'] : $item['product_id']);

        //Get price of the item
        $price = ($product = wc_get_product($product_id)) ? $product->get_price() : 0;

        //Update Line Subtotal for each Item in the bundle
        if (wc_get_order_item_meta($item_id, '_line_subtotal') == 0 || wc_get_order_item_meta($item_id, '_line_subtotal') == '') {
            wc_update_order_item_meta($item_id, '_line_subtotal', $price);
        }

        //Update Line Total for each Item in the bundle (Quantity * Price)
        if (wc_get_order_item_meta($item_id, '_line_total') == 0 || wc_get_order_item_meta($item_id, '_line_total') == '') {
            $qty = wc_get_order_item_meta($item_id, '_qty');
            $qty = ($qty != 0 || !empty($qty)) ? $qty : 1;
            wc_update_order_item_meta($item_id, '_line_total', ($price * $qty));
        }
    }
}}
Related