Automattic\WooCommerce\Gateways\PayPal

Request::get_paypal_order_itemsprivateWC 1.0

Get the order items for the PayPal create-order request. Returns an empty array if any of the items (amount, quantity) are invalid.

Method of the class: Request{}

No Hooks.

Returns

Array.

Usage

// private - for code of main (parent) class only
$result = $this->get_paypal_order_items( $order ): array;
$order(WC_Order) (required)
Order object.

Request::get_paypal_order_items() code WC 10.7.0

private function get_paypal_order_items( WC_Order $order ): array {
	$items = array();

	foreach ( $order->get_items( array( 'line_item', 'fee' ) ) as $item ) {
		$item_amount = $this->get_paypal_order_item_amount( $order, $item );
		if ( $item_amount < 0 ) {
			// PayPal does not accept negative item amounts in the items breakdown, so we return an empty list.
			return array();
		}

		$quantity = $item->get_quantity();
		// PayPal does not accept zero or fractional quantities.
		if ( ! is_numeric( $quantity ) || $quantity <= 0 || floor( $quantity ) != $quantity ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual
			return array();
		}

		$items[] = array(
			'name'        => $this->limit_length( $item->get_name(), PayPalConstants::PAYPAL_ORDER_ITEM_NAME_MAX_LENGTH ),
			'quantity'    => $item->get_quantity(),
			'unit_amount' => array(
				'currency_code' => $order->get_currency(),
				// Use the subtotal before discounts.
				'value'         => wc_format_decimal( $item_amount, wc_get_price_decimals() ),
			),
		);
	}

	return $items;
}