WC_REST_Orders_V2_Controller::prepare_line_items()protectedWC 1.0

Create or update a line item.

Метод класса: WC_REST_Orders_V2_Controller{}

Хуков нет.

Возвращает

WC_Order_Item_Product.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->prepare_line_items( $posted, $action, $item );
$posted(массив) (обязательный)
Line item data.
$action(строка)
'create' to add line item or 'update' to update it.
По умолчанию: 'create'
$item(объект)
Passed when updating an item. Null during creation.
По умолчанию: null

Код WC_REST_Orders_V2_Controller::prepare_line_items() WC 8.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;
}