', // ePOS attributes are printer state that persists across jobs, so // start from a known reset and switch emphasis off again explicitly. '', '' . $this->escape( $store_name ) . "\n" . '', 'RECEIPT' . "\n" . '', 'Order #' . $this->escape( $order_number ) . "\n" . '', ); foreach ( $this->line_items( $receipt_data ) as $item ) { $name = isset( $item['name'] ) ? (string) $item['name'] : ''; $quantity = isset( $item['quantity'] ) ? (float) $item['quantity'] : (float) ( $item['qty'] ?? 1 ); $item_total = isset( $item['total'] ) ? (float) $item['total'] : (float) ( $item['line_total_incl'] ?? 0 ); $lines[] = '' . $this->escape( $name ) . ' x' . $this->format_quantity( $quantity ) . ' ' . $this->format_money( $item_total ) . "\n" . ''; } $lines[] = 'Total ' . $this->escape( $this->format_money( $total ) ) . "\n" . ''; $lines[] = ''; $lines[] = ''; return implode( '', $lines ); } /** * Read line items from known canonical keys. * * @param array $receipt_data Canonical payload. * * @return array */ private function line_items( array $receipt_data ): array { if ( isset( $receipt_data['lines'] ) && is_array( $receipt_data['lines'] ) ) { return $receipt_data['lines']; } if ( isset( $receipt_data['line_items'] ) && is_array( $receipt_data['line_items'] ) ) { return $receipt_data['line_items']; } if ( isset( $receipt_data['items'] ) && is_array( $receipt_data['items'] ) ) { return $receipt_data['items']; } return array(); } /** * Escape XML text content. * * @param string $value Raw text. * * @return string */ private function escape( string $value ): string { return htmlspecialchars( $value, ENT_XML1 | ENT_COMPAT, 'UTF-8' ); } /** * Format an item quantity for receipt output. * * @param float $quantity Quantity. * * @return string */ private function format_quantity( float $quantity ): string { return wc_format_decimal( $quantity, 2 ); } /** * Format a money amount for receipt output. * * @param float $amount Amount. * * @return string */ private function format_money( float $amount ): string { return get_woocommerce_currency() . ' ' . wc_format_decimal( $amount, wc_get_price_decimals() ); } }