Automattic\WooCommerce\Internal\RestApi\Routes\V4\Orders

UpdateUtils::get_product_id_from_line_itemprotectedWC 1.0

Gets the product ID from the SKU or posted ID.

Method of the class: UpdateUtils{}

No Hooks.

Returns

Int.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_product_id_from_line_item( $request_data, $action );
$request_data(array) (required)
Request data.
$action(string)
'create' to add line item or 'update' to update it.
Default: 'create'

UpdateUtils::get_product_id_from_line_item() code WC 10.4.3

protected function get_product_id_from_line_item( $request_data, $action = 'create' ) {
	if ( ! empty( $request_data['sku'] ) ) {
		$product_id = (int) wc_get_product_id_by_sku( $request_data['sku'] );
	} elseif ( ! empty( $request_data['product_id'] ) && empty( $request_data['variation_id'] ) ) {
		$product_id = (int) $request_data['product_id'];
	} elseif ( ! empty( $request_data['variation_id'] ) ) {
		$product_id = (int) $request_data['variation_id'];
	} elseif ( 'update' === $action ) {
		$product_id = 0;
	} else {
		throw new WC_REST_Exception( 'woocommerce_rest_required_product_reference', esc_html__( 'Product ID or SKU is required.', 'woocommerce' ), 400 );
	}
	return $product_id;
}