create_style_string( $this->get_element_styles() ); $columns = $this->get_property( 'columns', array( array( 'label' => 'Produit', 'key' => 'name', ), array( 'label' => 'Quantité', 'key' => 'quantity', ), array( 'label' => 'Prix', 'key' => 'price', ), array( 'label' => 'Total', 'key' => 'total', ), ) ); $items = $this->order_data['products'] ?? $this->order_data['items'] ?? array(); return '
' . $this->buildProductTableHtml( $columns, $items ) . '
'; } /** * Map legacy column key aliases to actual OrderDataExtractor keys. */ private const KEY_ALIASES = array( 'product_name' => 'name', 'product_qty' => 'quantity', 'product_price' => 'price', 'product_total' => 'total', 'product_sku' => 'sku', 'product_image' => 'image', ); /** * Build product table HTML. * * @param array $columns Table columns. * @param array $items Table items. */ private function buildProductTableHtml( array $columns, array $items ): string { $header = $this->buildTableHeader( $columns ); $body = $this->buildTableBody( $columns, $items ); $tfoot = $this->buildTableFooter( $columns ); return '' . $header . '' . $tfoot . '' . $body . '
'; } /** * En-tĂȘte du tableau. * * @param array $columns Table columns. */ private function buildTableHeader( array $columns ): string { $header = ''; foreach ( $columns as $column ) { $header .= '' . htmlspecialchars( $column['label'] ?? '' ) . ''; } return $header . ''; } /** * Corps du tableau (lignes produits ou ligne placeholder si vide). * * @param array $columns Table columns. * @param array $items Table items. */ private function buildTableBody( array $columns, array $items ): string { if ( empty( $items ) ) { $row = ''; foreach ( $columns as $column ) { $row .= '-'; } return $row . ''; } $body = ''; foreach ( $items as $item ) { $row = ''; foreach ( $columns as $column ) { $row .= $this->buildTableCell( $column, $item ); } $body .= $row . ''; } return $body; } /** * Cellule de tableau (image ou texte). * * @param array $column Column definition. * @param array $item Item data. */ private function buildTableCell( array $column, array $item ): string { $raw_key = $column['key'] ?? ''; $key = self::KEY_ALIASES[ $raw_key ] ?? $raw_key; $value = $item[ $key ] ?? $item[ $raw_key ] ?? '-'; if ( 'image' === $key && ! empty( $value ) ) { return ''; } return '' . htmlspecialchars( (string) $value ) . ''; } /** * Pied de tableau (totaux) - retourne '' si pas de totaux. * * @param array $columns Table columns. */ private function buildTableFooter( array $columns ): string { $totals = $this->order_data['totals'] ?? array(); if ( empty( $totals ) ) { return ''; } $col_count = count( $columns ); $last_two = $col_count >= 2 ? $col_count - 2 : 0; $tfoot = ''; if ( ! empty( $totals['subtotal'] ) ) { $tfoot .= '' . 'Sous-total' . '' . htmlspecialchars( (string) $totals['subtotal'] ) . ''; } if ( ! empty( $totals['tax'] ) && ( $totals['tax_raw'] ?? 0 ) > 0 ) { $tfoot .= '' . 'TVA' . '' . htmlspecialchars( (string) $totals['tax'] ) . ''; } if ( ! empty( $totals['total'] ) ) { $tfoot .= '' . 'TOTAL' . '' . htmlspecialchars( (string) $totals['total'] ) . ''; } return $tfoot . ''; } }