WC_REST_Orders_V2_Controller::prepare_line_items()
Create or update a line item.
Method of the class: WC_REST_Orders_V2_Controller{}
No Hooks.
Return
WC_Order_Item_Product
.
Usage
// protected - for code of main (parent) or child class $result = $this->prepare_line_items( $posted, $action, $item );
- $posted(array) (required)
- Line item data.
- $action(string)
- 'create' to add line item or 'update' to update it.
Default: 'create' - $item(object)
- Passed when updating an item. Null during creation.
Default: null
WC_REST_Orders_V2_Controller::prepare_line_items() WC REST Orders V2 Controller::prepare line items code WC 7.7.0
protected function prepare_line_items( $posted, $action = 'create', $item = null ) { $item = is_null( $item ) ? new WC_Order_Item_Product( ! empty( $posted['id'] ) ? $posted['id'] : '' ) : $item; $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 ); $this->maybe_set_item_meta_data( $item, $posted ); return $item; }