WC_REST_Orders_V1_Controller::prepare_line_items
Create or update a line item.
Method of the class: WC_REST_Orders_V1_Controller{}
No Hooks.
Returns
WC_Order_Item_Product.
Usage
// protected - for code of main (parent) or child class $result = $this->prepare_line_items( $posted, $action );
- $posted(array) (required)
- Line item data.
- $action(string)
'create'to add line item or'update'to update it.
Default:'create'
WC_REST_Orders_V1_Controller::prepare_line_items() WC REST Orders V1 Controller::prepare line items code WC 10.8.1
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, $action ) );
if ( $product && $product !== $item->get_product() ) {
$item->set_product( $product );
if ( 'create' === $action ) {
$quantity = isset( $posted['quantity'] ) ? $posted['quantity'] : 1;
$total = wc_get_price_excluding_tax( $product, array( 'qty' => $quantity ) );
$item->set_total( $total );
$item->set_subtotal( $total );
}
}
$this->maybe_set_item_props( $item, array( 'name', 'quantity', 'total', 'subtotal', 'tax_class' ), $posted );
return $item;
}