/**
* Create or update a line item.
*
* @param array $posted Line item data.
* @param string $action 'create' to add line item or 'update' to update it.
* @throws WC_REST_Exception Invalid data, server error.
*/
protected function prepare_line_items($posted, $action = 'create')
{
$item = new WC_Order_Item_Product(!empty($posted['id']) ? $posted['id'] : '');
$product = wc_get_product($this->get_product_id($posted));
if ($product !== $item->get_product()) {
$item->set_product($product);
if ('create' === $action) {
$total = $product->get_price() * (isset($posted['quantity']) ? $posted['quantity'] : 1);
$item->set_total($total);
$item->set_subtotal($total);
}
}
$this->maybe_set_item_props($item, array('name', 'quantity', 'total', 'subtotal', 'tax_class'), $posted);
return $item;
}