productRepository = $productRepository; $this->logger = $logger; } /** * Add cost variables before processing template */ public function beforeProcessTemplate($subject, array $variables = []) { if (!$this->isProcessing) { $this->logger->info('TemplatePlugin: beforeProcessTemplate called'); // Check if we have an order in variables if (isset($variables['order']) && $variables['order'] instanceof Order) { $this->logger->info('TemplatePlugin: Found order ' . $variables['order']->getIncrementId()); $variables = $this->addCostVariables($variables, $variables['order']); } } return [$variables]; } /** * Hook into setVars to add our variables */ public function afterSetVars($subject, $result, $variables) { // Only process once to avoid infinite loop if (!$this->isProcessing && is_array($variables) && isset($variables['order']) && $variables['order'] instanceof Order) { $this->isProcessing = true; $this->logger->info('TemplatePlugin: afterSetVars called - adding cost variables'); // Add cost variables $enhancedVariables = $this->addCostVariables($variables, $variables['order']); // Set variables back $subject->setVars($enhancedVariables); $this->isProcessing = false; } return $result; } /** * Add cost variables to the variables array */ protected function addCostVariables($variables, $order) { // Check if we already added these variables if (isset($variables['items_html'])) { $this->logger->info('Cost variables already exist, skipping'); return $variables; } try { // Build ONLY the table rows (tbody content) $itemsHtml = ''; $totalCost = 0; $itemCount = 0; foreach ($order->getAllVisibleItems() as $item) { $itemCount++; try { $product = $this->productRepository->getById($item->getProductId()); $cost = (float)($product->getCost() ?: $product->getData('cost') ?: 0); $this->logger->info('Product ' . $item->getSku() . ' cost: ' . $cost); } catch (\Exception $e) { $this->logger->error('Could not load product: ' . $e->getMessage()); $cost = 0; } $qty = (float) $item->getQtyOrdered(); $rowTotal = $cost * $qty; $totalCost += $rowTotal; // Just the row content $itemsHtml .= ''; $itemsHtml .= '' . htmlspecialchars($item->getName()) . ''; $itemsHtml .= '' . htmlspecialchars($item->getSku()) . ''; $itemsHtml .= '' . (int)$qty . ''; $itemsHtml .= '' . $this->formatPrice($cost, $order) . ''; $itemsHtml .= '' . $this->formatPrice($rowTotal, $order) . ''; $itemsHtml .= ''; } $shippingCost = $order->getShippingAmount() ?: 0; // Create a complete table HTML for standalone use $completeTableHtml = ''; $completeTableHtml .= ''; $completeTableHtml .= ''; $completeTableHtml .= ''; $completeTableHtml .= ''; $completeTableHtml .= ''; $completeTableHtml .= ''; $completeTableHtml .= ''; $completeTableHtml .= ''; $completeTableHtml .= ''; $completeTableHtml .= ''; $completeTableHtml .= $itemsHtml; $completeTableHtml .= ''; $completeTableHtml .= ''; $completeTableHtml .= ''; $completeTableHtml .= ''; $completeTableHtml .= ''; $completeTableHtml .= ''; if ($shippingCost > 0) { $completeTableHtml .= ''; $completeTableHtml .= ''; $completeTableHtml .= ''; $completeTableHtml .= ''; } $completeTableHtml .= ''; $completeTableHtml .= ''; $completeTableHtml .= ''; $completeTableHtml .= ''; $completeTableHtml .= ''; $completeTableHtml .= '
ItemSKUQtyUnit CostTotal Cost
Subtotal:' . $this->formatPrice($totalCost, $order) . '
Shipping:' . $this->formatPrice($shippingCost, $order) . '
Grand Total:' . $this->formatPrice($totalCost + $shippingCost, $order) . '
'; $terms = $order->getData('terms') ?? ''; // Or $order->getTerms() if it's a getter $termsHtml = str_replace(',', '
', htmlspecialchars($terms)); // Add variables for both approaches $variables['items_html'] = $itemsHtml; // Just rows for inserting into existing table $variables['items_complete_table'] = $completeTableHtml; // Complete table $variables['order_subtotal_cost'] = $this->formatPrice($totalCost, $order); $variables['order_total_cost'] = $this->formatPrice($totalCost + $shippingCost, $order); $variables['shipping_cost'] = $this->formatPrice($shippingCost, $order); $variables['raw_subtotal_cost'] = $totalCost; $variables['raw_total_cost'] = $totalCost + $shippingCost; $variables['raw_shipping_cost'] = $shippingCost; $variables['terms_html'] = $termsHtml; // Test variable $variables['test_cost_injection'] = 'WORKING'; $this->logger->info('Added cost variables - Items: ' . $itemCount . ', Total: ' . $totalCost); } catch (\Exception $e) { $this->logger->error('Error adding cost variables: ' . $e->getMessage()); } return $variables; } protected function formatPrice($price, $order) { return $order->getOrderCurrency()->formatPrecision($price, 2, [], false); } }