decimal = ( ',' === wc_get_price_decimal_separator() && ',' !== $request->delimiter ) ? ',' : '.'; $this->lines = LineMatcher::from_filters( $request->filters ); } /** * Preview rows for the UI. * * @param int $limit Row cap. * @return array{headers: string[], keys: string[], rows: array, total: int} */ public function preview( int $limit = 10 ): array { $query = new Query( $this->request->filters ); $rows = []; foreach ( $query->preview( $limit ) as $order ) { foreach ( $this->rows_for( $order ) as $row ) { $rows[] = $row; } } return [ 'headers' => $this->request->headers(), 'keys' => $this->request->fields, 'rows' => array_slice( $rows, 0, $limit ), 'total' => $query->count(), ]; } /** * Write the full export to a file writer (CSV or XLSX) and complete it. * * @param RowWriter $writer Destination. * @return int Number of rows written (excluding header). */ public function write( RowWriter $writer ): int { $writer->row( $this->request->headers() ); $count = 0; $query = new Query( $this->request->filters ); foreach ( $query->iterate() as $order ) { foreach ( $this->rows_for( $order ) as $row ) { $writer->row( $row ); ++$count; } if ( 0 === $count % 200 ) { $writer->flush(); } } $writer->close(); /** * Fires after an export completes. * * @param Request $request The export request. * @param int $count Rows written. */ do_action( 'modulary_export_completed', $this->request, $count ); return $count; } /** * The rows one order adds to the file, exactly as write() writes them. * A background export (Job) collects these a batch at a time. * * @param \WC_Order $order Order. * @return array> */ public function rows( \WC_Order $order ): array { return $this->rows_for( $order ); } /** * One order → one row, or one row per product line. * * Order-level columns repeat on every line of the same order, except money * columns flagged `once`, which only fill the first line so column sums * stay correct. Line-item columns (scope "item") are filled per line and * stay empty in order mode. * * @param \WC_Order $order Order. * @return array> */ private function rows_for( \WC_Order $order ): array { $fields = $this->request->fields; $options = $this->request->options; $base = array_map( static fn( string $key ) => 'item' === Fields::scope( $key ) ? '' : Fields::resolve( $key, $order, $options[ $key ] ?? [] ), $fields ); if ( 'item' !== $this->request->rows ) { return [ $this->localise( $base ) ]; } $rows = []; foreach ( $order->get_items() as $item ) { if ( ! $item instanceof \WC_Order_Item_Product ) { continue; } // Filtered by product or category: only the lines that were asked for. if ( ! $this->lines->matches( $item ) ) { continue; } $row = $base; foreach ( $fields as $i => $key ) { if ( 'item' === Fields::scope( $key ) ) { $row[ $i ] = Fields::resolve_item( $key, $item, $order, $options[ $key ] ?? [] ); } elseif ( 'items' === $key ) { $row[ $i ] = $item->get_quantity() . '× ' . $item->get_name(); } elseif ( 'skus' === $key ) { $product = $item->get_product(); $row[ $i ] = $product ? (string) $product->get_sku() : ''; } elseif ( 'item_count' === $key ) { $row[ $i ] = (string) $item->get_quantity(); } } if ( [] !== $rows ) { foreach ( $fields as $i => $key ) { if ( Fields::once( $key ) ) { $row[ $i ] = ''; } } } $rows[] = $this->localise( $row ); } return [] === $rows ? [ $this->localise( $base ) ] : $rows; } /** * Apply the money decimal separator to one row. * * @param array $row Cells in field order. * @return array */ private function localise( array $row ): array { if ( '.' === $this->decimal ) { return $row; } foreach ( $this->request->fields as $i => $key ) { if ( Fields::money( $key ) && isset( $row[ $i ] ) && is_numeric( $row[ $i ] ) ) { $row[ $i ] = str_replace( '.', $this->decimal, $row[ $i ] ); } } return $row; } }