WordPress как на ладони
Недорогой хостинг для сайтов на WordPress: wordpress.jino.ru
функция не описана

WC_API_Orders::set_line_item() protected WC 2.2

Create or update a line item

{} Это метод класса: WC_API_Orders{}

Хуков нет.

Возвращает

null. Ничего.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->set_line_item( $order, $item, $action );
$order(\WC_Order) (обязательный)
-
$item(массив) (обязательный)
line item data
$action(строка) (обязательный)
'create' to add line item or 'update' to update it

Список изменений

С версии 2.2 Введена.

Код WC_API_Orders::set_line_item() WC 5.6.0

protected function set_line_item( $order, $item, $action ) {
	$creating  = ( 'create' === $action );

	// product is always required
	if ( ! isset( $item['product_id'] ) && ! isset( $item['sku'] ) ) {
		throw new WC_API_Exception( 'woocommerce_api_invalid_product_id', __( 'Product ID or SKU is required', 'woocommerce' ), 400 );
	}

	// when updating, ensure product ID provided matches
	if ( 'update' === $action ) {

		$item_product_id   = wc_get_order_item_meta( $item['id'], '_product_id' );
		$item_variation_id = wc_get_order_item_meta( $item['id'], '_variation_id' );

		if ( $item['product_id'] != $item_product_id && $item['product_id'] != $item_variation_id ) {
			throw new WC_API_Exception( 'woocommerce_api_invalid_product_id', __( 'Product ID provided does not match this line item', 'woocommerce' ), 400 );
		}
	}

	if ( isset( $item['product_id'] ) ) {
		$product_id = $item['product_id'];
	} elseif ( isset( $item['sku'] ) ) {
		$product_id = wc_get_product_id_by_sku( $item['sku'] );
	}

	// variations must each have a key & value
	$variation_id = 0;
	if ( isset( $item['variations'] ) && is_array( $item['variations'] ) ) {
		foreach ( $item['variations'] as $key => $value ) {
			if ( ! $key || ! $value ) {
				throw new WC_API_Exception( 'woocommerce_api_invalid_product_variation', __( 'The product variation is invalid', 'woocommerce' ), 400 );
			}
		}
		$variation_id = $this->get_variation_id( wc_get_product( $product_id ), $item['variations'] );
	}

	$product = wc_get_product( $variation_id ? $variation_id : $product_id );

	// must be a valid WC_Product
	if ( ! is_object( $product ) ) {
		throw new WC_API_Exception( 'woocommerce_api_invalid_product', __( 'Product is invalid.', 'woocommerce' ), 400 );
	}

	// quantity must be positive float
	if ( isset( $item['quantity'] ) && floatval( $item['quantity'] ) <= 0 ) {
		throw new WC_API_Exception( 'woocommerce_api_invalid_product_quantity', __( 'Product quantity must be a positive float.', 'woocommerce' ), 400 );
	}

	// quantity is required when creating
	if ( $creating && ! isset( $item['quantity'] ) ) {
		throw new WC_API_Exception( 'woocommerce_api_invalid_product_quantity', __( 'Product quantity is required.', 'woocommerce' ), 400 );
	}

	if ( $creating ) {
		$line_item = new WC_Order_Item_Product();
	} else {
		$line_item = new WC_Order_Item_Product( $item['id'] );
	}

	$line_item->set_product( $product );
	$line_item->set_order_id( $order->get_id() );

	if ( isset( $item['quantity'] ) ) {
		$line_item->set_quantity( $item['quantity'] );
	}
	if ( isset( $item['total'] ) ) {
		$line_item->set_total( floatval( $item['total'] ) );
	} elseif ( $creating ) {
		$total = wc_get_price_excluding_tax( $product, array( 'qty' => $line_item->get_quantity() ) );
		$line_item->set_total( $total );
		$line_item->set_subtotal( $total );
	}
	if ( isset( $item['total_tax'] ) ) {
		$line_item->set_total_tax( floatval( $item['total_tax'] ) );
	}
	if ( isset( $item['subtotal'] ) ) {
		$line_item->set_subtotal( floatval( $item['subtotal'] ) );
	}
	if ( isset( $item['subtotal_tax'] ) ) {
		$line_item->set_subtotal_tax( floatval( $item['subtotal_tax'] ) );
	}
	if ( $variation_id ) {
		$line_item->set_variation_id( $variation_id );
		$line_item->set_variation( $item['variations'] );
	}

	// Save or add to order.
	if ( $creating ) {
		$order->add_item( $line_item );
	} else {
		$item_id = $line_item->save();

		if ( ! $item_id ) {
			throw new WC_API_Exception( 'woocommerce_cannot_create_line_item', __( 'Cannot create line item, try again.', 'woocommerce' ), 500 );
		}
	}
}